The Power of Terraform: A Step-by-Step Guide to Validate, Format, and Deploy (05) : Day 55

"From Code to Cloud: Terraform's Five Command Checklist for Infrastructure Success"

Table of contents

Introduction

In the world of Infrastructure as Code (IaC), precision and correctness are paramount. Terraform, the powerful IaC tool, offers several commands to ensure your infrastructure configuration is not just robust but also free from errors and inconsistencies. Let's dive into the essential commands that help you validate, format, initialize, plan, and apply your Terraform configurations.

Task 1: Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google Compute Engine, etc. (any one)

STEP 1: Create a Ubuntu Machine on AWS:

STEP 2: Install Terraform — To Install Terraform on Ubuntu Machine

sudo su

mkdir terraform_aws

cd terraform_aws

STEP 3: Create an IAM user:

STEP 4: Login as IAM User and Create an ENV Variable.

sudo apt update

sudo apt install awscli

aws configure

STEP 5: Create a variable.tf file for AMI (Amazon Machine Image)ID. As per your need, you can select OS and respected AMI ID.

STEP 6: We will set SSH keygen before the configuration of VPC, security groups.

STEP 7: Now create a file main.tf to configure keypair, vpc and security groups. We will define all these in resource blocks.

1. Resource Block (aws_instance):
• Creates an EC2 instance resource.
• Uses the specified AMI ID to launch the instance.
• Sets the instance type as t2.micro.
• Associates the instance with a security group defined by aws_security_groups
• Specifies the key pair name for SSH access.
• Adds a tag to the instance to identify it.

2. Resource Block (aws_security_group):
• Creates a security group resource and sets the name of the security group.
•Defines an ingress rule to allow SSH traffic on port 22 from any IP.

3. Resource Block (aws_key_pair):
• Creates an AWS key pair resource and sets the key pair name.
• Associates the public key from the (home/ubuntu/.ssh/terra-key.pub) resource with the key pair.

4. Resource Block (aws_default_vpc):
• Assign a default vpc to the instance.

Task 2: Check state files before running the plan and apply commands & Use the validate command to validate your tf file for errors and provide the Output generated by each command.

Run the below commands in the same directory.

terraform init

terraform plan

terraform apply

Terraform will download the necessary plugins and then create the EC2 instance based on the defined configuration. You can customize the configuration according to your specific requirements.

The state files contain information about the current state of your infrastructure, and Terraform uses this information to determine what changes need to be made. If the state files are not up-to-date, Terraform may make incorrect changes to your infrastructure.

Check State Files: To check the state files, you can use the terraform state list command. It lists all the resources managed by Terraform and their current state.

Validate Configuration File: To validate the configuration file for errors, you can use the terraform validate command. It checks the syntax and structure of the Terraform files and reports any errors or warnings.

These commands help you check the state files, validate the configuration, and get insights into the changes that Terraform will make to your infrastructure before actually applying them.

Task 3: Add a provisioner to the configuration file to configure the resource after it is created and use Terraform commands to apply for changes and destroy to remove resources.

To configure a resource after it is created, you can use provisioners in Terraform. Provisioners allow you to run scripts or execute commands on the resource during creation or destruction.

Provider Block:
• Sets the provider as AWS and specifies the region as “eu-west-2”. Here’s an example of adding a provisioner to an AWS EC2 instance resource:

To apply changes and provision the resources, you can use the following command:

Check the instance and click on the connect button, your instance will be running successfully.

Output:

To destroy the resources created by the Terraform configuration, you can use the following command:

terraform destroy

This command will remove the EC2 instance and any associated resources.

Task 3: Add lifecycle management configurations to the configuration file to control the creation, modification, and deletion of the resource and use Terraform commands to apply the changes.

To control the lifecycle management of resources in Terraform, you can use lifecycle blocks. These blocks allow you to define specific behaviour for resource creation, modification, and deletion. Here’s an example of adding lifecycle management configurations to an AWS EC2 instance resource:

Example:1

The prevent_destroy parameter is set to true, which will prevent destroying instances accidentally.

Example:2

In the above example, a lifecycle block is added to the AWS EC2 instance resource. The prevent_destroy parameter is set to true, which means that Terraform will create a new instance before destroying the old one during updates. The prevent_destroy parameter is set to false, which allows the instance to be destroyed using the terraform destroy command

To apply changes and create or update the resource with the lifecycle management configurations, you can use the following command:

terraform apply

terraform destroy

Output:

Conclusion

In the realm of Terraform, a diligent approach to configuration is your path to success. The terraform validate command ensures your code's syntax is impeccable, the terraform init command sets the stage, fetching providers and modules. With terraform plan, you gain foresight, understanding the proposed changes before they are executed, and finally, terraform apply brings your infrastructure to life.

Embrace these commands as your allies in the journey of Infrastructure as Code mastery.

If you read till the end, Thank you !..🤗and If you have any queries or want to share any suggestions, please feel free to comment below.