Day 64 : Terraform with AWS

Day 64 : Terraform with AWS

Provisioning with AWS involves setting up and managing cloud resources on Amazon Web Services, a leading cloud computing platform. AWS offers a wide range of services, from virtual servers (EC2) to databases (RDS), enabling users to create, configure, and scale their infrastructure as needed. This process can be done through the AWS Management Console, CLI, or Infrastructure as Code (IaC) tools like AWS CloudFormation. It allows businesses and individuals to leverage the power of the cloud without the hassle of managing physical hardware, making it easier to build and scale applications and services.

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 update
sudo apt install unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Screenshot from 2023-09-21 09-17-00

  • After Installation Check the version of AWS CLI using the following command:
aws --version

Screenshot from 2023-09-21 09-17-34

What is an 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.

Screenshot from 2023-09-21 09-19-45

  • Step 2: Specify the User Name select the Access Type as Programmatic Access and click on Next: Permissions.

Screenshot from 2023-09-21 09-20-34

  • Step 3: Select the Attach Policies Directly and add the policy AmazonEC2FullAccess click on Next: Tags and then click on Next: Review.

Screenshot from 2023-09-21 09-22-08

  • Step 4: Review the details and click on Create User.

Screenshot from 2023-09-21 09-22-55

  • Step 5: Now Click on User go to Security Credentials and click on Create Access Key.

Screenshot from 2023-09-21 09-24-36

  • Step 6: Download the .csv file and save it on your local machine.

Screenshot from 2023-09-21 09-25-18

  • Step 7: Now Configure the AWS CLI using the following command:
aws configure

Screenshot from 2023-09-21 09-27-40

Let's Install the required providers for Terraform.

# Define required providers and their versions.
terraform {
  required_providers {
    # Declare the AWS provider with its source and version constraints.
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16" # Use version 4.x of the AWS provider.
    }
  }

  # Set the minimum required Terraform version.
  required_version = ">=1.2.0"
}

# Configure the AWS provider with your desired region.
provider "aws" {
  region = "ap-south-1" # Specify the AWS region (e.g., Asia Pacific - Mumbai).
}
  • Create a file main.tf and paste the above code in it.

Screenshot from 2023-09-21 09-32-56


Task 1: Provision an AWS EC2 instance using Terraform.

# Declare required providers and their versions.
terraform {
  required_providers {
    # Define the AWS provider with its source and version constraints.
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16" # Use version 4.x of the AWS provider.
    }
  }

  # Specify the minimum required Terraform version.
  required_version = ">=1.2.0"
}

# Configure the AWS provider with the desired region.
provider "aws" {
  region = "ap-south-1" # Set the AWS region to Asia Pacific - Mumbai.
}

# Define AWS EC2 instances.
resource "aws_instance" "example_instance" {
  count                  = 3 # Create 3 instances with the same configuration.
  ami                    = "ami-0f5ee92e2d63afc18" # Use a specific AMI.
  instance_type          = "t2.micro" # Specify the instance type.
  vpc_security_group_ids = ["sg-0c168b0d362f5a2b4"] # Attach a security group to instances.

  connection {
    type        = "ssh"
    user        = "ubuntu" # SSH user for connecting to instances.
    private_key = file("/home/ubuntu/apache.pem") # Path to the private key for SSH.
  }
}
  • Edit the main.tf file and paste the above code in it.

Screenshot from 2023-09-21 09-41-00

  • Now run the following command to initialize the Terraform.
terraform init

Screenshot from 2023-09-21 09-41-37

  • Now run the following command to check the execution plan.
terraform plan

Screenshot from 2023-09-21 09-42-06

  • Now run the following command to apply the changes.
terraform apply

Screenshot from 2023-09-21 09-43-23

  • Check the created instances on CLI.

Screenshot from 2023-09-21 09-43-37

  • Check the created instances on AWS Console.

Screenshot from 2023-09-21 09-45-03


Happy Learning :)

Did you find this article valuable?

Support DevOps by becoming a sponsor. Any amount is appreciated!