Troubleshooting a Terraform for_each Error

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

Hey there, Gurus! Welcome to the lab! This lab will demonstrate how to troubleshoot a Terraform `for_each` error. We will attempt to go through the different stages of deployment to try and deploy our configuration and work through the `for_each` error until we have successfully deployed our resources.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Validate Your Code
  1. In the terminal, once logged into your instance, change to the terraform directory.
  2. In the terraform directory, edit the main.tf file and add in your ami ,subnet_id, and vpc from the resource_ids.txt file.
  3. Run the terraform fmt command.
  4. Now initialize your working directory.
  5. If initialization is successful, then validate your code.
Fix the for_each Error
  1. After running the terraform validate command, you should get an error. Read the error and go to each line in the main.tf file where the error is.
  2. Fix each for_each error that you get.
  3. Check the formatting, then validate your code again. If the error is fixed, move on to the next step in your deployment.
Deploy Your Resources
  1. Once you have worked past the error, successfully validated your code, and run the plan command, then deploy your resources.
  2. When you have successfully deployed your infrastructure, the lab is complete.

Additional Resources

You are an admin who supports one of the largest online comic book retailers in the world. You are one of a handful of admins who have been assigned to use Terraform to manage the company's cloud deployments. You are being asked to start to create a new dev environment to test out new features of the company's site. You have your configuration created, which will create the app servers for the site. You are getting ready to use the code to deploy your resources, but you have run into a for_each error. Fix the error and successfully deploy your app server in AWS using Terraform to complete this lab.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?