私有子网中未使用 AWS CDK 添加路由。

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

Routes are not added in Private Subnets using AWS CDK

问题

我正在尝试将私有子网的路由指向我已创建的NAT实例。
VPC已经创建。

在执行cdk deploy后,私有子网没有我添加的路由。
我认为这可能是由于在natInstanceeIPAssociation尚未创建时存在依赖性问题。所以我甚至添加了依赖关系,但似乎从未添加过。

如何在AWS CDK中成功添加路由到私有子网?

  1. // NAT instance
  2. const natInstance = new ec2.Instance(this, "nat-instance", {
  3. vpc,
  4. vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
  5. instanceType: new ec2.InstanceType("t3.micro"),
  6. machineImage: new ec2.NatInstanceImage(),
  7. securityGroup: natSG,
  8. // Helps SSH into the instance
  9. // You need to create a key pair in AWS console and download the .pem file in named seungho-key-pair.pem
  10. keyName: "seungho-key-pair",
  11. // Need this for NAT instance
  12. sourceDestCheck: false,
  13. });
  14. // Attach Elastic IP to NAT instance
  15. const eIPAssociation = new ec2.CfnEIPAssociation(
  16. this,
  17. "Nat EIP Association",
  18. {
  19. instanceId: natInstance.instanceId,
  20. eip: new ec2.CfnEIP(this, "Nat EIP", {}).ref,
  21. }
  22. );
  23. // Update Private Subnet Route Table
  24. vpc.privateSubnets.forEach((subnet, index) => {
  25. const route = new ec2.CfnRoute(this, `NAT ROUTE${index + 1}`, {
  26. routeTableId: subnet.routeTable.routeTableId,
  27. destinationCidrBlock: "0.0.0.0/0",
  28. instanceId: natInstance.instanceId,
  29. });
  30. // Make sure NAT instance is created before creating route
  31. route.addDependsOn(natInstance.instance);
  32. route.addDependsOn(eIPAssociation);
  33. });
英文:

I am trying to point Private Subnets' Route to the NAT instance I have.
The VPC is already created.

After doing cdk deploy, the private subnets don't have the route that I added.
I thought it could be due to the dependency issue when natInstance or eIPAssociation is not yet created. So I even added dependency but it never seems to be added.

How can I add the route successfully to the private subnets in AWS CDK?

  1. // NAT instance
  2. const natInstance = new ec2.Instance(this, "nat-instance", {
  3. vpc,
  4. vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
  5. instanceType: new ec2.InstanceType("t3.micro"),
  6. machineImage: new ec2.NatInstanceImage(),
  7. securityGroup: natSG,
  8. // Helps SSH into the instance
  9. // You need to create a key pair in AWS console and download the .pem file in named seungho-key-pair.pem
  10. keyName: "seungho-key-pair",
  11. // Need this for NAT instance
  12. sourceDestCheck: false,
  13. });
  14. // Attach Elastic IP to NAT instance
  15. const eIPAssociation = new ec2.CfnEIPAssociation(
  16. this,
  17. "Nat EIP Association",
  18. {
  19. instanceId: natInstance.instanceId,
  20. eip: new ec2.CfnEIP(this, "Nat EIP", {}).ref,
  21. }
  22. );
  23. // Update Private Subnet Route Table
  24. vpc.privateSubnets.forEach((subnet, index) => {
  25. const route = new ec2.CfnRoute(this, `NAT ROUTE${index + 1}`, {
  26. routeTableId: subnet.routeTable.routeTableId,
  27. destinationCidrBlock: "0.0.0.0/0",
  28. instanceId: natInstance.instanceId,
  29. });
  30. // Make sure NAT instance is created before creating route
  31. route.addDependsOn(natInstance.instance);
  32. route.addDependsOn(eIPAssociation);
  33. });

答案1

得分: 1

根据评论,未创建路由的原因是因为您正在对一个空列表进行迭代 - 您的VPC没有私有子网。

私有子网是具有指向NAT网关的路由的子网。

没有出口的子网被称为隔离子网,您应该对isolatedSubnets属性进行迭代。

英文:

As per the comments, the reason no routes were created is because you are iterating on an empty list - your VPC does not have Private subnets.

Private subnets are those that have a route to a NAT gateway.

Subnets without egress are called Isolated and you should be iterating over the isolatedSubnets prop instead.

huangapple
  • 本文由 发表于 2023年7月20日 10:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726370.html
匿名

发表评论

匿名网友

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

确定