如何使用Go AWS SDK在特定的AWS区域中描述VPCs?

huangapple go评论93阅读模式
英文:

How to DescribeVPCs in a particular AWS region using Go AWS SDK?

问题

我想查询属于特定区域的所有VPC,该VPC是基于我的Go微服务的。

https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcs.html 表明不存在按区域或任何其他请求参数进行过滤的选项。

Golang SDK 参考文档:
https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#EC2.DescribeVpcs

命令行 SDK 参考文档:https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-vpcs.html

这是一个示例。

{
    "Vpcs": [
        {
            "CidrBlock": "30.1.0.0/16",
            "DhcpOptionsId": "dopt-19edf471",
            "State": "available",
            "VpcId": "vpc-0e9801d129EXAMPLE",
            "OwnerId": "111122223333",
            "InstanceTenancy": "default",
            "CidrBlockAssociationSet": [
                {
                    "AssociationId": "vpc-cidr-assoc-062c64cfafEXAMPLE",
                    "CidrBlock": "30.1.0.0/16",
                    "CidrBlockState": {
                        "State": "associated"
                    }
                }
            ],
            "IsDefault": false,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "Not Shared"
                }
            ]
        }
    ]
}

然而,如果我使用以下命令

$aws ec2 describe-vpcs --region us-west-1

那么我可以查询区域us-west-1中的所有vpcs

问题1:为什么CLI SDK文档中没有提到--region选项?

问题2:在使用GO SDK时,我该如何在DescribeVpcsInput中加入相同的选项?

英文:

I want to query all VPCs belonging to a particular region in my Go-based microservice.

https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcs.html indicates that there exists no filter option by region or any other request parameter.

Golang SDK reference document:
https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#EC2.DescribeVpcs

Command line SDK reference document: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-vpcs.html

Here's an example.

{
    "Vpcs": [
        {
            "CidrBlock": "30.1.0.0/16",
            "DhcpOptionsId": "dopt-19edf471",
            "State": "available",
            "VpcId": "vpc-0e9801d129EXAMPLE",
            "OwnerId": "111122223333",
            "InstanceTenancy": "default",
            "CidrBlockAssociationSet": [
                {
                    "AssociationId": "vpc-cidr-assoc-062c64cfafEXAMPLE",
                    "CidrBlock": "30.1.0.0/16",
                    "CidrBlockState": {
                        "State": "associated"
                    }
                }
            ],
            "IsDefault": false,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "Not Shared"
                }
            ]
        }
    ]
}

However, if I use the command

$aws ec2 describe-vpcs --region us-west-1

then I can query all vpcs in region us-west-1.

Question 1. Why is the --region option not mentioned in the CLI SDK document?

Question 2. How can I incorporate the same in DescribeVpcsInput while using GO SDK?

答案1

得分: 1

--region标志在CLI上不是一个过滤器,而是一个必需的设置,用于告诉AWS CLI要连接到哪个区域。ec2 describe-vpcs命令始终限制在单个区域内(大多数AWS命令都是如此)。

您还可以使用所需的区域配置AWS SDK客户端。请参阅此处的"指定AWS区域" 链接

英文:

The --region flag on the CLI is not a filter, it is a required setting that tells the AWS CLI what region to connect to. The ec2 describe-vpcs command is always limited to a single region (most AWS commands are).

You would configure your AWS SDK client with the region you want it to connect to as well. See "Specifying the AWS Region" here.

huangapple
  • 本文由 发表于 2022年7月29日 11:04:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/73161212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定