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.

1.Create an EC2 instance.

2.SSH into EC2 instance

3.Install AWS CLI

sudo apt install awscli        
No alt text provided for this image

Check aws cli version

aws --version        

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.

Create Access key for IAM user.

Click on 'Create access key'

No alt text provided for this image

Select 'command line interface'

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

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

The?terraform?block defines the version of Terraform that is required to execute this configuration. In this case, it specifies that the Terraform version must be?>= 1.2.0.

The?required_providers?block declares the AWS provider and its version that Terraform will use for the resources defined in this configuration. In this case, it declares the AWS provider with the source?hashicorp/aws?and specifies that the version of the provider should be?~> 4.16, which means any version of the AWS provider greater than or equal to 4.16 and less than 5.0 will be acceptable.

Add the region where you want your instances to be

provider "aws" {
region = "ap-south-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-0f8ca728008ff5af4"
        instance_type = "t2.micro"
        tags = {
            Name = "TerraformTestInstance"
  }
}        

The resource block has a resource type of "aws_instance" and a resource name of "aws_ec2_demo". The count parameter is set to 2, which means that two instances will be created.

The ami parameter specifies the Amazon Machine Image (AMI) to use for the instances. In this case, the AMI ID is "ami-0f8ca728008ff5af4".

The instance_type parameter specifies the type of instance to create. In this case, the instance type is "t2.micro".

The tags parameter specifies metadata to attach to the instance, in this case, a tag named "Name" with the value "TerraformTestInstance".

No alt text provided for this image

first initialize the working directory with the necessary plugins and modules by executing?terraform init

No alt text provided for this image

It will create an execution plan by analyzing the changes required to achieve 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 apply the changes to create or update resources as needed with?terraform apply.

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

You can check two instances created using terraform.

Thanks For Reading!!!!!!

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

Anup D Ghattikar的更多文章

  • Django Rest Framework

    Django Rest Framework

    Introduction to Django Rest Framework Django REST framework (DRF) is a powerful and elegant toolkit built on top of the…

    11 条评论
  • Docker Tool Mastering

    Docker Tool Mastering

    What is Docker? Docker is a platform and toolset that simplifies the process of developing, deploying, and running…

    4 条评论
  • Devop's Bits & Byte's - By Anup Ghattikar

    Devop's Bits & Byte's - By Anup Ghattikar

    Medium Link: https://medium.com/@aghattikar82/devops-bits-bytes-by-anup-ghattikar-9a8c6b788ec9 HashNode Link:…

    10 条评论
  • Kubernetes Troubleshooting

    Kubernetes Troubleshooting

    Cluster Management Display endpoint information about the master and services in the cluster kubectl cluster-info…

    3 条评论
  • Kubernetes Cluster Maintenance

    Kubernetes Cluster Maintenance

    Kubernetes Cluster Upgrade Upgrade master Upgrading the control plane consist of the following steps: Upgrade kubeadm…

    2 条评论
  • Day 05:Kubernetes Storage Kubernetes Security

    Day 05:Kubernetes Storage Kubernetes Security

    Persistent Volumes Managing storage is a distinct problem from managing compute instances. The PersistentVolume…

  • Kubernetes services and service discovery

    Kubernetes services and service discovery

    Kubernetes is a powerful platform for deploying, scaling, and managing containerized applications. However, once you…

  • Day 03:Kubernetes Workloads

    Day 03:Kubernetes Workloads

    Kubernetes Deployment with YAML YAML (which stands for YAML Ain’t Markup Language) is a language used to provide…

    2 条评论
  • Kubernetes Networking

    Kubernetes Networking

    Topics Are: Services, Ingress, Network Policies, DNS, CNI Plugins Services Services provide a way to expose a set of…

    7 条评论
  • 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 .

    2 条评论

社区洞察

其他会员也浏览了