public async Task<string> GetPrivateSubnetIdAsync(string vpcId, string availabilityZone)
{
using (var ec2Client = new AmazonEC2Client(credentials, ec2ConfigurationProvider.RegionEndpoint))
{
var allSubnetsInMyVpcAndAz = await ec2Client.DescribeSubnetsAsync(new DescribeSubnetsRequest
{
Filters = new List<Filter>
{
new Filter {Name = "vpc-id", Values = new List<string> {vpcId}},
new Filter {Name = "availabilityZone", Values = new List<string> {availabilityZone}}
}
});
var privateSubnet = allSubnetsInMyVpcAndAz.Subnets.SingleOrDefault(s => s.Tags.Any(t =>
string.Equals(t.Key, "AccessLevel", StringComparison.CurrentCultureIgnoreCase) &&
string.Equals(t.Value, "private", StringComparison.CurrentCultureIgnoreCase)));
if (privateSubnet == null)
{
var message = $"Could not locate private subnet for vpc:{vpcId} and az:{availabilityZone}";
loggerProvider.GetLogger().Error(message);
throw new Ec2ServiceException(message);
}
return privateSubnet.SubnetId;
}
}