如何检索用于启动 Fargate 任务的子网列表?

How to retrieve a list of subnets for launching a Fargate task?

我在 Visual Studio 2017 年使用带有 C# 的 AWS SDK,并且有一个原型可以在 ECS 中启动 Fargate 服务。作为设置的一部分,您实例化一个 CreateServiceRequest 对象,该对象需要 NetworkConfiguration.AwsVpcConfiguration 设置 SecurityGroupsSubnets.

var request = new CreateServiceRequest();

request.ServiceName = "myService";
request.TaskDefinition = "myTask";   // family[:revision] of the task definition to use
request.ClientToken = Guid.NewGuid().ToString().Replace("-", "");  // max 32 characters!
request.Cluster = "default";
request.DesiredCount = 1;
request.LaunchType = LaunchType.FARGATE;

request.DeploymentConfiguration = new DeploymentConfiguration
{
    MaximumPercent = 100,
    MinimumHealthyPercent = 50
};

// configure the network and security groups for the task
List<string> securityGroups = new List<string>();
securityGroups.Add("sg-123456");

List<string> subnets = new List<string>();
subnets.Add("subnet-9b36aa97");

request.NetworkConfiguration = new NetworkConfiguration
{
    AwsvpcConfiguration = new AwsVpcConfiguration
    {
        AssignPublicIp = AssignPublicIp.ENABLED,
        SecurityGroups = securityGroups,
        Subnets = subnets
    }
};

当我通过 AWS 控制台手动配置服务时,它们会显示可供选择的子网列表。所以,我想知道如何以编程方式检索我们的 VPC 中可用的子网列表。

我正在他们的 SDK documentation 中搜索可能的解决方案,感谢任何指向他们的文档或示例的指针!

看看EC2Client,你会发现很多VPC相关的API都与EC2服务相关联。具体查看 AmazonEC2Client.DescribeSubnets(DescribeSubnetsRequest)、方法文档 here:

请求

Amazon.EC2.Model.DescribeSubnetsRequest

回应

Amazon.EC2.Model.DescribeSubnetsResponse

响应包含 Amazon.EC2.Model.Subnet 的列表,在决定将哪个子网传递给您的 Fargate 请求时,您将从中检索字符串 属性 SubnetId

示例用法(来自链接文档):

var response = client.DescribeSubnets(new DescribeSubnetsRequest 
{
    Filters = new List<filter> {
        new Filter {
            Name = "vpc-id",
            Values = new List<string> {
                "vpc-a01106c2"
            }
        }
    }
});

List<subnet> subnets = response.Subnets;

进一步阅读