Updated: Aug 9, 2020

Hello Readers, This blog is the Part-4 of our Terraform course series. In case you missed previous blogs please check out part one of it before going further. The code examples name mentioned for the reference of each topic can be found in the GitHub repository mentioned at the end of the article.


Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration.

A data source is defined under the data block.

It reads from a specific data source (aws_ami) and exports results under “app_ami”

Code: data_source_demo


Terraform has detailed logs that can be enabled by setting the TF_LOG environment variable to any value.

You can set TF_LOG to one of the log levels TRACE, DEBUG, INFO, WARN or ERROR to change the verbosity of the logs

 export TF_LOG_PATH=/tmp/crash.log
 export TF_LOG=TRACE

If these parameters are set , we will get more explanatory and verbose output.


  • Anyone who is into programming knows the importance of formatting the code for readability.

  • We have a command terraform fmt which will allow us to format the code in a clean manner so that it looks presentable.

  • just go inside the directory containing the .tf files and run terraform fmt

  • The terraform fmt command is used to rewrite Terraform configuration files to take care of the overall formatting.


  • Terraform Validate primarily checks whether a configuration is syntactically valid.

  • It can check various aspects including unsupported arguments, undeclared variables, and others.

  • Terraform validate command can be used to check the syntax of the files.

  • Below is the example

When something is wrong, we will get an error message like above.


Dynamic Block allows us to dynamically construct repeatable nested blocks which are supported inside resource, data, provider, and provisioner blocks:

Instead of writing code, again and again, we can use dynamic blocks.

Overview of iterators

The iterator argument (optional) sets the name of a temporary variable that represents the current element of the complex value

If omitted, the name of the variable defaults to the label of the dynamic block ("ingress" in the example above).

Code : dynamic_blocks_demo


You have created a new resource via Terraform.

Users have made a lot of manual changes (both infrastructure and inside the server)

Two ways to deal with this: Import The Changes to Terraform / Delete & Recreate the resource

The terraform taint command manually marks a Terraform-managed resource as tainted, forcing it to be destroyed and recreated on the next apply.

This command will not modify infrastructure but does modify the state file in order to mark a resource as tainted.

Once a resource is marked as tainted, the next plan will show that the resource will be destroyed and recreated and the next apply will implement this change.

Note that tainting a resource for recreation may affect resources that depend on the newly tainted resource.

Taint command is

terraform taint aws_instance.myec2

Code: taint_example

Here we will observe that as resource myec2 was tainted, it will be recreated again.

NOTE: To get the Terraform code used for demonstration in the article CLICK HERE

Hope you are enjoying the journey to learn to terraform.

884 views0 comments

Recent Posts

See All