簡體   English   中英

如何在 CDK 中創建 NAT 網關,然后將路由添加到指向 CIDR 的私有 su.net?

[英]How to create a NAT Gateway in CDK and then add route to a private subnet pointing CIDR to it?

我找到了一些關於將 NAT 實例設置為私有 su.net 的例子。 我不想讓 AWS 在每個 AZ 中創建 NAT 網關,因為我不會擁有多 AZ。

我可能誤解了你的問題。 我按照以下(Python)的方式進行工作。 使用ec2.Vpc.from_lookup獲取 vpc。

allocation_id = 'eipalloc-xxx1'

nat_gateway = ec2.CfnNatGateway(
    self,
    'My-Nat-Gateway',
    allocation_id = allocation_id,
    subnet_id = 'subnet-1234' # the ID of the first default subnet in the VPC, in my case it was ok not to do it for all subnets
)

ip_range_index_offset = 3

for i, az in enumerate(vpc.availability_zones):
    sub_net = ec2.PrivateSubnet(
        self,
        id = 'private-subnet-' + str(i),
        availability_zone = az,
        cidr_block = '123.12.'+ str(16 * (i+ip_range_index_offset)) +'.0/20', # there is likely a better way to do this
        vpc_id = vpc.vpc_id,
    )

    route_table_entry = ec2.CfnRoute(
        self,
        id = 'route-table-entry' + str(i),
        route_table_id  = sub_net.route_table.route_table_id,
        destination_cidr_block = '0.0.0.0/0',
        nat_gateway_id = nat_gateway.ref

    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM