Day 64 - Terraform with AWS

Day 64 - Terraform with AWS

Prerequisites

Step 1: AWS CLI installed

The AWS Command Line Interface (AWS CLI) is a unified tool to manage your AWS services. With AWS CLI configured we can interact with AWS services directly from your command line. We can use various AWS CLI commands to manage and configure your AWS resources.

sudo apt-get update

sudo apt install awscli -y

aws --version

Step 2: AWS IAM User

IAM (Identity Access Management) AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources. Use IAM to grant a user to provide admin privileges and get the Access Key for that.

To connect your AWS account and Terraform, you need the access keys and secret access keys exported to your machine which we already got from AWS IAM User. Refer link: Create AWS Access Key.

export AWS_ACCESS_KEY_ID=<access key>
export AWS_SECRET_ACCESS_KEY=<secret access key>

And Configure aws as well.

aws configure

Step 3: Install AWS Providers

# terraform.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

Add the region where you want your instances to be.

# provider.tf

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

TASK: Create a terraform file, provision an AWS EC2 instance using terraform aws provider.

Step 1: Create a terraform.tf and pass the aws provider.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

Step 2: Create a providers.tf and put the selected AWS Region that you want to create an EC2 instance.

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

Step 3: Create the aws.tf file to provide all the details like AMI ID, instance type and instance name and the number of EC2 count that has to be created.

resource "aws_instance" "aws_ec2_test" {
     count = 1
     ami = "ami-0c7217cdde317cfec"
     instance_type = "t2.micro"
     tags = {
        Name = "TerraformTestServerInstance"
     }
}

Step 4: Now the first step is to initialize the working directory with the necessary plugins and modules by executing terraform init

Step 5: Once you initialize all the plugins required for AWS, now execute the terraform plan which will create an execution plan by analyzing the changes required to achieve the desired state of your infrastructure.

Step 6: Finally, use the command terraform apply it will apply the changes to create or update resources as needed

Step 7: You can check, a new EC2 instance is created using Terraform as we provided a count as 1.

Step 8: Once you are done with the newly created instance we can use terraform destroy command which will delete the complete infrastructure.

Step 9: Now from EC2 Instance, we can verify that the newly created EC2 instance is in the terminated state.

Conclusion

In Conclusion, Terraform offers a powerful infrastructure as code (IaC) solution for managing AWS resources efficiently and consistently. With Terraform, infrastructure can be defined in a declarative configuration language, enabling automation and version control.

By leveraging Terraform's state management and dependency resolution features, teams can provision, modify, and destroy AWS infrastructure reliably. This simplifies deployment workflows, enhances collaboration, and ensures infrastructure consistency across environments.

Overall, Terraform combined with AWS empowers organizations to streamline operations, reduce manual errors, and accelerate the delivery of scalable and resilient cloud-based solutions.

In this blog, we have created AWS EC2 instance using terraform.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

👆The information presented above is based on my interpretation. Suggestions are always welcome.😊

~Smriti Sharma✌