Introduction
Amazon Web Services (AWS) provides cloud-based virtual machines (VMs) known as EC2 (Elastic Compute Cloud) instances. These instances allow users to deploy and run applications on the cloud. This blog will cover:
Creating an EC2 instance in AWS
Writing scripts to automate EC2 management
Installing and configuring AWS CLI
1. Creating an EC2 Instance in AWS
1. Log in to AWS Console
Navigate to AWS Console
Sign in with your credentials.
2. Launch an EC2 Instance
Search for EC2 in the AWS services search bar and click on EC2.
Click on the dashboard and Launch Instance.
Enter an instance name.
Choose an Amazon Machine Image (AMI), such as Ubuntu, Amazon Linux, or Windows Server.
Select an instance type (e.g.,
t2.micro
for free-tier users).Create and Configure Key Pair
The key pair is crucial for accessing your EC2 instance securely.
Without the private key file, you cannot SSH into the instance, and access will be lost.
If you already have a key pair, select an existing one. Otherwise, create a new key pair:
Click on Create new key pair.
Provide a name for the key pair.
Choose the key format (
.pem
for Linux/macOS or.ppk
for Windows ).Download and store the key file securely.
Note :- Once you Delete or lose the file you cant access into your ssh and also you wont able to download it again. so make sure to keep it safe in a folder
Set up the security group (allow SSH, HTTP, or other required ports).
Click Launch Instance.
3. Connect to the EC2 Instance
Once the instance is running:
Click on the instance and find the Public IPv4 DNS.
Open a terminal and connect via SSH:
ssh -i /path/your-key.pem ubuntu@your-ec2-ip
(Replace
your-key.pem
with your actual key file andyour-ec2-ip
with the instance's IP.)
NOTE :- If it Denies Permission then Run this command
chmod 600 /path/your-key.pem
Writing Scripts for EC2 Automation
Types of Scripts for AWS Automation
Shell Scripts (Bash for Linux/macOS, PowerShell for Windows)
AWS CLI Scripts (Command-line automation)
Python Scripts (Boto3) (Programmatic access using AWS SDK)
TypeScript Scripts (AWS SDK for JavaScript/Node.js) (For modern cloud automation)
Installing and Configuring AWS CLI
1. Install AWS CLI
For Linux & MacOS
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
For Windows
Download and install AWS CLI from AWS CLI Installer.
2. Verify Installation
Check if AWS CLI is installed:
aws --version
To get your AWS Access Key ID and Secret Access Key, follow these steps:
1. Sign in to AWS Management Console
Sign in with your AWS account.
2. Create a New Access Key
For Root User
Click on your username (top-right corner) → Security credentials.
Scroll down to Access Keys → Click Create access key.
Download the Access Key ID and Secret Access Key (this will not be shown again).
3. Store Your Credentials Securely
The Secret Access Key will only be shown once.
Save it in a secure location (e.g., AWS Secrets Manager, environment variables, or a password manager).
4. Configure AWS CLI
After installation, configure AWS CLI with your credentials:
aws configure
AWS Access Key ID
AWS Secret Access Key
Default region name (e.g.,
us-east-1
)Default output format (json, table, or text)
Conclusion
This guide covered:
How to create an EC2 instance in AWS
Importance of key pairs and how to create them
Installing and configuring AWS CLI
Using AWS CLI and automation scripts, you can efficiently manage EC2 instances and scale your infrastructure with ease.