Lab description
This lab will cover the very basic terraform workflow for the updating of resources in Azure.
The terraform Core Workflow is still:
- Write
- Init (not needed this time)
- Plan
- Apply
Day 2 operation (update)
Setup
Make sure you are in the correct folder:
1
cd ~/terraform-labs
1
cd ~/terraform-labs
If you were unable to complete the last lab, you can find a copy of the files in the solutions folder
1. Write
-
Open
main.tf
on the editor and add atag
by making the below change withincontoso_rg
resource1 2 3
tags = { cost_center = "contoso research" }
It should look something like below:
1 2 3 4 5 6 7 8
resource "azurerm_resource_group" "contoso_rg" { name = "contoso_rg" location = "UK South" tags = { cost_center = "contoso research" } }
-
Save
main.tf
Note: we are not doing an
init
this time because the.terraform
folder andazure provider
plugin is already available from our previous run. When running in aCI / CD
pipeline, this step may be required.
2. Plan
-
Do a
terraform plan
but this time we are also storing theoutput
in a separate.tfplan
file1
terraform plan -out contoso.tfplan
1
terraform plan -out contoso.tfplan
-
Take a look at the plan file that’s been created using
show
command as before.1
terraform show contoso.tfplan
1
terraform show contoso.tfplan
You should see something like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
azurerm_resource_group.contoso_rg: Refreshing state... [id=/subscriptions/b857908d-3f5c-4477-91c1-0fbd08df4e88/resourceGroups/contoso_rg] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: ~ update in-place Terraform will perform the following actions: # azurerm_resource_group.contoso_rg will be updated in-place ~ resource "azurerm_resource_group" "contoso_rg" { id = "/subscriptions/b857908d-3f5c-4477-91c1-0fbd08df4e88/resourceGroups/contoso_rg" name = "contoso_rg" ~ tags = { + "cost_center" = "contoso research" } # (2 unchanged attributes hidden) } Plan: 0 to add, 1 to change, 0 to destroy. ─────────────────────────────────────────────────────────────────────── Saved the plan to: contoso.tfplan To perform exactly these actions, run the following command to apply: terraform apply "contoso.tfplan"
-
Add to
.gitignore
fileMuch like, .tfstate, .tfplan files can also contain sensitive information and should be kept as secure as possible. It is also in
.gitignore
of this repo.1
Add-Content .gitignore "$([Environment]::NewLine)contoso.tfplan"
1
echo "contoso.tfplan" >> .gitignore
See: https://developer.hashicorp.com/terraform/cli/commands/plan#out-filename
3. Apply
Pass the plan file to terraform apply and this time, we also do an -auto-approve
, so we are not prompted for approval.
1
terraform apply -auto-approve "contoso.tfplan"
1
terraform apply -auto-approve "contoso.tfplan"
The terminal output should state something like below
1
2
3
4
azurerm_resource_group.contoso_rg: Modifying... [id=/subscriptions/b857908d-3f5c-4477-91c1-0fbd08df4e88/resourceGroups/contoso_rg]
azurerm_resource_group.contoso_rg: Modifications complete after 1s [id=/subscriptions/b857908d-3f5c-4477-91c1-0fbd08df4e88/resourceGroups/contoso_rg]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
4. Verify
Verify that cost_center
tag has been created on our resource group.
1
az group show --name "contoso_rg"
1
az group show --name "contoso_rg"
Take a quick look at the state file as well.
1
terraform show terraform.tfstate
1
terraform show terraform.tfstate
5. Version control your code
-
Add
main.tf
to git1 2 3 4
cd ~/terraform-labs git add . git commit -m "Added tags to resource group"
1 2 3 4
cd ~/terraform-labs git add . git commit -m "Added tags to resource group"
-
You are welcome to push your changes to your own remote if you prefer.
Back to Lab Index