DevOps(Day: 64) Terraform with AWS

DevOps(Day: 64) Terraform with AWS

Day 64:#90DaysOfDevOpsChallenge

Dear Learners, As you know very well in my previous lecture, I have Explain the Terraform Variables. Now in these Article I explain the Terraform with AWS with examples:-

TABLE OF CONTENTS

Prerequisites: AWS CLI Install, AWS IAM User, Install the required providers.

Task -01

Provision an AWS EC2 instance using Terraform

Prerequisites:-

AWS CLI installed

The AWS Command Line interface(AWS CLI) is a unified tool to manage your AWS services. With just one tool and configure,you can control multiple AWS servivces from the command line and automate them through scripts.

1 Create an EC2 instanc in AWS.

2 SSH into EC2 instance.

3 Install the AWS CLI.

sudo apt install awscli

No alt text provided for this image

sudo apt install awscli Diagram.

Check the aws cli version (highlight in White Colour)

aws --version

No alt text provided for this image

Check the aws cli version Diagram.

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 resource. You use IAM to control access to AWS resource. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use the resources.

(highlight in Blue Colour)

No alt text provided for this image

IAM (identity Access Management) AWS identity and Access Management (IAM) Diagram

Create Access key for IAM user.

Click on "Create a access key"

No alt text provided for this image

Select the command line interface (highlight in Blue Colour)

No alt text provided for this image

Select the command line interface Diagram.

No alt text provided for this image

Reterive Access Key Diagram

In order to connect your AWS account and Terraform you need the access keys and secret keys exported to your machine.

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

No alt text provided for this image

In order to connect your AWS account and Terraform you need the access keys and secret keys exported to your machine Diagram.

Install the required Providers

terraform {
 required_providers {
        aws = {
        source  = "hashicorp/aws"
        version = "~> 4.16"
}
}
        required_version = ">= 1.2.0"
}

The terraform block defines the version of Terraform that is required to execute this configuration. In this case it specifies that the Terraform version must be >=1.2.0

The required_providers block declares the AWS provider and its version that Terraform will use for the defined in this configuration. In this case it declares the AWS provider with the source hashicorp/aws and specifies that the version of the provider should be ~> 4.16 which means any version of the AWS provider greater than or equal to 4.16 and less than 5.0 will be acceptable.

Add the region where you want your instance to be.

provider "aws" {
region = "ap-south-1"
}

Now add the syntax in themain.tffile in the below diagram

No alt text provided for this image

Add the region where you want your instance to be Diagram.

Task -01

Provision an AWS EC2 instance using the Terraform

resource "aws_instance" "aws_ec2_demo" {
        count = 2
        ami = "ami-0f8ca728008ff5af4"
        instance_type = "t2.micro"
        tags = {
            Name = "TerraformTestInstance"
  }
}

The resource block has a resource of "aws_instance" and a resource name of "aws_ec2_demo". The count parameter is set to 2 which means that 2 instances will be created.

The ami parameter specifies the Amazon Machine image(AMI) to use for the instance. In this case the AMI ID is "ami-0f8c72008ff5af4".

The instance_type parameter specifies the type of instance to create. In this case, the instance type is "t2.micro"

The tags parameter specifies metadata to attach to the instance, in this case a tag named "Name" with the value "TerraformTestinstance".

No alt text provided for this image

The tags parameter specifies metadata to attach to the instance, in this case a tag named "Name" with the value "TerraformTestinstance" Diagram

First initialize the working directory with the necessary plugins and modules by executing terraform init

No alt text provided for this image

First initialize the working directory with the necessary plugins and modules by executing terraform init

It will create an execution plan by analyzing the changes required to achieve the desired state of your infrastructure with terraform plan

No alt text provided for this image

terraform plan Diagram

No alt text provided for this image

terraform plan Diagram

No alt text provided for this image

terraform plan Diagram

No alt text provided for this image

terraform plan Diagram

Finally, it will apply the changes to create or update resources as needed with terraform apply.

No alt text provided for this image

Finally, it will apply the changes to create or update resources as needed with terraform apply Diagram

No alt text provided for this image

No alt text provided for this image

terraform apply Diagram

No alt text provided for this image

No alt text provided for this image

terraform apply Diagram

Now at the end you can check now 2 instances created using Terraform. Here are the 2 Ec2 instnce which are highlight in the blue colour and tick in the below Diagram

No alt text provided for this image

check now 2 instances created using Terraform Diagram.

Thank you for reading!! Hope you find this helpful.

#day64challenge#90daysofdevops

Always open to suggestions..!!

~ Manoj Bhamidipati 🙂