मैं कोशिश कर रहा हूँ बनाने के लिए एक VPC के साथ सबनेट के लिए एक परियोजना है, अभी तक मैं चल रहा हूँ, एक मामले में जहां CDK लगता है बनाने के लिए दो सबनेट के साथ ही सीआईडीआर ब्लॉक synthesizing जब कोड एक CloudFormation टेम्पलेट, हालांकि मैं का उपयोग केवल एक अंतरफलक के ब्लॉक में एक बार प्रति सबनेट घोषणा. इस कारण परिनियोजन विफल करने के लिए के बाद से अंतरफलक के ब्लॉक एक दूसरे के साथ संघर्ष जब बनाने सबनेट. यहाँ कोड को परिभाषित करने के लिए ढेर:
from aws_cdk import core as cdk
from aws_cdk import aws_ec2 as ec2
class CdkWorkshop3Stack(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# The code that defines your stack goes here
# instantiate VPC with dns support and hostname enabled and cidr block at 10.0.0.0/24
vpc = ec2.Vpc(self, "vpc-cf",
cidr="10.0.0.0/24",
enable_dns_support=True,
enable_dns_hostnames=True
)
# instantiate internet gateway and attach VPC with internet gateway
igw = ec2.CfnInternetGateway(self, "igw")
igw_attach = ec2.CfnVPCGatewayAttachment(
self, "igw_attach", vpc_id=vpc.vpc_id, internet_gateway_id=igw.attr_internet_gateway_id)
# instantiate elastic IP with VPC domin
eip = ec2.CfnEIP(self, "eip", domain="VPC")
# instantiate public and private subnets and use first availability zone
pub_subnetA = ec2.Subnet(
self, "public-subnetA", availability_zone=super().availability_zones[0], cidr_block="10.0.0.0/26", vpc_id=vpc.vpc_id, map_public_ip_on_launch=True)
pub_subnetB = ec2.Subnet(
self, "public-subnetB", availability_zone=super().availability_zones[0], cidr_block="10.0.0.64/26", vpc_id=vpc.vpc_id, map_public_ip_on_launch=True)
pri_subnetA = ec2.Subnet(
self, "private-subnetA", availability_zone=super().availability_zones[0], cidr_block="10.0.0.128/26", vpc_id=vpc.vpc_id, map_public_ip_on_launch=False)
pri_subnetB = ec2.Subnet(
self, "private-subnetB", availability_zone=super().availability_zones[0], cidr_block="10.0.0.192/26", vpc_id=vpc.vpc_id, map_public_ip_on_launch=False)
# instantiate NAT gateway
nat_gateway = ec2.CfnNatGateway(
self, "nat_gateway", allocation_id=eip.attr_allocation_id, subnet_id=pub_subnetA.subnet_vpc_id)
# instantiate routing tables
pub_route_table = ec2.CfnRouteTable(
self, "pub_route_table", vpc_id=vpc.vpc_id)
pri_route_table = ec2.CfnRouteTable(
self, "pri_route_table", vpc_id=vpc.vpc_id)
# instantiate public and private routes
pub_route = ec2.CfnRoute(self, "pub_route", route_table_id=pub_route_table.attr_route_table_id,
destination_cidr_block="0.0.0.0/0", gateway_id=igw.attr_internet_gateway_id)
pri_route = ec2.CfnRoute(self, "pri_route", route_table_id=pri_route_table.attr_route_table_id,
destination_cidr_block="0.0.0.0/0", nat_gateway_id=nat_gateway.ref)
# instantiate subnet route table associations
pub_subnetA_route_table_association = ec2.CfnSubnetRouteTableAssociation(
self, "pub_subnetA_route_table_association", route_table_id=pub_route_table.attr_route_table_id, subnet_id=pub_subnetA.subnet_vpc_id)
pub_subnetB_route_table_association = ec2.CfnSubnetRouteTableAssociation(
self, "pub_subnetB_route_table_association", route_table_id=pub_route_table.attr_route_table_id, subnet_id=pub_subnetB.subnet_vpc_id)
pri_subnetA_route_table_association = ec2.CfnSubnetRouteTableAssociation(
self, "pri_subnetA_route_table_association", route_table_id=pri_route_table.attr_route_table_id, subnet_id=pri_subnetA.subnet_vpc_id)
pri_subnetB_route_table_association = ec2.CfnSubnetRouteTableAssociation(
self, "pri_subnetB_route_table_association", route_table_id=pri_route_table.attr_route_table_id, subnet_id=pri_subnetB.subnet_vpc_id)
security_group = ec2.SecurityGroup(self, "security_group", vpc=vpc)
security_group.add_ingress_rule(
ec2.Peer.ipv4("0.0.0.0/0"), ec2.Port.tcp(22))
ec2_instance = ec2.Instance(self, "EC2", instance_type=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO), machine_image=ec2.MachineImage.latest_amazon_linux(),
key_name="RandomKeyName", security_group=security_group, vpc=vpc, vpc_subnets=pri_subnetA)
मैं नहीं जानता कि कैसे इस मुद्दे को हल करने के बाद से, यह स्पष्ट रूप से करना चाहिए केवल उन का उपयोग करें अंतरफलक के साथ ब्लॉक में एक बार. लेकिन यह नहीं करता है ।
संपादित करें: मैं अब कर रहा हूँ सहित एक लिंक करने के लिए CloudFormation टेम्पलेट का उत्पादन करने के लिए प्रतिक्रिया में एक टिप्पणी की है । मैंने देखा है कि इस संघर्ष का मुद्दा केवल करने के लिए लगता है के साथ होते हैं public_subnetA और private_subnetA. मैं अभी भी समझ में नहीं आता क्यों हो रहा है ।
संपादित करें 2: मैं करने की कोशिश की निम्नलिखित maafk के सुझाव और कम अपने कोड के लिए निम्न. मैं रखने के लिए चाहते हैं 2 सार्वजनिक और निजी सबनेट विन्यास, लेकिन अब मैं नहीं कर सकते से कनेक्ट करने के लिए मेरे EC2 के उदाहरण कोई फर्क नहीं पड़ता कि मैं क्या प्रयास करें. क्यों कुछ है कि हो सकता है आसानी से हासिल की है में Terraform तो यूँ ही मुश्किल में करने के लिए एडब्ल्यूएस CDK, अकेले जाने में अजगर?
from aws_cdk import core as cdk
from aws_cdk import aws_ec2 as ec2
class HelloCdkStack(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
vpc = ec2.Vpc(self, "vpc-cf",
cidr="10.0.0.0/24",
enable_dns_support=True,
enable_dns_hostnames=True,
nat_gateways=1,
max_azs=1,
subnet_configuration=[ec2.SubnetConfiguration(
name="public-subnetA",
cidr_mask=26,
subnet_type=ec2.SubnetType.PUBLIC
), ec2.SubnetConfiguration(
name="public-subnetB",
cidr_mask=26,
subnet_type=ec2.SubnetType.PUBLIC
), ec2.SubnetConfiguration(
name="private-subnetA",
cidr_mask=26,
subnet_type=ec2.SubnetType.PRIVATE
), ec2.SubnetConfiguration(
name="private-subnetB",
cidr_mask=26,
subnet_type=ec2.SubnetType.PRIVATE)]
)
ec2_instance = ec2.Instance(self, "EC2", instance_type=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO), machine_image=ec2.MachineImage.latest_amazon_linux(
), key_name="KeyPairRandom", vpc=vpc, vpc_subnets=vpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC).subnets[0])
ec2_instance.connections.allow_from_any_ipv4(
ec2.Port.tcp(22), 'Allow inbound SSH from anywhere')