Mastering Terragrunt for Terraform Management (with examples)

Mastering Terragrunt for Terraform Management (with examples)

Terragrunt is a wrapper that facilitates the management of complex Terraform configurations. It is specifically designed to DRY (Don’t Repeat Yourself) your Terraform CLI arguments. Terragrunt helps in keeping configurations consistent and manageable across multiple environments, modules, and teams, by providing a variety of commands that streamline workflows, enforce policies, and simplify tasks otherwise prone to error. Below, we delve into specific Terragrunt use cases, demonstrating each with practical examples.

Generate and Show an Execution Plan

Code:

terragrunt plan

Motivation: The terragrunt plan command is essential for verifying what actions Terraform will undertake without actually changing the state of your infrastructure. It helps in checking for syntax errors, understanding resource changes, and planning your deployments effectively.

Explanation: The command plan allows you to simulate the effect of the proposed configuration on your infrastructure. By doing this, you can catch potential issues before any actual resources are modified, ensuring efficient allocation and informed decision-making.

Example Output:

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
 
------------------------------------------------------------------------
 
An execution plan has been generated and is shown above.
Resource actions are indicated with the following symbols:
  + create
...

Build or Change Infrastructure

Code:

terragrunt apply

Motivation: The terragrunt apply command is what you use to actually create, update, or modify your infrastructure. This command is crucial when it’s time to implement the configurations defined in your Terraform files.

Explanation: The apply command commits the changes you intended, which were laid out in the plan stage, to the cloud provider’s platform. It picks up all defined resources and parameters, creating or altering them as specified in the Terraform configuration.

Example Output:

...
Terraform will perform the following actions:
  + module.network.aws_vpc.main:
...
Plan: 5 to add, 0 to change, 0 to destroy.
 
Do you want to perform these actions?
...

Show Current Deployment (from State)

Code:

terragrunt show

Motivation: This command is useful if you want to review the current state of your deployment without introducing changes. Whether for compliance checks or audits, terragrunt show provides insights into the existing setup.

Explanation: By utilizing show, you can comprehend the infrastructure that exists in a given workspace. It reads the current state file and presents the existing resources, which is beneficial for monitoring and reviewing the applied configurations.

Example Output:

Terraform state on <date>:
 
Resources:
  aws_vpc.main:
    id = vpc-abc12345
    ...

Show Module Output Values

Code:

terragrunt output

Motivation: The terragrunt output command retrieves and displays the output values from your Terraform configurations. This is particularly useful when you need to programmatically access the outputs from your infrastructure modules.

Explanation: Output variables typically contain information about the infrastructure such as IP addresses, resource IDs, etc., and output ensures these can be easily retrieved and reused in other scripts or modules.

Example Output:

vpc_id = vpc-abc12345
subnet_id = subnet-67890def
...

Destroy Terraform-Managed Infrastructure

Code:

terragrunt destroy

Motivation: When infrastructure is no longer needed or you want to decommission a set of resources, the terragrunt destroy command allows you to safely and efficiently dismantle those resources.

Explanation: Designed to systematically clean up and remove resources, this command helps manage costs and resource availability by removing infrastructure that is no longer required. It prevents rogue resources from incurring unnecessary expenses.

Example Output:

...
Plan: 0 to add, 0 to change, 5 to destroy.

Do you really want to destroy?
...

Build or Change Infrastructure from a Tree of Terragrunt Modules (Stack)

Code:

terragrunt run-all apply

Motivation: For large-scale deployments involving multiple modules or environments, deploying resources manually might be tedious. The terragrunt run-all apply command automates this by applying changes across a tree of modules, facilitating a more structured approach to managing complex infrastructures.

Explanation: The run-all argument iterates over all directories containing Terraform configurations within the specified folder tree. It executes the apply command on each, ensuring that changes are propagated across all levels of your project.

Example Output:

...
Running 'terragrunt apply' recursively
Processing module: module_a
...
Processing module: module_b
...

Conclusion

Terragrunt offers indispensable enhancements to managing Terraform configurations – from planning and applying infrastructure changes to efficiently destroying obsolete resources. By following the described methodologies and examples, users can streamline their workflows, enhance productivity, and effectively ensure DRY principles are applied across their Terraform deployments.

Related Posts

How to use the command 'dhcpwn' (with examples)

How to use the command 'dhcpwn' (with examples)

The dhcpwn is a command-line utility designed for testing DHCP IP exhaustion attacks and sniffing local DHCP traffic.

Read More
How to Use the 'openvpn' Command (with Examples)

How to Use the 'openvpn' Command (with Examples)

OpenVPN is a versatile, open-source software application that implements virtual private network (VPN) techniques for creating secure point-to-point or site-to-site connections.

Read More
How to Use the Command 'fileicon' (with Examples)

How to Use the Command 'fileicon' (with Examples)

Fileicon is a versatile command-line utility designed to manage custom icons for files and folders on macOS.

Read More