LinkedInLearning / learning-terraform-3087701

This repo is for the Linkedin Learning course: Learning Terraform
Other
97 stars 5.16k forks source link

Code 04_01 error in function call: aws_route_table.private is empty tuple #80

Open atticus-trellix opened 1 year ago

atticus-trellix commented 1 year ago

When running the code solution in branch 04_01, terraform returns errors:

Error: Error in function call
on .terraform/modules/blog_vpc/main.tf line 1087, in resource "aws_route" "private_nat_gateway":
  route_table_id         = element(aws_route_table.private[*].id, count.index)
while calling element(list, index)
aws_route_table.private is empty tuple
count.index is [NB: terraform returns 3 errors, all with the same trace except here, where count.index ranges between 0 and 2.] 
Call to function "element" failed: cannot use element function with an empty list.
patrickodacre commented 1 year ago

The code in the video has the following for the VPC module : enable_nat_gateway = true. To understand the error you need to understand why we would want a NAT gateway.

NAT gateways are used when you want EC2 instances in a private subnet to be able to connect to the internet for things like Linux package updates, but you do not want devices outside the private subnet to be able to connect to the instances (like with ssh). The NAT gateway is on a public subnet in your VPC, and an entry for the IP of that NAT gateway is added to the route table of the private subnet.

The Problem :: The configuration in the video asks for a NAT gateway, but there aren't any private subnets with a route table to update with the IP of that NAT gateway. The error is saying that you have an empty list of private subnets. Put another way, the configuration is incomplete.

You can fix the error by removing the enable_nat_gateway = true setting.