AWS CDK: VPC子网与另一个子网冲突

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

AWS CDK: Vpc subnet conflicts with another subnet

问题

I want to create an OpenSearch domain in AWS CDK within a VPC. I've made some progress, but I'm stuck with an issue regarding CIDR.

var vpc = new Vpc(scope, "Vpc");
var subnet = new Subnet(scope, "Subnet", new SubnetProps
{
     VpcId = vpc.VpcId,
     CidrBlock = "10.0.1.0/24",
     AvailabilityZone = "us-east-1a"
});
var domain = new Domain(scope, "Domain", new DomainProps
{
     Vpc = vpc,
     VpcSubnets = new SubnetSelection[] 
     { 
         new SubnetSelection
         {
             Subnets = new Subnet[]
             {
                 subnet
             }
         }
     },
     SecurityGroups = new SecurityGroup[]
     {
         new SecurityGroup(scope, "SecurityGroup", new SecurityGroupProps
         {
             Vpc = vpc
         })
     },
     // other property initializations are omitted
}

The error I'm getting is The CIDR '10.0.1.0/24' conflicts with another subnet. How can this be true if I'm creating a brand new Vpc with only one subnet? I assume there may be more subnets that are created implicitly. How can I address this issue?

Currently I don't care for multiple AZs so I need to place the domain within a single subnet.

英文:

I want to create an OpenSearch domain in AWS CDK within a VPC. I've made some progress, but I'm stuck with an issue regarding CIDR.

var vpc = new Vpc(scope, "Vpc");
var subnet = new Subnet(scope, "Subnet", new SubnetProps
{
     VpcId = vpc.VpcId,
     CidrBlock = "10.0.1.0/24",
     AvailabilityZone = "us-east-1a"
});
var domain = new Domain(scope, "Domain", new DomainProps
{
     Vpc = vpc,
     VpcSubnets = new SubnetSelection[] 
     { 
         new SubnetSelection
         {
             Subnets = new Subnet[]
             {
                 subnet
             }
         }
     },
     SecurityGroups = new SecurityGroup[]
     {
         new SecurityGroup(scope, "SecurityGroup", new SecurityGroupProps
         {
             Vpc = vpc
         })
     },
     // other property initializations are omitted
}

The error I'm getting is The CIDR '10.0.1.0/24' conflicts with another subnet. How can this be true if I'm creating a brand new Vpc with only one subnet? I assume there may be more subnets that are created implicitly. How can I address this issue?

Currently I don't care for multiple AZs so I need to place the domain within a single subnet.

答案1

得分: 2

Vpc vpc = new Vpc(this, "VPC", new VpcProps {
    IpAddresses = IpAddresses.Cidr("10.0.0.0/24")
    MaxAzs = 1,
    SubnetConfiguration = new ISubnetConfiguration[]
    {
        new SubnetConfiguration
        {
            SubnetType = SubnetType.PUBLIC,
            Name = "Public"
        }
    }
});
英文:

The VPC construct's IpAddresses and SubnetConfiguration props allow you to customise the CIDR range and subnets.

Your VPC constructor is not defining these props, so the defaults (10.0.0.0/16 allocated to 2 subnets) are applied instead. These defaults are conflicting with your manually created subnet.

Pardon my bad C#, but you'll want a VPC constructor something like this:

Vpc vpc = new Vpc(this, "VPC", new VpcProps {
    IpAddresses = IpAddresses.Cidr("10.0.0.0/24")
    MaxAzs = 1,
    SubnetConfiguration = new ISubnetConfiguration[]
    {
        new SubnetConfiguration
        {
            SubnetType = SubnetType.PUBLIC,
            Name = "Public"
        }
    }
});

See the Ip Address Management section in the docs for details.

huangapple
  • 本文由 发表于 2023年2月14日 00:26:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438614.html
匿名

发表评论

匿名网友

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

确定