TerraWeek Day 3

Supriya SurkarSupriya Surkar
3 min read

This is #TerraWeek challenge under the guidance of Shubham Londhe sir.

Welcome back to Terraform Day 3! Today, we're going to dive deeper into Terraform, the cool tool that lets us manage our computer stuff using code. We'll explore some advanced tricks that will turn you into a Terraform pro. And don't worry if you're new to this โ€“ we'll walk through everything step by step with simple examples to help you understand it all.

Task1: Let's start by setting up a Terraform configuration file to define an EC2 instance on AWS.

  1. Setting Up the Configuration File: Create a file named main.tf in your Terraform project folder.

  2. Defining the AWS Provider: Tell Terraform that we'll be working with AWS and specify the region:

provider "aws" {
  region = "us-west-2"  # Replace with your desired region
}

  1. Creating an EC2 Instance Resource: Now, let's define the EC2 instance resource:
resource "aws_instance" "my_instancce" {
  ami           = "ami-0e0bf53f6def86294"  # Amazon Linux 2 AMI ID
  instance_type = var.instace_type
}

Task 2: Checking State Files and Validating Configuration

Before we move on, let's make sure everything is set up correctly and there are no errors in our configuration.

Before checking the Terraform state, make sure you have initialized Terraform, planned your changes, and applied them using the following commands:

terraform init
terraform plan
terraform apply

Once you've applied your changes, you can use the following commands to view the Terraform state:

  1. Checking State Files: Use the terraform state command to see the current state of your infrastructure:
terraform state list  # List all resources managed by Terraform
terraform state show aws_instance.my_instance  # Show details of a specific resource

  1. Validating Configuration: Double-check that our configuration is error-free:
terraform validate

Task 3: Adding Provisioners and Applying Changes

Now, let's add a provisioner to execute commands on our EC2 instance after it's created.

  1. Adding a Provisioner: Update our main.tf file to include a provisioner:
resource "aws_instance" "my_instance" {
  ami           = "ami-0e0bf53f6def86294"
  instance_type = "t2.micro"

  provisioner "local-exec" {
    command = "echo 'Hello, Terraform!' > hello.txt"
  }
}

  1. Applying Changes: Apply the changes to our infrastructure:
terraform apply

Task 4: Implementing Lifecycle Management

Lastly, let's learn about lifecycle management to control how Terraform handles our resources.

  1. Adding Lifecycle Configuration: Enhance our main.tf file to include lifecycle settings:
resource "aws_instance" "my_instance" {
  ami           = "ami-0e0bf53f6def86294"
  instance_type = "t2.micro"

  lifecycle {
    create_before_destroy = true
  }
}

  1. Applying Lifecycle Changes: Apply these changes to our infrastructure:
terraform apply

Conclusion

Congratulations! You've learned some advanced Terraform techniques for managing AWS resources. With these skills, you can now confidently provision and manage EC2 instances using Terraform. Keep practicing and exploring, and soon you'll be a Terraform pro!

๐Ÿ’ก
Feel free to drop any questions you have in the comments section! ๐Ÿค” I would be happy to answer them.
๐Ÿ’ก
If you found this post helpful, a thumbs up ๐Ÿ‘ would mean a lot. Don't forget to hit the follow button for more useful content. Your support is truly valued!

Thank you for taking the time to read! ๐Ÿ’š Happy reading!

0
Subscribe to my newsletter

Read articles from Supriya Surkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Supriya Surkar
Supriya Surkar