You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/blog/terragrunt-envs-management.md
+14-16Lines changed: 14 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,9 +11,9 @@ Managing multiple environments was a never-ending headache for me. Like many oth
11
11
12
12
## The Repetition Trap
13
13
14
-
At first, my solution was simple: copy-paste. I created three separate folders: one for production, one for staging, and one for development. In each folder, I had the same Terraform files with minor tweaks like different variables or configurations for each environment. It worked, but I soon found myself dealing with the chaos of managing three sets of nearly identical infrastructure code.
14
+
At first, my solution was simple: copy-paste. I created three separate folders: one for production, one for staging, and one for development. Each folder contained the same Terraform files with minor tweaks, such as different variables or configurations for each environment. It worked, but I soon found myself dealing with the chaos of managing three sets of nearly identical infrastructure code.
15
15
16
-
Let’s say I needed to update a configuration for my database service. This meant that I had to open each folder, make the same change in three different places, and run Terraform multiple times for each environment. It was a tedious and error-prone process, and as my infrastructure grew, so did the complexity. I knew there had to be a better way.
16
+
Let’s say I needed to update a configuration for my database service. This meant opening each folder, making the same change in three different places, and running Terraform multiple times for each environment. It was a tedious and error-prone process, and as my infrastructure grew, so did the complexity. I knew there had to be a better way.
17
17
18
18
> My Initial Setup was like this
19
19
@@ -33,17 +33,18 @@ Let’s say I needed to update a configuration for my database service. This mea
33
33
└── redis.tf
34
34
```
35
35
36
-
The terraform files are contains same resources in different environment but the only difference was the values of the variables.
37
-
That is lot of Repeated Code.
36
+
The Terraform files contain the same resources in different environments, but the only difference is the values of the variables.
37
+
That is lot of repeated code.
38
38
39
39
## The Terragrunt Revelation
40
40
41
41
That’s when I discovered Terragrunt. Terragrunt is a thin wrapper for Terraform that provides extra tools for keeping your Terraform code DRY (Don’t Repeat Yourself). It allows you to manage multiple Terraform modules in a single repository and reuse code across different environments. Terragrunt’s ability to manage remote state, generate configurations, and apply configurations across multiple environments made it the perfect solution for my environment management woes.
42
42
43
43
How we reduce the repeated code using Terragrunt:
44
+
44
45
First step was to make a module of the resources with all the changeable variables exposed, Next was to use them in each environment.
45
46
46
-
> The new setup with Terragrunt
47
+
> The new folder structure using Terragrunt
47
48
48
49
```text
49
50
.
@@ -71,8 +72,7 @@ First step was to make a module of the resources with all the changeable variabl
71
72
└── terragrunt.hcl
72
73
```
73
74
74
-
In the `terragrunt.hcl` file, we define the module to use and the
75
-
variables to pass to the module.
75
+
In the `terragrunt.hcl` file, we define the module to use and the variables to pass to the module.
76
76
77
77
```hcl
78
78
# terragrunt.hcl
@@ -85,8 +85,8 @@ inputs = {
85
85
}
86
86
```
87
87
88
-
Similarly in the other terragrunt files we define the module to use and the variables to pass to the module.
89
-
and now for the storing the state file we can use the remote state file.
88
+
Similarly, in the other terragrunt files, we define the module to use and the variables to pass to the module.
89
+
And now for the storing the state file we can use the remote state file.
90
90
91
91
```hcl
92
92
# terragrunt.hcl
@@ -107,8 +107,7 @@ EOF
107
107
}
108
108
```
109
109
110
-
This tells terragrunt to create a new file `backend.tf` with the contents of the `contents` block, Even if backend.tf already exists it will overwrite the contents because we have specified `overwrite_terragrunt`.
111
-
This is like a defining a function, Now we need to call it in the files i.e `terragrunt.hcl` files.
110
+
This tells terragrunt to create a new file `backend.tf` with the contents of the `contents` block, Even if backend.tf already exists it will overwrite the contents because we have specified `overwrite_terragrunt`. This is like a defining a function, Now we need to call it in the files i.e `terragrunt.hcl` files.
112
111
113
112
> This would look like this
114
113
@@ -118,7 +117,7 @@ include "root" {
118
117
}
119
118
```
120
119
121
-
here the `find_in_parent_folders` returns the absolute path to the first `terragrunt.hcl` file it finds in the parent folders above the current `terragrunt.hcl` file.
120
+
Here the `find_in_parent_folders` returns the absolute path to the first `terragrunt.hcl` file it finds in the parent folders above the current `terragrunt.hcl` file.
122
121
and the include block tells terragrunt to include all the cofigurations from the file with the path returned by the `find_in_parent_folders` function.
123
122
124
123
## The Terragrunt Workflow
@@ -136,7 +135,7 @@ This will run the terragrunt plan in each sub directory it finds, Then it reads
136
135
5. Terragrunt stores the Terraform state in the remote state file
137
136
6. Terragrunt returns the output of the Terraform run
138
137
139
-
This workflow allows me to manage multiple environments with ease. I can make changes to my infrastructure code in a single place and apply those changes across all environments with a single command. Terragrunt’s ability to keep my Terraform code DRY has saved me time and effort, and I no longer have to worry about managing multiple sets of nearly identical infrastructure code.
138
+
This workflow allows me to manage multiple environments with ease. I am able to make changes to my infrastructure code in one place and then apply those changes across all environments with just one command. Terragrunt's capability to keep my Terraform code DRY has saved me time and effort, eliminating the need to manage multiple sets of nearly identical infrastructure code.
140
139
141
140
## How Terragrunt worked for me
142
141
@@ -148,14 +147,13 @@ This workflow allows me to manage multiple environments with ease. I can make ch
148
147
149
148
4. Dependency Management: Another Terragrunt superpower is managing dependencies between infrastructure components. For example, my ECS service depends on the VPC and subnets being provisioned first. Terragrunt’s dependencies block ensures that I deploy resources in the correct order, so my ECS service doesn’t attempt to launch before the network is ready.
150
149
151
-
## Obvious Question: Why to use terragrunt instead of directly using terraform modules or terraform workspaces
150
+
## Obvious Question: Why to use terragrunt instead of directly using terraform modules or terraform workspaces?
152
151
153
152
While terraform workspaces can be using for multiple environments , [hashicorp does not recommend it](https://developer.hashicorp.com/terraform/cli/workspaces)
154
153
155
154
> CLI workspaces within a working directory use the same backend, so they are not a suitable isolation mechanism for this scenario.
156
155
157
-
Even with terraform modules you can just pass the variable and use it but that's just adding more boilerplate and you are just trying to prove the point that modules can be used here and
158
-
It will become more complex to maintain down the road, I'm not saying you should use terragrunt I'm just pointing the facts. Ultimately it depends on the type of project you are working on.
156
+
Even with Terraform modules, you can simply pass the variable and use it. However, this adds more boilerplate, making it more complex to maintain in the long run. I'm not suggesting that you should use Terragrunt; I'm just pointing out the facts. Ultimately, the decision depends on the type of project you are working on.
0 commit comments