Day 3:  Terraform configuration files

Day 3: Terraform configuration files

Defining and Managing Your Infrastructure as Code

ยท

4 min read

๐Ÿ”ถ TerraWeek Day 3

First of all, Make an instance and install Terraform.

๐Ÿ”ถ Task 1: Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google Compute Engine, etc. (anyone)

Terraform configuration file (main.tf) to define an AWS EC2 instance resource:

provider "aws" {
  region = "us-east-1"  # Change this to your desired AWS region
}
resource "aws_instance" "terra_instance" {
   # Specify the desired Amazon Machine Image (AMI)
  ami           = "ami-053b0d53c279acc90"  
  # Specify the instance type  
  instance_type = "t2.micro"  

  tags = {
    Name = "TF_instances"  
  }
}

In this example:

  • We define an EC2 instance using the aws_instance resource block.

  • Within the instance block, we set the AMI (Amazon Machine Image) and instance type.

  • We also assign tags to the instance for identification.


๐Ÿ”ถ 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.

Checking the state file below run the plan and apply commands.

terraform init: the plugins are installed.

terraform plan: The execution plan is generated.

terraform apply: The execution is done and the state is maintained.

terraform validate:


๐Ÿ”ถ 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.

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "terra_instance" {
   # Specify the desired Amazon Machine Image (AMI)
  ami           = "ami-053b0d53c279acc90"
  # Specify the instance type
  instance_type = "t2.micro"

  tags = {
    Name = "TF_instances"
  }


provisioner "local-exec" {
   command  = "echo 'This file has been created for provisioner' > /home/ubuntu/Terraweek/Day3/terra_file.txt"

}
}

Now, to apply the changes and create the resources:

terraform init: Initialize the Terraform configuration
terraform apply: Apply the changes and create the EC2 instance.

To destroy the resources:

terraform destroy: Destroy the created resources.


๐Ÿ”ถ Task 4: 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 add lifecycle management configurations to control the creation, modification, and deletion of a resource in Terraform, you can use the lifecycle block within the resource configuration. Here's an example of how to do this for an AWS EC2 instance:

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "terra_instance" {
   # Specify the desired Amazon Machine Image (AMI)
  ami           = "ami-053b0d53c279acc90"
  # Specify the instance type
  instance_type = "t2.micro"

  tags = {
    Name = "TF_instances"
  }

lifecycle {
    create_before_destroy = true
    prevent_destroy      = false
  }

provisioner "local-exec" {
   command  = "echo 'This file has been created for provisioner' > /home/ubuntu/Terraweek/Day3/terra_file.txt"

}
}

In this example:

  • create_before_destroy is set to true, which means Terraform will create a new instance before destroying the old one when you apply changes. This can help ensure high availability during updates.

  • prevent_destroy is set to false, which allows Terraform to destroy the resource when you use the terraform destroy command. By default, Terraform would prevent the destruction of a resource, but in some cases, you might want to allow it.

After adding the lifecycle block to your resource configuration, save the Terraform configuration file, and then use the terraform apply command to apply the changes. Terraform will take into account the lifecycle settings when managing the resource.

After using terraform apply the old instance will destroy and create new instances:

#Terraweek Repo: https://github.com/Chandreshpatle28/TerraWeek.git


Happy Learning :)

Stay in the loop with my latest insights and articles on cloud โ˜๏ธ and DevOps โ™พ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #Terraform

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!

ย