Terragrunt: Enhancing the Terraform Experience

Terragrunt: Enhancing the Terraform Experience

Introduction

An Infrastructure as Code solution such as?Terraform?allows you to create and manage cloud resources using configuration files.

Terraform can be particularly complex to manage when it is necessary to maintain multiple environments while minimizing code reuse (DRY: don’t repeat yourself), or to manage multiple?distinct backends?for each environment.

So,?Terragrunt?solves these problems.

Introduction to Terragrunt

Terragrunt is a thin wrapper for Terraform that provides extra tools for keeping your Terraform configurations DRY (Don't Repeat Yourself). With Terragrunt, you can easily manage remote states and multiple environments. It also helps you keep your codebase clean and organized.

File Structure Using Terragrunt

Terragrunt simplifies the management of Terraform configurations for multiple environments. As we can see here.

Terraform VS Terragrunt

On the left side of the image, we can see that "classic" Terraform code must be maintained for each environment. We have to copy the code three times for the app modules, which is not ideal for maintenance.

with Terragrunt, we Separate our Terraform code into a folder from Terragrunt code, which only contains environment specific variables.

How to Manage modules using terragrunt

let's focus on terragrunt.hcl on every stage let's see how will be the structure of the file!

In these files,

  1. we will declare the path where the terraform module is located
  2. we will pass the necessary variables to our modules so that they are called. To do this, simply use an inputs block.

at the end our file will be looks like:

#/home/rana/demo-terragrunt/terragrunt/dev/app/terragrunt.hcl
# Include root terragrunt.hcl
include "root" {
  path = find_in_parent_folders()
}


# Terraform block to call app module
terraform {
    source = "../../../terraform//app/"
}

# Values
inputs = {
  variable1 = "first value"
  variable2 = "second value"
}        

Once our code is complete, all we have to do is generate the terragrunt plan and provision the resources.

# move to the directory
cd /home/rana/demo-terragrunt/terragrunt/dev/app

# Terragrunt plan to see the changes 
terragrunt plan 

# Apply the confg
terragrunt apply         



Fouad Waly, (SAFe Agilist?)

Agile Practitioner at _VOIS | SAFe 6 RTE |SAFe 6 Agilist | CSM? | SFC

1 年

Well done ya Rana ??????

要查看或添加评论,请登录

Rana Nagy的更多文章