Day 64 - Terraform with AWS ????

Day 64 - 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.

Steps:

  • Provision of an EC2 instance
  • SSh into the EC2 instance
  • Install AWS CLI

use command:

sudo apt install awscli        
No alt text provided for this image

Confirm and check the AWS CLI version

No alt text provided for this image

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

No alt text provided for this image

Make an access key for the IAM user.

Select Create Access Key

Select Command Line Interface

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

You'll need the access keys and secret access keys exported to your machine to connect your AWS account with Terraform.

export AWS_ACCESS_KEY_ID=<access key>
export AWS_SECRET_ACCESS_KEY=<secret access key>        
No alt text provided for this image

Install required providers

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

The terraform block specifies the Terraform version necessary to run this setup. In this situation, it states that the Terraform version must be more than or equal to 1.2.0.

The required_providers section specifies the AWS provider and version that will be used by Terraform for the resources defined in this configuration. In this case, it defines the AWS provider with the source hashicorp/aws and specifies that the provider version should be > 4.16, which means that any version of the AWS provider more than or equal to 4.16 but less than 5.0 is acceptable.

Add the region where you want your instances to appear.

provider "aws" {
region = "eu-west-1"
}        
No alt text provided for this image

Task-01

Provision an AWS EC2 instance using Terraform

resource "aws_instance" "aws_ec2_demo" {
        count = 2
        ami = "ami-00aa9d3df94c6c354"
        instance_type = "t2.micro"
        tags = {
            Name = "DemoTerraformEC2"
  }
}        

The resource block has an "aws_instance" resource type and an "aws_ec2_demo" resource name. Because the count argument is set to 2, two instances will be produced.

The Amazon Machine Image (AMI) to utilize for the instances is specified by the ami option. The AMI ID in this situation is ""ami-00aa9d3df94c6c354"".

The instance_type option indicates the type of instance that should be created. The instance type is "t2.micro" in this example.

The tags option defines the metadata to be attached to the instance, in this case, a tag called "Name" with the value "DemoTerraformEC2"

No alt text provided for this image

First, run terraform init to create the working directory with the appropriate plugins and modules.

No alt text provided for this image

It will generate an execution plan based on an analysis of the changes required to attain the desired state of your infrastructure with Terraform plan.

No alt text provided for this image
No alt text provided for this image

Finally, it will use terraform apply to construct or update resources as needed.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

You can verify there will be two ec2 instances in your AWS console

No alt text provided for this image

I appreciate your reading. I am sure it will benefit you. ?????



Sunil Kumar

Senior Cloud Engineer - Nagarro | Google Cloud Certified {"CDI, ACE , PCA, PDE}|Cloud DevOps Specialist | 5x GCP , Azure Certified | Devops | Linux | Docker | Terraform | Jenkins CI/CD | Kubernetes

1 年

Great work

要查看或添加评论,请登录

Vinay Kumar的更多文章

  • DevOps Project - 4 ????

    DevOps Project - 4 ????

    Project Description The project aims to deploy a web application using Docker Swarm, a container orchestration tool…

    7 条评论
  • DevOps Project 3 ????

    DevOps Project 3 ????

    Project Description The project involves hosting a static website using an AWS S3 bucket. Amazon S3 is an object…

    3 条评论
  • DevOps Project -2 ????

    DevOps Project -2 ????

    Project Description The project is about automating the deployment process of a web application using Jenkins and its…

  • Day 80: DevOps Project 1 ????

    Day 80: DevOps Project 1 ????

    Project Description The project aims to automate the building, testing, and deployment process of a web application…

    2 条评论
  • Day 73 - Setup Grafana on AWS EC2 Instance ????

    Day 73 - Setup Grafana on AWS EC2 Instance ????

    Task: Set up grafana in your local environment on AWS EC2. Go to the AWS console and Launch an EC2 instance To enable…

  • Day 72 - Grafana ????

    Day 72 - Grafana ????

    What is Grafana? No matter where your metrics are kept, Grafana is an open-source data visualization and monitoring…

    4 条评论
  • Day71 - Terraform Interview Questions ????

    Day71 - Terraform Interview Questions ????

    1. What is Terraform and how it is different from other IaaC tools? HashiCorp's Terraform is an Infrastructure as Code…

  • Day 70 - Terraform Modules ????

    Day 70 - Terraform Modules ????

    Modules are containers for multiple resources that are used together. A module consists of a collection of .

    4 条评论
  • Day 69 - Meta-Arguments in Terraform ???

    Day 69 - Meta-Arguments in Terraform ???

    When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage…

    4 条评论
  • Day 65 - Terraform Resources ????

    Day 65 - Terraform Resources ????

    Understanding Terraform Resources A resource in Terraform represents a component of your infrastructure, such as a…

    2 条评论

社区洞察

其他会员也浏览了