Day 2 - Life Cycle of Terraform

Day 2 - Life Cycle of Terraform

10 Days of Terraform

Table of contents

Terraform Init :-

Terraform Init performs various initialization steps to prepare the current working directory for use with Terraform. This command downloads the necessary provider plugins and sets up the backend configuration. The backend configuration determines where Terraform stores its state file, which tracks the current state of your infrastructure.

Some Points to Note

  • Running multiple times ensures the working directory is up to date.

  • It initializes a working directory containing Terraform config files, performs backend initialization (storage for state file and module installation), and downloads from the Terraform registry to local.

  • It also handles provider plugin installation, with plugins being downloaded in a subdirectory of the present directory at the .plugins folder.

terraform init #initialize directory, pull down providers
terraform init -get-plugins=false #initialize directory, do not download plugins
terraform init -verify-plugins=false #initialize directory, do not verify plugins for Hashicorp signature
terraform init -input=true #Ask for input if necessary
terraform init -lock=false #Disable locking of state files during state-related operations

Terraform Validate :-

The "validate" command in Terraform is used to verify whether a configuration is syntactically correct and internally consistent, regardless of any provided variables or existing state. Its primary focus is to verify the correctness of reusable modules, including attribute names and value types. It checks the syntax of the Terraform files for format and correctness. This command performs a static analysis of your code, checking for any errors or warnings in the configuration, without making any modifications to your infrastructure.

To use the Terraform validate command, navigate to the directory containing your Terraform files and execute the following command:

  1. If your Terraform configuration is valid and error-free, the command will exit with a success message, indicating that the configuration is syntactically correct.

    Terraform validate success

  2. If there are any syntax errors or issues in your Terraform files, the command will display specific error messages pointing to the problematic lines or elements in your configuration.

    Terraform validate Error

In this case, you need to review the error message, correct the syntax issue, and run Terraform validate again until the configuration is error-free.

Terraform Plan :-
Running terraform plan to check whether the execution plan for configuration matches your expectations before provisioning or changing the infra

Terraform creates an execution plan that includes the current state of already existing remote objects to ensure that it is up to date. It compares the configuration and notes any differences, proposing a set of changes and actions that should be taken.

terraform plan #Creates an execution plan (dry run) 
terraform plan -out=out.plan #save generated plan output as a file named as out.plan
terraform plan -destroy #Outputs a destroy plan

Output example

Terraform plan

Terraform Apply :-
To execute the actions proposed in terraform plan, there are two ways to apply it. /

  • The first method is to run terraform apply without any arguments, which will automatically create a new plan and ask for approval before taking any actions.

  • The second method is to save the plan during the planning phase using the -out option. In this case, Terraform will approve the plan without any further approval needed. Alternatively, we can pass some arguments as well. Once the apply command is executed, it will create a state file.

Terraform Apply

Terraform Destroy :-
When you need to dismantle the infrastructure, utilize Terraform destroy. This command examines the state file and eradicates all resources managed by Terraform. It's crucial to exercise caution with this command as it permanently removes resources.

Throughout the workflow, it's vital to maintain the Terraform state file, which monitors the current state of your infrastructure. This file should be securely stored and version-controlled to ensure consistency and enable collaboration.

Additionally, you can employ other Terraform commands and features as required, such as Terraform validate to verify your configuration for syntax errors, terraform state to directly interact with the state file, and modules to structure and reuse infrastructure code.

It's important to recognize that while this outlines a general workflow, actual practices may differ based on your specific use case, team processes, and best practices in your organization.

Screenshot-2023-06-01-at-51558-PM-(1).png

Extra Commands:-

Terraform Taint -
Terraform taint is a command that can be used to mark a resource as degraded or damaged. This indicates that the resource will be destroyed and recreated during the next apply operation. This can be particularly helpful when a resource is in an undesirable or unexpected state, despite its configuration remaining unchanged. Terraform enforces the recreation of resources even if the configuration matches the current state.

terraform taint aws_instance.my_ec2 #marks a Terraform-managed resource as tainted, forcing it to be destroyed and recreated on the next apply

terraform untaint aws_instance.my_ec2 #unmarks a Terraform-managed resource as tainted

Terraform Graph -
The terraform graph command is a useful tool for generating a visual representation of either a configuration or execution plan. This output is in the DOT format, which can be used to create charts using GraphViz. The graph is outputted in DOT format, which can be read by various programs, including GraphViz and many web services.

terraform graph -type=plan | dot -Tpng > graph.png

Did you find this article valuable?

Support Venkatesh Krishnan R by becoming a sponsor. Any amount is appreciated!