hands-on lab

Creating Loops in Terraform and Scaling Resources

Intermediate
Up to 40m
2,640
4.6/5
Get guided in a real environmentPractice with a step-by-step scenario in a real, provisioned environment.
Learn and validateUse validations to check your solutions every step of the way.
See resultsTrack your knowledge and monitor your progress.

Description

Using loops in Terraform can be very powerful. It can take messy spaghetti code and turn it into something clean, simple, and easily understandable. It can make infrastructure scalable and efficient with just a few lines of code. The HCL language is both human-readable and machine-friendly, where components and lists can be iterated to create looping logic. Loops are commonly used in the Terraform community to make modules dynamic. For example, an EC2 Instance module can use loops to deploy multiple disks if needed or contain multiple IP addresses.

In this lab, you will create an EC2 Module that contains loops using various methods in Terraform.

Learning objectives

Upon completion of this lab, you will be able to:

  • Understand how Terraform code can be made dynamic for reusability using loops
  • Learn about count and for_each loops

Intended audience

  • Individuals studying to take the HashiCorp Certified: Terraform Associate exam
  • Anyone interested in learning how to use Terraform to manage Cloud Service Providers

Prerequisites

You should be familiar with:

  • Cloud Services
  • DevOps
  • Basic understanding of Terraform modules and variables

The following course and lab can be used to fulfill the prerequisites:

Updates

January 4th, 2024 - Lab updated to utilize CodeTour IDE

March 16th, 2023 - Resolved IDE URL issue

Environment before

Environment after

About the author

Students
19,921
Labs
42
Courses
1
Learning paths
15

Luke is a Site Reliability Engineer at Microsoft. His background is infrastructure development using Terraform and in 2021 he was awarded the HashiCorp Ambassador award. He is an Azure DevOps Engineer Expert, Azure Administrator Associate, and HashiCorp Certified - Terraform Associate.

Covered topics

Lab steps

Creating Loops in the Terraform Configuration and Scaling Resources