Terraform Basic Commands
Terraform Validate
This command helps you to validate the configuration file. It will validate all the configuration files available in a directory and tells you if there is any issue. If all the files are properly updated, then it will tell you th success message. Remember, this will validate only the syntax
Syntax
Terraform Format
This command helps you to format the configuration file in canonical format. This is mainly useful for readability.
Syntax
Terraform Show
This command helps you to show the current state of the infrastructure resource including all attributes created by Terraform.
Syntax
terraform show -json
command will show the current state of the infrastructure resource in JSON format
Terraform Provides
This command helps you to list out all the providers used in the configuration directory.
Syntax

Terraform Provides Mirror
This command helps you to copy provider plugins required for the current configuration to another directory.
Syntax

In the above example, all the provider plugins are copied from test
directory to new_dir
directory.
Terraform Refresh
This command helps you to sync terraform with real world infrastructure.
Syntax
Terraform Refresh
This command helps you to refresh the configuration file if any changes made.
Syntax

Terraform Graph
This command helps you to create a visual representation of the dependencies and configuration. Output is graph generated in a DOT format.
Syntax
If we have installed graphviz, then we can generate a graph which tells the dependencies and configuration details in diagram.
graph.svg
can be viewd in any browser.
Mutable Vs Immutable
If a software wants to upgrade from one version to another version, it can be done via manual approach, ansible, or shell script. But this can be done during maintenance window (if there is only server). In case, if we are using multiple server, we can upgrade the server one by one for HA. This is called as in place update. This is mutable infrastructure.
If there are multiple servers in the infrastructure, and each server has different version of same software. Then this is called as Configuration drift.
If there are multiple servers in the infrastructure and while upgrading the software, we spin up new server with new version and once that is in place and working, we can destroy one of the old server which running with old version. Similar approach can be taken for other servers too. This is called as Immutable infrastructure. Remember, while upgrading if any failures occur, then old server will still be in place. Terraform follows immutable approach. However, there is only one difference. By default, Terraform first destroy the resources first and then will create a new resource . However, it can be changed via lifecycle rules.