Terraform with AWS

Terraform with AWS

Day 64 of #90DaysOfDevOps

Task-01

  • Provision an AWS EC2 instance using 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.

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.

Refer

After completion of the configuration continue on vs code with terraform.

write the below code:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "aws_ec2_test" {
  count         = 2
  ami           = "ami-0c7217cdde317cfec"
  instance_type = "t2.micro"
  tags = {
    Name = "TerraformTestServerInstance"
  }
}

Here in the terraform block, it will fetch AWS from it's registry with the specific version.

In the provider block, aws parameter is used to specify the region.

Next, in the resource block, as we want to create the AWS instances, the aws_instance parameter has been specified and aws_ec2_test as resource name.

In this resource block, count indicates the amount of instances you need to create, the ami to specify the image, instance_type to specify the instance type and tags to provide tag (name) to the instace.

You can find AMI ID from the launch instance.

After this write command: terraform init , followed by terraform apply . Then it will create instances.

Write "yes" when asked for a value.

Then it will start creating instances.

Verify on the AWS console.


The provision of the EC2 instance has been successfully completed.

Thank You For Reading. Have a great day ๐Ÿ˜Š

ย