Provisioning with AWS involves setting up and managing cloud resources on Amazon Web Services, a leading cloud computing platform. AWS offers a wide range of services, from virtual servers (EC2) to databases (RDS), enabling users to create, configure, and scale their infrastructure as needed. This process can be done through the AWS Management Console, CLI, or Infrastructure as Code (IaC) tools like AWS CloudFormation. It allows businesses and individuals to leverage the power of the cloud without the hassle of managing physical hardware, making it easier to build and scale applications and services.
What is AWS-CLI?
- The AWS CLI (Amazon Web Services Command Line Interface) is a versatile a
command-line
tool that enables users to interact with and manage a wide range of AWS services directly from the command line or throughscripts
. It facilitates tasks such asresource provisioning
,application deployment
, andsecurity management
, making it invaluable for developers, administrators, and DevOps professionals. AWS CLI isplatform-agnostic
,supporting Windows
,macOS
, andLinux
, and offers features for access control and customizable output formats, empowering users to efficiently automate and control their AWS resources and workflows.
Installation of AWS CLI on EC2 Instance.
- To install the AWS CLI on an EC2 instance, you must first connect to the instance using SSH. Once connected, you can install the AWS CLI using the following command:
sudo apt update
sudo apt install unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
- After Installation Check the version of AWS CLI using the following command:
aws --version
What is an IAM User?
An IAM (Identity and Access Management) user in
Amazon Web Services (AWS)
is an entity representing an individual, system, or application within an AWS account, granting them unique credentials and permissions to access and manage AWS resources securely. IAM users help enforce the principle of least privilege by ensuring users and systems only have the necessary permissions, enhancing overall account security and access control.Step 1: Create an IAM User in AWS Console for that go to Services search for IAM and click on it to Create a User.
- Step 2: Specify the User Name select the Access Type as Programmatic Access and click on Next: Permissions.
- Step 3: Select the Attach Policies Directly and add the policy AmazonEC2FullAccess click on Next: Tags and then click on Next: Review.
- Step 4: Review the details and click on Create User.
- Step 5: Now Click on User go to Security Credentials and click on Create Access Key.
- Step 6: Download the .csv file and save it on your local machine.
- Step 7: Now Configure the AWS CLI using the following command:
aws configure
Let's Install the required providers for Terraform.
# Define required providers and their versions.
terraform {
required_providers {
# Declare the AWS provider with its source and version constraints.
aws = {
source = "hashicorp/aws"
version = "~> 4.16" # Use version 4.x of the AWS provider.
}
}
# Set the minimum required Terraform version.
required_version = ">=1.2.0"
}
# Configure the AWS provider with your desired region.
provider "aws" {
region = "ap-south-1" # Specify the AWS region (e.g., Asia Pacific - Mumbai).
}
Task 1: Provision an AWS EC2 instance using Terraform.
# Declare required providers and their versions.
terraform {
required_providers {
# Define the AWS provider with its source and version constraints.
aws = {
source = "hashicorp/aws"
version = "~> 4.16" # Use version 4.x of the AWS provider.
}
}
# Specify the minimum required Terraform version.
required_version = ">=1.2.0"
}
# Configure the AWS provider with the desired region.
provider "aws" {
region = "ap-south-1" # Set the AWS region to Asia Pacific - Mumbai.
}
# Define AWS EC2 instances.
resource "aws_instance" "example_instance" {
count = 3 # Create 3 instances with the same configuration.
ami = "ami-0f5ee92e2d63afc18" # Use a specific AMI.
instance_type = "t2.micro" # Specify the instance type.
vpc_security_group_ids = ["sg-0c168b0d362f5a2b4"] # Attach a security group to instances.
connection {
type = "ssh"
user = "ubuntu" # SSH user for connecting to instances.
private_key = file("/home/ubuntu/apache.pem") # Path to the private key for SSH.
}
}
- Edit the main.tf file and paste the above code in it.
- Now run the following command to initialize the Terraform.
terraform init
- Now run the following command to check the execution plan.
terraform plan
- Now run the following command to apply the changes.
terraform apply
- Check the created instances on CLI.
- Check the created instances on AWS Console.