ปัญหา Deploying VPC กับ Subnets กับ CDK และเป็นภาษาไพธอน

0

คำถาม

ฉันกำลังพยายามสร้าง VPC กับ subnets สำหรับโครงการแต่ฉันวิ่งเข้าหาปัญหาที่ CDK ดูเหมือนจะทำให้สองคน subnets กับเดียวกัน CIDR บล็อกเมื่ลังสังเคราะห์รหัสเข้าไปใน CloudFormation แบบ,แม้ว่าฉันเพียงการใช้ CIDR บล็อคครั้งต่อ subnet ปล่อยให้เธอได้หายใจอยู่อีก นี่ทำให้ deployment ล้มเหลวตั้งแต่ CIDR ช่วงตึกความขัดแย้งกับคนอื่นตอนที่กำลังสร้างค subnets. นี่คือรหัสสำหรับ defining งงาน:

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)

ฉันไม่รู้ว่าจะแก้ไขเรื่องนี้ปัญหาตั้งแต่เห็นได้ชัดว่ามันควรเท่านั้นใช้พวกนั้น CIDR ช่วงตึกครั้งหนึ่ง แต่มันไม่ช่วยเลย

แก้ไข:ฉันตอนนี้รวมถึงกา รเชื่อมโยง ไปที่ CloudFormation ต้นแบบแสดงผลในการตอบสนอต้องเป็นความเห็นเลิกประชุม. ฉันคงสังเกตุเห็นว่าความขัดแย้งเรื่องเดียวที่ดูเหมือนจะเกิดขึ้นกับ public_subnetA และ private_subnetA. ฉันยังไม่เข้าใจว่าทำไมเรื่องนี้กำลังเกิดขึ้น

แก้ไขที่ 2:ฉันพยายามตาม maafk เป็นข้อแนะนำและลดของฉันรหัสที่รับข้อความอะไรเลย ฉันยังต้องการที่จะรักษาที่ 2 สาธารณะและเป็นส่วนตัว subnet การปรับแต่งแต่ตอนนี้ฉันไม่สามารถเชื่อมต่อไปของฉัน EC2 ย่างเช่นไม่ว่าอะไรฉันพยายาม ทำไมถึงเป็นบางอย่างที่สามารถเป็นอย่างง่ายดายประสบความสำเร็จใน Terraform ดังนั้น needlessly ยากที่จะทำใน AWS 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')
1

คำตอบที่ดีที่สุด

1

คุณเพิ่มมากขึ้นรหัสว่านจะมีความจำเป็นอย่างมาก

สร้าง VPC กับ CDK แล้วเอาดูแลของส่วนใหญ่ของมันเพื่อคุณ

จาก docs

เป็นค่าปริยาย VPC การปรับแต่งค่าจะต้องสร้างสาธารณะและเป็นส่วนตัว subnets

นี่มันเพิ่งเป็น"CDK เป็นชนพื้นเมืองที่ทาง"เพื่อเขียนเรื่องนี้

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)

        vpc = ec2.Vpc(self, "vpc-cf",
            cidr="10.0.0.0/24",
            # dns hostnames and support enabled by default
        )

        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", 
            vpc=vpc,
            # PRIVATE subnets are chosen by default
        )
        # Not a great security practice to open ssh to the world, but can do it this way
        ec2_instance.connections.allow_from_any_ipv4(ec2.Port.tcp(22), 'Allow inbound SSH from anywhere')

นอกจากรหัส,และใช้คำว่า"ไร้ที่ติสำหรับค่าปริยาย"นั่นมันมากับ CDK

2021-11-20 12:31:11

ฉันจะทำให้มันเลือกเป็นสาธารณะ subnet. ฉันคิดว่าคนส่วนตัว subnets ค่าปริยายคืออกไปฉันไม่สามารถเชื่อมต่อไปของฉันตัวอย่างผ่านทางบริการ ssh.
Karma Cool

ตรวจสอบคอบเอกสารที่ Vpc สร้าง.
gshpychka

ทางเลือกที่ดีที่สุดสำหรับบริการ ssh คือต้องใช้ SSM การจัดการวาระงาน(ถูกติดตั้งโดยปริยายบนอเมซอนระบบลินุกซ์),หรือต้องการใช้ bastion เครื่องในที่สาธารณะ subnet. ถ้าคุณอย่างแน่นอ นต้อง สร้างของคุณตัวอย่างในที่สาธารณะ subnet คุณสามารถเพิ่ม vpc_subnets=vpc.SubnetSelection(subnet_type=ec2.SubnetType.PUBLIC) ไปที่ ec2.Instance พารามิเตอร์
maafk

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่