Setting Up Terragrunt and Creating a Basic Configuration File

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

In this lab, you will learn how to use Terragrunt to launch two EC2 instances in different AWS AZs with a single template. You will use a simple folder structure that includes a module folder with a `main.tf`, `variables.tf`, and `outputs.tf` file, as well as a `terragrunt.hcl` file. By the end of the lab, you will have a better understanding of how Terragrunt can be used to easily manage infrastructure in AWS.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the Directory Structure for the Terragrunt Project
  • Check that Terraform is installed and functioning properly.
  • Check that Terragrunt is installed and functioning properly.
  • Create a new directory to house your Terraform code called ec2-instances.
  • In the ec2-instances directory, create a directory called module.
  • Also create a terragrunt.hcl file in the same ec2-instances directory.
Write Your Terragrunt Module Code
  • In the module directory, create a new file called main.tf and add the provided code.
  • Create a new file called variables.tf and add the provided code.
  • Create a new file called outputs.tf and add the provided code.
Write Your terragrunt.hcl File Code

In the intro_terragrunt directory, create a new file called terragrunt.hcl and add the provided code.

Deploy Your Code and Verify Resources Were Launched in AWS
  • Initialize the Terragrunt configuration to fetch any required providers and get the code being referenced in the module block.
  • Validate the code.
  • Review the actions that will be performed when you deploy the code.
  • Deploy the code.
  • View the resources that were created.
  • Tear down the infrastructure.

Additional Resources

You are a DevOps engineer at a software company that has recently migrated its infrastructure to AWS. As a part of your job, you are responsible for automating the infrastructure deployment process using Terragrunt. Your manager has assigned you a task to set up two EC2 instances in different Availability Zones in order to host a new application that is being tested.


In the files you create in the module directory, you will need to insert the following provided code.

In the main.tf file:

provider "aws" {
  region = "us-east-1"
}

data "aws_vpc" "vpc" {
}

data "aws_subnet" "zone1" {
  filter {
    name = "tag:Name"
    values = ["SubnetA"]
  }
}

data "aws_subnet" "zone2" {
  filter {
    name = "tag:Name"
    values = ["SubnetB"]
  }
}

resource "aws_instance" "ec2_instance_1" {
  subnet_id     = data.aws_subnet.zone1.id
  ami           = var.ami
  instance_type = var.instance_type
  availability_zone = var.az1
}

resource "aws_instance" "ec2_instance_2" {
  subnet_id     = data.aws_subnet.zone2.id
  ami           = var.ami
  instance_type = var.instance_type
  availability_zone = var.az2
}

In the variables.tf file:

variable "instance_type" {
  type = string
}

variable "ami" {
  type = string
}

variable "az1" {
  type = string
}

variable "az2" {
  type = string
}

In the outputs.tf file:

output "public_ip_1" {
  value = aws_instance.ec2_instance_1.public_ip
}

output "public_ip_2" {
  value = aws_instance.ec2_instance_2.public_ip
}

In the files you create in the intro_terragrunt directory, you will need to insert the following provided code.

In the terragrunt.hcl file:

terraform {
  source = "./module///"
}

inputs = {
  instance_type = "t3.micro"
  ami = "ami-06e46074ae430fba6"
  az1 = "us-east-1a"
  az2 = "us-east-1b"
}

To get started, log in to the lab server using the credentials provided:

ssh cloud_user@<Terraform-Controller>

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?