【问题标题】:In AWS, How can i find if a given vpc id is a valid one using the aws cli?在 AWS 中,如何使用 aws cli 查找给定的 vpc id 是否有效?
【发布时间】:2019-12-09 16:55:09
【问题描述】:

我在我的 jenkins 文件中使用以下内容

sh aws ec2 describe-vpcs --vpc-ids ${VPC_ID} --query 'Vpcs[*].[State]' --output text

如果有效则给我available

否则它会给我错误:

An error occurred (InvalidVpcID.NotFound) when calling the DescribeVpcs operation: The vpc ID 'vpc-0xx0xx0xx' does not exist

有没有办法使用这种方法优雅地捕捉响应?

【问题讨论】:

    标签: amazon-web-services jenkins-pipeline aws-cli amazon-vpc jenkins-groovy


    【解决方案1】:

    我认为您可以使用过滤器而不是 --vpc-ids。

    aws ec2 describe-vpcs --filters Name=vpc-id,Values="id" --query 'Vpcs[*].[State]'
    

    它不会报错,只是空的。

    此外,这是我修改过的。由于 vpc-id 是唯一的,所以设置 vpc-id 时不需要 [*],只需选择第一个,state 没有括号。

    aws ec2 describe-vpcs --filters Name=vpc-id,Values="id" --query 'Vpcs[0].State'
    

    然后,结果是这样的

    "avalilable"
    

    null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 2015-01-03
      • 2016-12-04
      • 2015-01-23
      • 2021-03-02
      相关资源
      最近更新 更多