Using Terragrunt to Reduce Terraform Code Redundancy and Accelerate Infrastructure Deployment
This article explains how Terragrunt, a wrapper for Terraform, can eliminate repetitive Terraform code, streamline provider, backend, and module configurations, and simplify multi‑environment deployments on Alibaba Cloud with practical code examples and step‑by‑step commands.
In this tutorial we explore Terragrunt, an open‑source command‑line wrapper for Terraform that helps reduce code duplication and manage remote state, making large‑scale infrastructure projects easier to maintain.
Terraform Issues
Repeated versions.tf code
Duplicated module imports
Backend configuration duplication
Provider configuration duplication
Terragrunt Overview
Terragrunt, maintained by Gruntwork, acts as a wrapper around Terraform, allowing remote state management and module orchestration while keeping Terraform logic separate from its implementation. By replacing *.tf files with terragrunt.hcl files, inputs can be passed to modules from any environment.
Using Terragrunt to Optimize Terraform Code
We first create a module named module-alicloud-dns in the repository and reference it from various environments.
provider "alicloud" {
region = "cn-beijing"
}
## oss
resource "alicloud_oss_bucket" "tfbucket" {
bucket = "tf-backend-datas"
acl = "private"
}
## tablestore
resource "alicloud_ots_instance" "tftable" {
name = "tftablenew"
description = "terraform tablestore"
accessed_by = "Any"
tags = {
Created = "TF"
For = "Building table"
}
}
resource "alicloud_ots_table" "basic" {
instance_name = alicloud_ots_instance.tftable.name
table_name = "myterraformtable"
primary_key {
name = "LockID"
type = "String"
}
time_to_live = -1
max_version = 1
deviation_cell_version_in_sec = 1
}Deploy the backend using Terraform commands:
terraform init
terraform plan
terraform apply --auto-approveConvert the local state to OSS by adding backend.tf and re‑initialising:
terraform {
backend "oss" {
bucket = "tf-backend-datas"
prefix = "global/backend"
key = "terraform-global-backend.tfstate"
region = "cn-beijing"
tablestore_endpoint = "https://tftablenew.cn-beijing.ots.aliyuncs.com"
tablestore_table = "myterraformtable"
}
}Deploy Dev Environment
Enter the dev directory, verify that backend.tf matches the backend configuration, then run:
terraform init
terraform plan
terraform apply --auto-approveVerification is performed in the Alibaba Cloud console.
Terragrunt Configuration Files
Root terragrunt.hcl defines provider, Terraform version, module source, and remote state:
// define provider
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <Environment‑Specific Inheritance
Dev environment terragrunt.hcl inherits the root configuration and supplies inputs:
include "root" {
path = find_in_parent_folders()
}
inputs = {
dns_zone_name = "zeyang.site"
dns_record = "tgdev"
eip = "123.123.123.123"
record_type = "A"
}Run:
terragrunt init
terragrunt plan
terragrunt applySimilar stg configuration inherits the root and provides its own inputs.
Summary
Terragrunt helps reduce Terraform code redundancy by centralising provider, backend, version, and module definitions, enabling clean multi‑environment deployments and simplifying infrastructure‑as‑code management on Alibaba Cloud.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.