Terragrunt Command Examples (with examples)
In this article, we will explore different use cases of the terragrunt
command and provide code examples for each use case. We will also discuss the motivation behind using each command, explain the arguments used, and provide an example output for better understanding.
1: Generating and Showing an Execution Plan
The terragrunt plan
command is used to generate and show an execution plan for Terraform. It shows what actions Terraform plans to take when creating, modifying, or deleting resources.
Code
terragrunt plan
Motivation You would use this command to review the planned changes before applying them to your infrastructure. It helps in understanding the impact of the changes and identifying any potential issues.
Explanation
The plan
command does not require any arguments. It reads the Terraform configuration files and state files to determine the actions that will be performed.
Example Output
The example output of the terragrunt plan
command will show a detailed overview of the planned changes. It will display the resources that will be created, modified, or destroyed.
2: Building or Changing Infrastructure
The terragrunt apply
command is used to build or change infrastructure based on Terraform configurations.
Code
terragrunt apply
Motivation
Use the apply
command to create or modify resources specified in your Terraform configuration. It automates the creation and modification process, making it easy to manage your infrastructure.
Explanation
The apply
command doesn’t require any arguments. It reads the Terraform configuration files and applies the planned changes to the infrastructure.
Example Output
The example output of the terragrunt apply
command will display the list of resources being created or modified. It will also show the execution progress and any error or success messages.
3: Showing Current Deployment (from State)
The terragrunt show
command is used to display the current deployment state for the Terraform-managed infrastructure.
Code
terragrunt show
Motivation
You can use the show
command to check the current state of your Terraform-managed infrastructure. It helps in understanding the existing resources and their configurations.
Explanation
The show
command doesn’t require any arguments. It reads the Terraform state file and displays the current deployed resources.
Example Output
The example output of the terragrunt show
command will show the details of the deployed resources. It includes information like resource type, name, attributes, and configurations.
4: Showing Module Output Values
The terragrunt output
command is used to display the output values of a Terraform module.
Code
terragrunt output
Motivation
Using the output
command, you can retrieve the output values of a Terraform module. These output values can be used in other modules or scripts, providing a way to pass information between different parts of your infrastructure.
Explanation
The output
command doesn’t require any arguments. It reads the Terraform module’s output values from the state file and displays them.
Example Output
The example output of the terragrunt output
command will show the name and value of each output variable defined in the module. It can be useful for retrieving information like instance IPs, security group IDs, or load balancer endpoints.
5: Destroying Terraform-Managed Infrastructure
The terragrunt destroy
command is used to destroy the Terraform-managed infrastructure.
Code
terragrunt destroy
Motivation
You can use the destroy
command to remove all the resources managed by Terraform, effectively destroying your infrastructure. This is useful when you want to clean up the environment or tear down a development or test infrastructure.
Explanation
The destroy
command doesn’t require any arguments. It reads the Terraform configuration files and destroys the resources defined in them.
Example Output
The example output of the terragrunt destroy
command will display the list of resources being destroyed. It will also show the progress of the destruction process and any error or success messages.
6: Building or Changing Infrastructure from a Tree of Terragrunt Modules (Stack)
The terragrunt run-all apply
command is used to build or change infrastructure from a tree of Terragrunt modules (stack).
Code
terragrunt run-all apply
Motivation
If you have a complex infrastructure that consists of multiple Terragrunt modules, you can use the run-all
command to apply changes to all the modules in the stack. This ensures that the changes are propagated correctly across the entire infrastructure.
Explanation
The run-all apply
command executes the apply
command for all modules in the Terragrunt stack. It applies changes recursively, starting from the root module and going down the module tree.
Example Output
The example output of the terragrunt run-all apply
command will display the execution progress for each module in the stack. It will show the changes applied to each module and provide any error or success messages.
Conclusion
In this article, we explored different use cases of the terragrunt
command and provided code examples for each use case. We discussed the motivation behind using each command, explained the arguments used, and provided example outputs for better understanding. Using these commands, you can effectively manage and control your Terraform infrastructure.