Day 64 - Terraform with AWS

Day 64 - Terraform with AWS

Provisioning on AWS is quite easy with 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 to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

  1. Create an EC2 instance and connect to it through SSH

  2. Install AWS CLI using the below command

 $ sudo apt install awscli

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

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

  1. Navigate to IAM in the AWS console

  2. Click on Users > Add users > Create a user with AmazonEC2FullAccess

  1. Once the user is created, we need to generate Access keys which are under Security Credentials

  1. Click on Create Access keys

Use case as CLI

Click on Create and download .csv file that contains Access key credentials.

Now use the below commands in EC2 to export the creds

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

Terraform is now configured with the our AWS.

Task-01

Provision an AWS EC2 instance using Terraform

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 resources 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 instances to be

provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "aws_ec2_test" {
        count = 4
        ami = "ami-08c40ec9ead489470"
        instance_type = "t2.micro"
        tags = {
     Name = "TerraformTestServerInstance"
  }
}

The resource block has a resource type of "aws_instance" and a resource identifier name of "aws_ec2_demo".

The count parameter is set to 4, which means 4 instances will be created.

The ami parameter specifies the Amazon Machine Image (AMI) to use for the instances. In this case, the AMI ID is "ami-0f8ca728008ff5af4".

The instance_type will be the type of instance that we wanted to create.

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

Apply terraform commands

Four Ec2 instances got created in our AWS console

Use $ terraform destroy to delete everything we created from the terraform script

Use a Free video Course for terraform here