我在Visual Studio 2017中使用带有C#的AWS开发工具包,并使用原型工作在ECS中启动Fargate服务。作为设置的一部分,您需要实例化一个CreateServiceRequest
对象,该对象需要NetworkConfiguration.AwsVpcConfiguration
设置SecurityGroups
和Subnets
。
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以寻找可能的解决方案,我们非常感谢您对其文档或示例的任何指示!
答案 0 :(得分:2)
看看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;