Terraform with AWS

Terraform with AWS

ยท

3 min read

What is AWS-CLI?

The AWS CLI (Amazon Web Services Command Line Interface) is a versatile a command-line tool that enables users to interact with and manage a wide range of AWS services directly from the command line or through scripts. It facilitates tasks such as resource provisioning, application deployment, and security management, making it invaluable for developers, administrators, and DevOps professionals. AWS CLI is platform-agnostic, supporting Windows, macOS, and Linux, and offers features for access control and customizable output formats, empowering users to efficiently automate and control their AWS resources and workflows.

Installation of AWS CLI on EC2 Instance.

  • To install the AWS CLI on an EC2 instance, you must first connect to the instance using SSH. Once connected, you can install the AWS CLI using the following command:
sudo apt-get update
sudo apt install awscli -y
aws --version

Step -2: AWS IAM User

An IAM (Identity and Access Management) user in Amazon Web Services (AWS) is an entity representing an individual, system, or application within an AWS account, granting them unique credentials and permissions to access and manage AWS resources securely. IAM users help enforce the principle of least privilege by ensuring users and systems only have the necessary permissions, enhancing overall account security and access control.

Step 1: Create an IAM User in AWS Console for that go to Services search for IAM and click on it to Create a 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"
  }

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

In this blog, we have learned the essential prerequisites for AWS infrastructure provisioning, including AWS CLI installation, IAM User setup, and AWS Providers installation.

ย