Terraform with AWS

Terraform with AWS

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.

sudo apt-get update && sudo apt-get install -y 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.

  • Create an IAM user with suitable permissions and create Access key for the IAM user.

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>

Install required providers

terraform {
 required_providers {
        aws = {
        source  = "hashicorp/aws"
        version = "~> 4.16"
}
}
        required_version = ">= 1.2.0"
}
  • Create a terraform file to download required providers on the system

  • Add the region where you want your instances to be
provider "aws" {
region = "us-east-1"
}

Task-01

  • Provision an AWS EC2 instance using Terraform
resource "aws_instance" "aws_ec2_test" {
        count = 4
        ami = "ami-0c7217cdde317cfec"
        instance_type = "t2.micro"
        tags = {
     Name = "TerraformTestServerInstance"
  }
}

Run terraform init after adding this configuration to initialize your Terraform environment and download the required provider.

Run terraform plan to preview the changes that Terraform will make to your infrastructure based on your configuration.

Run terraform apply to apply the changes

Now, checking the console will reveal four created servers.

Thank you.