Course outlines for learning terraform.
expected time | requirements |
---|---|
60 minutes | Basic Terraform knowledge. |
Goal: Now that you are familiar with Terraform, you can automate deployments.
Continuous Integration/Continouos Development (CI/CD) is the practice of testing frequently and pushing changes to production frequently. The idea is that small changes can be tested and integrated (into production) quickly and safely.
CI/CD helps with these aspects:
(And likely many other benefits.)
How you setup CI/CD depends a bit on the facilities you have, but generally speaking all version control systems (GitHub/GitLab/BitBucket/Jenkins/Travis) have some kind of pipeline
-mechanism. Such a mechanism wakes up when a push (with commits) is done to a repository.
With Terraform you’ll probably have two types of repositories:
The Terraform Modules are independent, small re-usable pieces. Tests should happen on these repositories frequently.
The Terraform integrations use modules to describe an infrastructure of a product or service. Tests on these repositories can happen and eventually the integration can be apply
-ed to the production environment. This is typically a manual step.
CI/CD typically uses three stages:
terraform apply
.
\O/ +--- GitHub|GitLab ---+
| ---> | - module |
/ \ +---------------------+
terraform validate
.terraform fmt
.
\O/ +--- GitHub|GitLab ---+
| ---> | main.tf |
/ \ | versions.tf | <- Terraform Registry
+---------------------+
A typical repository uses either providers directly or modules. The repository contains all information required to build the infrastructure. (Except sensitive values such as usernames and password.)
By applying (terraform apply
) the Terraform code, your environment will be modified. If you setup CI/CD, you have the benefit of automatic changes, but the drawback of unexpected (automatic) changes happening to your environment.
Here is an example for BitBucket:
---
image: hashicorp/terraform:full
pipelines:
default:
- step:
script:
- terraform init
- terraform validate
- terraform plan
branches:
master:
- step:
trigger: manual
script:
- terraform init
- terraform validate
- terraform plan -out "planfile"
- terraform apply -input=false -auto-approve "planfile"