Terraform with AWS

This is Day 64 of #90daysofdevops challenge

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.

  • Install AWS CLI on the Ubuntu server.

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.

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>
  • Create an IAM user with suitable permissions.

  • Now, click on the Create access key and select CLI.

  • The access key and Secret access key are created. Now, export the access keys to configure the aws console to the terminal through awscli.

Install required providers

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

Add the region where you want your instances to be

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

Task-01: Provision an AWS EC2 instance using Terraform

resource "aws_instance" "aws_ec2_test" {
        count = 4
        ami = "ami-08c40ec9ead489470"
        instance_type = "t2.micro"
        tags = {
     Name = "TerraformTestServerInstance"
  }
}

Let's add this inside main.tf file.

  • Initialise the terraform in the servers to download the required providers.
terraform init

  • View the plan of Terraform to check the servers spin up with given configurations.
terraform plan

  • Now, apply the terraform file to create the servers.
terraform apply

Let’s check the console.


"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.

Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/) for the latest updates and discussions.

Did you find this article valuable?

Support Som Pandey's blog by becoming a sponsor. Any amount is appreciated!