Terraform with AWS

Terraform with AWS

·

4 min read

Provisioning on AWS is quite easy and straightforward 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.

2.SSH into EC2 instance

3.Install AWS CLI

sudo apt install awscli

Check aws cli version

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.

Create IAM user.

Create Access key for IAM user.

Click on 'Create access key'

Select 'command line interface'

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

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

After exporting the AWS credentials using the export commands, you won't need to run aws configure specifically for Terraform. The credentials set in the environment variables will be automatically picked up by Terraform when interacting with AWS.

Install required providers

Configure Terraform Providers and Region:

# Specify Terraform Configuration Requirements
terraform {
  # Require Terraform Version
  required_version = ">= 1.2.0"

  # Declare Required Providers
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }
}

# Configure AWS Provider and Region
provider "aws" {
  # Set AWS Region
  region = "ap-south-1"
}

Explanation:

  1. Terraform Configuration Requirements:

    • The terraform block sets the minimum required version of Terraform to execute the configuration (>= 1.2.0).
  2. Provider Configuration:

    • The required_providers block specifies the AWS provider and its version. Here, it declares that the AWS provider from hashicorp/aws with a version ~> 4.16 is required.
  3. AWS Provider Configuration:

    • The provider "aws" block configures the AWS provider, setting the region to "ap-south-1." This ensures that resources created in this configuration will be provisioned in the specified AWS region.

By using these blocks, you ensure that your Terraform configuration is compatible with the required version of Terraform, specifies the necessary AWS provider, and sets the AWS region where your instances will be created.

Task-01

Provision an AWS EC2 instance using Terraform:

# Define AWS EC2 Instances
resource "aws_instance" "aws_ec2_demo" {
  # Create 2 Instances
  count = 2

  # Set Amazon Machine Image (AMI)
  ami = "ami-0f8ca728008ff5af4"

  # Specify Instance Type
  instance_type = "t2.micro"

  # Attach Metadata Tags
  tags = {
    Name = "TerraformTestInstance"
  }
}

  1. Resource Block:

    • The resource block declares the creation of AWS EC2 instances with the type "aws_instance" and the name "aws_ec2_demo."
  2. Count Parameter:

    • The count parameter is set to 2, indicating that two instances will be created based on the specified configuration.
  3. AMI Parameter:

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

    • The instance_type parameter defines the type of instance to create. Here, it's set to "t2.micro."
  5. Tags Parameter:

    • The tags parameter is used to attach metadata tags to the instances. In this example, a tag named "Name" is added with the value "TerraformTestInstance."

Begin by setting up your working directory with essential plugins and modules. Execute the command terraform init to initialize the environment.

Next, generate an execution plan by evaluating the necessary modifications to reach the desired state of your infrastructure. Utilize the terraform plan command for this analysis.

Finally, implement the changes to either create new resources or update existing ones by using the terraform apply command.

After the execution, you can verify the creation of two instances using the aws console.


Thanks for reading until here. See you in the next one.