简体   繁体   English

将每个 EIP 附加到 Terraform 中的每个 Nat Gatway

[英]Attach each EIP to each Nat Gatway in Terraform

I'm creating two public subnets that will each contain a nat gateay.我正在创建两个公共子网,每个子网都包含一个 nat gateay。 My code, attempts to create these nats per subnet, and then allocate the eip to each.我的代码尝试为每个子网创建这些 nat,然后将 eip 分配给每个子网。 However, since my for each starts the code block, it looks like the allocation id became us-east-* instead of the id of the eip.但是,由于我的 for each 启动了代码块,看起来分配 id 变成了 us-east-* 而不是 eip 的 id。

Variables.tf:变量.tf:

    variable "public_subnet_numbers" {
  type = map(number)
 
  description = "Map of AZ to a number that should be used for public subnets"
 
  default = {
    "us-east-1a" = 1
    "us-east-1b" = 2
    #"us-east-1c" = 3
  }
}
 
variable "private_subnet_numbers" {
  type = map(number)
 
  description = "Map of AZ to a number that should be used for private subnets"
 
  default = {
    "us-east-1a" = 4
    "us-east-1b" = 5
    #"us-east-1c" = 6
  }
}
 
variable "vpc_cidr" {
  type        = string
  description = "The IP range to use for the VPC"
  default     = "192.168.0.0/16"
}

Main.tf:主文件:

    resource "aws_eip" "nat" {
  count = 2
  vpc = true
 
  lifecycle {
    # prevent_destroy = true
  }
 
  tags = {
    Name        = "cf-${var.infra_env}-eip"
    Project     = "cf.io"
    Environment = var.infra_env
    VPC         = aws_vpc.vpc.id
    ManagedBy   = "terraform"
    Role        = "private"
  }
}
 

resource "aws_nat_gateway" "ngw" {
  for_each = var.private_subnet_numbers
  subnet_id = each.value.id #aws_subnet.public[each.key].id
  allocation_id = aws_eip.nat[each.key].id
 
 
  tags = {
    Name        = "cf-${var.infra_env}-ngw"
    Project     = "cf.io"
    VPC         = aws_vpc.vpc.id
    Environment = var.infra_env
    ManagedBy   = "terraform"
    Role        = "private"
  }
}

Error:错误:

    Error: Invalid index
│ 
│   on ../terraform/modules/networking/gateways.tf line 42, in resource "aws_nat_gateway" "ngw":
│   42:   allocation_id = aws_eip.nat[each.key].id
│     ├────────────────
│     │ aws_eip.nat is tuple with 2 elements
│     │ each.key is "us-east-1a"
│ 
│ The given key does not identify an element in this collection value: a number is required.
╵
╷
│ Error: Invalid index
│ 
│   on ../terraform/modules/networking/gateways.tf line 42, in resource "aws_nat_gateway" "ngw":
│   42:   allocation_id = aws_eip.nat[each.key].id
│     ├────────────────
│     │ aws_eip.nat is tuple with 2 elements
│     │ each.key is "us-east-1b"
│ 
│ The given key does not identify an element in this collection value: a number is required.

You're mixing count and for_each .您正在混合countfor_each The easiest way to solve this would be to use for_each in your EIP creation as well, which makes sense because you are creating an EIP for each NAT.解决这个问题的最简单方法是在您的 EIP 创建中也使用for_each ,这是有道理的,因为您正在为每个 NAT 创建一个 EIP。 That would also make your code work better if you decided to add another subnet later, you wouldn't need to go in and change the count from 2 to 3 .如果您决定稍后添加另一个子网,这也将使您的代码更好地工作,您无需进入并将count2更改为3

Otherwise, you need to use the index function to convert the each value to an index number.否则,您需要使用index 函数each值转换为索引号。

As Mark B mentioned mixing the count and for_each is not recommended.正如 Mark B 提到的那样,不建议将countfor_each混合使用。 In your current setup using exclusively for_each is the way to go based on the private_subnet_numbers variable.在您当前的设置中,仅使用for_each是基于private_subnet_numbers变量的方法。

In your aws_eip.nat resource change count to for_each在您的aws_eip.nat资源更改countfor_each

resource "aws_eip" "nat" {
  for_each = var.private_subnet_numbers
  vpc = true
}

Next in your resource aws_nat_gateway.ngw you should refer to subnet ids using each接下来在您的资源aws_nat_gateway.ngw中,您应该使用each引用subnet ids

resource "aws_nat_gateway" "ngw" {
  for_each      = var.private_subnet_numbers
  subnet_id     = aws_subnet.public[each.key].id
  ....
}

And the code as a whole for clarity为了清楚起见,整个代码

resource "aws_vpc" "vpc" {
 ... vpc configurations ...
}

resource "aws_subnet" "public" {
  for_each = var.private_subnet_numbers
  vpc_id   = aws_vpc.vpc.id
 ... subnet configurations ...
}

resource "aws_eip" "nat" {
  for_each = var.private_subnet_numbers
  vpc      = true

  lifecycle {
    # prevent_destroy = true
  }

  tags = {
    Name        = "cf-${var.infra_env}-eip"
    Project     = "cf.io"
    Environment = var.infra_env
    VPC         = aws_vpc.vpc.id
    ManagedBy   = "terraform"
    Role        = "private"
  }
}

resource "aws_nat_gateway" "ngw" {
  for_each      = var.private_subnet_numbers
  subnet_id     = aws_subnet.public[each.key].id
  allocation_id = aws_eip.nat[each.key].id


  tags = {
    Name        = "cf-${var.infra_env}-ngw"
    Project     = "cf.io"
    VPC         = aws_vpc.vpc.id
    Environment = var.infra_env
    ManagedBy   = "terraform"
    Role        = "private"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM