0

I am trying to create nat gateway from terraform by using AWS as provider but subnet_id in resource aws_nat_gateway always gives me error. I am trying to assign public subnet in subnet_id on resource "aws_nat_gateway" "sample_nat_gateway" from variables.tf file but failing in doing so and need support if someone can assist ?

Below is my vpc.tf file of vpc module

 resource "aws_subnet" "public-subnet" {
  for_each = var.prefix
 
  availability_zone_id = each.value["az"]
  cidr_block = each.value["cidr"]
  vpc_id     = aws_vpc.sample_vpc.id     
  tags = {
    Name = "${var.name}-${each.value["az"]}"
 }
}
resource "aws_nat_gateway" "sample_nat_gateway" {
  allocation_id = aws_eip.sample_eip.id
  subnet_id      = ""
  tags = {
    Name = "${var.name}-sample-nat-gateway"
    Environment = var.environment
}
  depends_on = [aws_internet_gateway.sample_igw]
}

variables.tf

variable "prefix" {
   type = map
   default = {
      sub-1 = {
         az = "use2-az1"
         cidr = "10.0.1.0/16"
      }
      sub-2 = {
         az = "use2-az2"
         cidr = "10.0.2.0/24"
      }
   }
}
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • │ Error: Missing resource instance key │ │ on ../modules/vpc/vpc.tf line 108, in resource "aws_nat_gateway" "sample_nat_gateway": │ 108: subnet_id = aws_subnet.public-subnet.id │ │ Because aws_subnet.public-subnet has "for_each" set, its attributes must be accessed on specific instances. │ │ For example, to correlate with indices of a referring resource, use: │ aws_subnet.public-subnet[each.key] – Abdulrehman Nov 27 '21 at 09:57

1 Answers1

1

Subent's can't be empty You have to provide valid subnet id where the NAT is going to be placed. For example:

resource "aws_nat_gateway" "sample_nat_gateway" {
  allocation_id = aws_eip.sample_eip.id

  subnet_id      = aws_subnet.public-subnet["sub-1"].id

  tags = {
    Name = "${var.name}-sample-nat-gateway"
    Environment = var.environment
}
  depends_on = [aws_internet_gateway.sample_igw]
}

where aws_subnet.example is one of the public subnets in your VPC.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • thanks it is working fine. Also can you please confirm how can I get these sub-1 and sub-2 values in output variables and use these values in other modules where I created EC2 instances from launch configurations in autsocaling group resource resource "aws_autoscaling_group" "sample_asg_1 vpc_zone_identifier = [var.subnet_id] – Abdulrehman Nov 27 '21 at 10:24
  • @Abdulrehman If my answer helped, its [acceptance](https://meta.stackexchange.com/a/86979) would be appreciated. For your new issue, I would suggest making new question with relevant details. – Marcin Nov 27 '21 at 10:26
  • yes it is correct thanks. I have voted as well. Please let me know if anything else require from my side – Abdulrehman Nov 27 '21 at 10:30
  • @Abdulrehman Thanks. To accept the answer you have to press a tick-like button as shown here: https://meta.stackexchange.com/a/86979 – Marcin Nov 27 '21 at 10:31
  • 1
    completed thanks for your feedback – Abdulrehman Nov 27 '21 at 10:45