我为什么在我的NAT网关的路由中看到InvalidInstanceID.Malformed?

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

Why am I seeing InvalidInstanceID.Malformed in my NAT Gateway's Route?

问题

以下是您要翻译的内容:

我正在尝试使用AWS CDK(在Javascript中)创建一个堆栈。在我的堆栈中,我想在公共子网中创建一个NAT网关,该子网由我的私有子网中的路由表引用。非常标准的操作。

以下是我用于创建这个基础设施的相关代码片段:

// 创建VPC和子网
const vpc = new ec2.CfnVPC(this, ${applicationName}-vpc, {
cidrBlock: '10.1.0.0/16',
})

const publicSubnet1 = new ec2.PublicSubnet(this, ${applicationName}-public1, {
availabilityZone: availabilityZone1,
cidrBlock: '10.1.0.0/20',
vpcId: vpc.attrVpcId,
mapPublicIpOnLaunch: true
})

// 创建NAT网关
const webserverNatGatewayEIP = new ec2.CfnEIP(this, ${applicationName}-webserver-natgwEIP)

const webserverNatGateway = new ec2.CfnNatGateway(this, ${applicationName}-webserver-natgw, {
subnetId: publicSubnet1.subnetId,
allocationId: webserverNatGatewayEIP.attrAllocationId,
tags: [{
key: 'Name',
value: ${applicationName}-webserver-natgw
}]
})

这很好。但是,当我尝试创建我的路由表和如下所示的路由到NAT网关时:

// 创建私有路由表
const webserverRouteTable = new ec2.CfnRouteTable(this, ${applicationName}-webserver-rtb, {
vpcId: vpc.attrVpcId
})

// 不起作用,需要调试
const webserverRouteTableNatGWRoute = new ec2.CfnRoute(this, 'webserver-rt-natgw-route', {
routeTableId: webserverRouteTable.attrRouteTableId,
destinationCidrBlock: '0.0.0.0/0',
instanceId: webserverNatGateway.attrNatGatewayId
})

当我尝试部署我的堆栈时,我看到以下错误消息:

无效的ID:“nat-0cfe60af88b3df93a”(服务:AmazonEC2;状态代码:400;错误代码:InvalidInstanceID.Malformed;请求
ID:4ecbe914-6ead-4d05-bf8c-d87cd0c72654;代理:null)

我不明白为什么对instanceId的.attrXYZ调用可能会格式错误。我在我的基础设施的其他地方都使用了类似的参数,一直运行得很好。

我手动检查了AWS控制台,以查看NAT网关ID是否不正确,但它完全匹配。我不明白为什么会出现此错误。我猜想也许NAT网关没有及时创建,所以我通过

webserverRouteTableNatGWRoute.addDependency(webserverNatGateway)

但是那并没有解决问题。有什么建议吗?

英文:

I am trying to create a Stack using AWS CDK (in Javascript). In my stack, I would like to create a NAT gateway in a public subnet that is referenced by Route Table in my private subnet. Pretty standard stuff.

Below is a snippet of the relevant code I have to create the infra for this

// CREATE VPC & SUBNET
const vpc = new ec2.CfnVPC(this, `${applicationName}-vpc`, {
  cidrBlock: '10.1.0.0/16',
})

const publicSubnet1 = new ec2.PublicSubnet(this, `${applicationName}-public1`, {
  availabilityZone: availabilityZone1,
  cidrBlock: '10.1.0.0/20',
  vpcId: vpc.attrVpcId,
  mapPublicIpOnLaunch: true
})

// CREATE NAT GATEWAY
const webserverNatGatewayEIP = new ec2.CfnEIP(this, `${applicationName}-webserver-natgwEIP`)

const webserverNatGateway = new ec2.CfnNatGateway(this, `${applicationName}-webserver-natgw`, {
  subnetId: publicSubnet1.subnetId,
  allocationId: webserverNatGatewayEIP.attrAllocationId,
  tags: [{
    key: 'Name',
    value: `${applicationName}-webserver-natgw`
  }]
})

This is fine. However, when I try to create my route table and a route to that NAT gateway as follows:

// CREATE PRIVATE ROUTE TABLE
const webserverRouteTable = new ec2.CfnRouteTable(this, `${applicationName}-webserver-rtb`, {
  vpcId: vpc.attrVpcId
})

// not working, need to debug
const webserverRouteTableNatGWRoute = new ec2.CfnRoute(this, 'webserver-rt-natgw-route', {
  routeTableId: webserverRouteTable.attrRouteTableId,
  destinationCidrBlock: '0.0.0.0/0',
  instanceId: webserverNatGateway.attrNatGatewayId
})

I am seeing the following error message when I try to deploy my stack.

Invalid id: "nat-0cfe60af88b3df93a" (Service: AmazonEC2; Status Code: 400; Error Code: InvalidInstanceID.Malformed; Request
ID: 4ecbe914-6ead-4d05-bf8c-d87cd0c72654; Proxy: null)

I don't understand how the .attrXYZ call for instanceId could be malformed. I am using all over the rest of my infra and it has been working just fine as similar arguments.

I manually checked via the AWS console to see if that NAT Gateway id was incorrect, and it matches perfectly. I don't see why this is failing. I guessed perhaps that maybe the NAT gateway wasn't being created in time, so I added a dependency on it via

webserverRouteTableNatGWRoute.addDependency(webserverNatGateway)

but that didn't fix the error. Any ideas?

答案1

得分: 0

为了直接回答这个问题,原因是 instanceId 属性是用于 EC2 实例 ID 的。要将其指向 NAT 网关,您需要将网关 ID 传递给 natGatewayId 属性。

修改如下:

const webserverRouteTableNatGWRoute = new ec2.CfnRoute(this, 'webserver-rt-natgw-route', {
  routeTableId: webserverRouteTable.attrRouteTableId,
  destinationCidrBlock: '0.0.0.0/0',
  natGatewayId: webserverNatGateway.attrNatGatewayId // 不同的属性名称
})
英文:

To answer the question directly, it is because the instanceId prop is for an EC2 instance ID. To point it to a NAT gateway, you need to pass the gateway ID to the natGatewayId prop instead.

Like so:

const webserverRouteTableNatGWRoute = new ec2.CfnRoute(this, 'webserver-rt-natgw-route', {
  routeTableId: webserverRouteTable.attrRouteTableId,
  destinationCidrBlock: '0.0.0.0/0',
  natGatewayId: webserverNatGateway.attrNatGatewayId // different prop name
})

huangapple
  • 本文由 发表于 2023年5月22日 07:57:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302369.html
匿名

发表评论

匿名网友

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

确定