Course outlines for learning terraform.
expected time | requirements |
---|---|
90 minutes | A computer with Terraform installed, terraform knowledge. |
Goal: Learn how to use Terragrunt.
Sometimes you’ve got an infrastructure coded that’s almost identical for another environment. For example:
In those cases you can copy-paste a whole repository, change a few values and apply
the terraform code. But what if you’ve copied such a repositories many times and you discover a flaw? You’d have to change multiple repositories to address the issue.
This is where Terragrunt comes in. Terragrunt allows you to define the “logic” (all resources
of an infrastructure) once and apply different settings for the different environments. (Actually terragrunt can also manage backends, cli arguments and more.
We’ll focus one managing terraform code with Terragrunt. Terragrunt (not by HashiCorp by the way) uses this filestructure:
.
├── modules # This contains (almost) normal Terraform code.
│ ├── main.tf
│ ├── providers.tf
│ └── versions.tf
├── development # This contains the specific settings for development.
│ └── terragrunt.hcl
├── production # This contains the specific settings for production.
│ └── terragrunt.hcl
└── README.md
The files development/terragrunt.hcl
and production/terragrunt.hcl
look quite similar, here is one:
terraform {
source = "../modules"
}
inputs = {
amount = 1
size = "s-1vcpu-1gb"
name = "development"
}
(Where the inputs
change per environment.)
The file modules/variables.tf
(or any .tf file) can pickup the variables set by terragrunt:
variable "amount" {
description = "The number of instances to create."
}
variable "size" {
description = "The size of the instance(s) to create."
}
variable "name" {
description = "The name of the instance(s) to create."
}
You can set default
values. In that case you may omit the variable in terragrunt.hcl
.
With those files, it’s time to create infrastructure.
cd
into production
.terragrunt init
to download dependencies. (Optional step, terragrunt plan
initialized when required.)terragrunt plan
.terragrunt apply
.terragrunt destroy
.Have a look.
environment
or location
.When going into the folders containing terragrunt.hcl
, try terragrunt apply
.
terragrunt init && terragrunt apply
in both applicationX
and applicationY
?