hands-on lab

Optimizing Terraform Configurations with Loops in GCP

Difficulty: Intermediate
Duration: Up to 45 minutes
Students: 260
Rating: 5/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

The D.R.Y. principle, "Don't Repeat Yourself", is generally followed by developers in the Terraform community. Terraform offers iteration options such as for loops to allow developers to define resources or settings once, and have those configurations multiplied and managed as a group. This results in less code for a team to manage in the long run.

Terraform has three main iteration options: the count and for_each arguments, and for loops. In this lab, you will implement each one of these options and learn how to choose the right option to meet your needs.

Learning Objectives

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

  • Iterate through lists and sets with for loops, for_each, and the count argument
  • Identify the limitations of each iteration option and how to choose between them

Intended Audience

  • Individuals looking to pass the HashiCorp Certified: Terraform Associate Exam
  • Individuals and teams seeking an introduction to using Terraform with Google Cloud Platform

Prerequisites

Familiarity with the following will be beneficial but is not required:

  • Basic concepts of Google Cloud Platform
  • Terraform Variables

The following lab can be used to fulfill the prerequisites:

Updates

June 9th, 2023 - Updated instance image

April 27th, 2023 - Updated theia to enable autosave

Environment before

Environment after

Covered topics

Lab steps

Activating the Google Cloud Platform Service Account
Multiplying Resource Configurations with Count and For Loops in Terraform
Iterating Through a Set with For Each in Terraform