Day 64 Task: Terraform with AWS

Day 64 Task: Terraform with AWS

ยท

2 min read

Provisioning on AWS becomes remarkably straightforward and efficient with Terraform. Terraform is a potent Infrastructure as Code (IaC) tool that streamlines the process of defining, deploying, and managing cloud resources.

๐Ÿ”ถ 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.

๐Ÿ”ถ 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.

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"
}

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

Create a main.tf file adds all the blocks of provider, resource, and required providers.

 terraform {
   required_providers {
     aws = {
       source = "hashicorp/aws"
       version = "5.17.0"
     }
   }
 }

 provider "aws" {
   region = "eu-north-1"
 }

 resource "aws_instance" "aws_ec2_test" {
   count         = 4
   ami           = "ami-0fe8bec493a81c7da"
   instance_type = "t3.micro"
   subnet_id = "subnet-00f8d547b19b2ff68"

   tags = {
     Name = "TerraformByNilesh"
   }
 }

Now Save and run the command terraform init to initialize terraform and install all the plugins and providers.

Run terraform plan to outline the required changes to achieve the planned infrastructure.

Now run terraform apply to create 4 instances.

You can check and verify on the AWS EC2 console for new instances running.

In conclusion, provisioning an AWS EC2 instance using Terraform is a powerful and efficient way to manage your infrastructure as code. Terraform allows you to define your infrastructure in a declarative manner, making it easy to version, reproduce, and scale your resources on AWS.

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #AWS

ย