DevOps Classroom Series – 10/Jan/2020 – Terraform

terraform cli

  • refresh: is used to update the local state (*.tfstate) with resources created.
terraform refresh -var 'accesskey=<youraccesskey>' -var 'secretkey=<your-secret-key>' .
  • taint: is used to mark resources for recreation during next apply
terraform taint <resource-type>.<resource-name>
terraform apply .

# example

terraform taint aws_subnet.subnet3
# subnet 3 will be marked as tainted

terraform apply -var 'accesskey=<youraccesskey>' -var 'secretkey=<your-secret-key>' . 
# since subnet3 is marked as tainted, it will be recreated.
  • untaint: if you have tainted any resource by mistake/for any other reason and if you want it to be removed from taint then use untaint.
  • graph: Generate a graph from your template. Ensure you install dot from graphviz. Refer Here
terraform graph | dot -Tsvg > graph.svg

Terraform outputs

  • Output is result of the infra provisioning which can be shared
  • To Create outputs, create a new file called as outputs.tf with following content
output "vpc-id" {
  value = "${aws_vpc.myvpc.id}"
}

output "subnet1-id" {
  value = "${aws_subnet.subnet1.id}"
}


  • Now execute terraform apply and observe the output

Terraform modules

  • Module is reusable terraform configuration.
  • Terraform community shares many modules for resuse in Terraform Registry
  • Terraform templates written by us can also be reused as modules.

Using Terraform module from Local folder

  • Create a new directory called as moduledemo
  • in the module demo create one file main.tf with following content
module "<name>" {
  source  = "../hello-tf"
  accesskey = ""
  secretkey = ""
}
  • Now execute
terraform init
terraform apply .
  • In hello-tf all the variables will be arguments to module and all the outputs will be attributes of module.
  • Any varaible without default is required argument and variable with default is optional argument.

Next Sections

  • Terraform Registry example
  • Terraform remote-state
  • terraform env and workspaces
  • Terraform import
  • Terraform example with Azure.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin