我有一个私有子网,默认路由指向 nat 网关。两者都是由 terraform 创建的。
现在我有另一个代码来提高 ec2 以在我的 VPC 中用作 NAT(因为 cloud-nat-gateway 变得非常昂贵)。我正在尝试将我的 rtb 中的默认路由更改为这个新的 ec2 并收到以下错误:
错误:应用计划时出错:
发生 1 个错误:* module.ec2-nat.aws_route.defaultroute_to_ec2-nat:发生 1 个错误:* aws_route.defaultroute_to_ec2-nat:创建路由时出错:RouteAlreadyExists:由 0.0.0.0/0 标识的路由已存在。状态码:400,请求 ID:408deb59-d223-4c9f-9a28-209e2e0478e9
我知道这条路线已经存在,但是如何将这条已经存在的路线更改为新目标?在这种情况下,我的新 ec2 网络接口?
谢谢你的帮助。
以下是我正在使用的代码:
#####################
# FIRST TERRAFORM
# create the internet gateway
resource "aws_internet_gateway" "this" {
count = "${var.create_vpc && length(var.public_subnets) > 0 ? 1 : 0}"
vpc_id = "${aws_vpc.this.id}"
tags = "${merge(map("Name", format("%s", var.name)), var.igw_tags, var.tags)}"
}
# Add default route (0.0.0.0/0) to internet gateway
resource "aws_route" "public_internet_gateway" {
count = "${var.create_vpc && length(var.public_subnets) > 0 ? 1 : 0}"
route_table_id = "${aws_route_table.public.id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.this.id}"
timeouts {
create = "5m"
}
}
#####################
# SECOND TERRAFORM
# Spin EC2 to run as NAT
resource "aws_instance" "ec2-nat" {
count = "${var.instance_qtd}"
ami = "${data.aws_ami.nat.id}"
availability_zone = "${var.region}a"
instance_type = "${var.instance_type}"
key_name = "${var.aws_key_name}"
vpc_security_group_ids = ["${var.sg_ec2}","${var.sg_ops}"]
subnet_id = "${var.public_subnet_id}"
iam_instance_profile = "${var.iam_instance_profile}"
associate_public_ip_address = true
source_dest_check = false
tags = {
Name = "ec2-nat-${var.brand}-${var.role}-${count.index}"
Brand = "${var.brand}"
Role = "${var.role}"
Type = "ec2-nat"
}
}
# Add default route (0.0.0.0/0) to aws_instance.ec2-nat
variable "default_route" {
default = "0.0.0.0/0"
}
resource "aws_route" "defaultroute_to_ec2-nat" {
route_table_id = "${var.private_route_id}"
destination_cidr_block = "${var.default_route}"
instance_id = "${element(aws_instance.ec2-nat.*.id, 0)}"
}