Terraform with AWS

Terraform with AWS

Day 64 of 90daysofdevops

ยท

3 min read

Welcome to our blog on leveraging the combined strength of Terraform and AWS! Dive into the world of Infrastructure as Code (IaC) as we explore how Terraform simplifies the provisioning and management of AWS resources. Discover step-by-step guides, and best practices empowering you to build and manage scalable infrastructure in the AWS cloud.

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.

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

Step -3: Install AWS Providers

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

Add the region where you want your instances to be

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

Task-01

  • Create a terraform file named main.tf provision an AWS EC2 instance using terraform aws provider.

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

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

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

    And in the aws.tf 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-053b0d53c279acc90"
              instance_type = "t2.micro"
              tags = {
                 Name = "TerraformTestServerInstance"
        }
      }
    

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

  • 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.

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

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

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

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


Thank You,

I want to express my deepest gratitude to each and every one of you who has taken the time to read, engage, and support my journey.

Feel free to reach out to me if any corrections or add-ons are required on blogs. Your feedback is always welcome & appreciated.

~ Abhisek Moharana ๐Ÿ™‚

ย