Provision EC2 Instance With EBS Volume Using Terraform 1722666558
Provision EC2 Instance With EBS Volume Using Terraform 1722666558
Prerequites -
- Terraform configuration is created to define an EC2 instance and a 3 GB
EBS volume.
- The Terraform code is applied, and an EC2 instance is provisioned in
your AWS account with the any AMI and instance type of your choice.
- An EBS volume of 3 GB is created. - The EBS volume is successfully
attached to the EC2 instance.
Step 1: Verify the aws console-
1. Login to AWS Management Console:
2. Navigate to the EC2 service in the AWS Management Console.
3. Verify that the EC2 Instance is created.
Ankita Lunawat
6. Click on user and click on “Security credentials”.
7. Click on “Create access key”.
8. Select “Command Line Interface (CLI).
9. Tick the confirmation box, click Next.
10. After creating access key successfully, you’ll get the
credentials that you need to configure AWS on your Windows
machine.
Ankita Lunawat
Step 3: Install Terraform
1. Download Terraform.
2. Visit the Terraform downloads page.
(https://developer.hashicorp.com/terraform/install# windows)
3. Download the Windows 64-bit ZIP file. 2.Extract the ZIP file.
4. Right click on the downloaded zip file and select “Extract All”.
Command –
wget-O-https://apt.releases.hashicorp.com/gpg|sudogpg--dearmor-o
/usr/share/keyrings/hashicorp-archive-keyring.gpg
echo"deb[signed-by=/usr/share/keyrings/hashicorp-archive-
keyring.gpg] https://apt.releases.hashicorp.com$(lsb_release-
cs)mainsudo/etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Open a Command prompt and run the following command to verify
Terraform is installed successfully - terraform version
Ankita Lunawat
Step 4: To install the AWS CLI on Ubuntu
1: Update Your Package List
First, ensure your package list is up-to-date - #sudo apt update
2: Install Prerequisites
The AWS CLI requires unzip and curl, so make sure these are installed:
#sudo apt install unzip curl -y
3: Download the AWS CLI Bundle
Download the AWS CLI installation bundle using curl:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip"
-o "awscliv2.zip"
Ankita Lunawat
4: Unzip the Installation Bundle
Unzip the downloaded bundle - #unzip awscliv2.zip
5: Run the Install Script
Run the AWS CLI install script - sudo ./aws/install
6: Verify the Installation
Check that the AWS CLI is installed correctly by verifying its version:
aws –version
7: Configure the AWS CLI
To configure the AWS CLI with your credentials, run:
aws configure
Ankita Lunawat
Open a text editor (e.g., Notepad, Visual Studio Code).
Create a new file named ‘provider.tf’ and add the following
content.
Step 2: Write Your Terraform Configuration
Create a new file called main.tf and add the following code –
# main.tf
# Specify the provider
provider "aws" {
region = "us-west-2" # Replace with your preferred region
}
# Create a new EC2 instance
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Replace with an appropriate AMI
ID for your region
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}
# Output the public IP of the instance
output "instance_public_ip" {
value = aws_instance.example.public_ip
Ankita Lunawat
}
Save the file in the directory.
3: Initialize and Apply Terraform Configuration.
1. Initialize Terraform – terraform init
Ankita Lunawat
When prompted, type ‘yes’ to confirm.
Ankita Lunawat
And here, I have successfully Install the Terraform and Provision EC2
Instance with EBS Volume using Terraform using CLI.
Ankita Lunawat