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:
use command:
sudo apt install awscli
Confirm and check the AWS CLI 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
Make an access key for the IAM user.
Select Create Access Key
Select Command Line Interface
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>
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"
}
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"
First, run terraform init to create the working directory with the appropriate plugins and modules.
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.
Finally, it will use terraform apply to construct or update resources as needed.
You can verify there will be two ec2 instances in your AWS console
I appreciate your reading. I am sure it will benefit you. ?????
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