Introduction
In today's DevOps learning journey, we will explore how to automate the creation and management of virtual machines (EC2 instances) on Amazon Web Services (AWS).
This blog will cover all the key topics in detail, including:
- Logging into AWS and launching EC2 instances
- Choosing the right terminal based on your OS
- Connecting to a VM and creating files
- Stopping and terminating instances to avoid unnecessary billing
- Configuring AWS CLI and access keys
- Interacting with AWS API using AWS CLI and CloudFormation
- Automating AWS tasks using Python and Boto3
- Installing and using AWS CLI
Let’s dive deep into each topic!
1. Logging into AWS and Launching EC2 Instances
AWS provides Elastic Compute Cloud (EC2), a scalable virtual server service.
Steps to Launch an EC2 Instance:
- Log in to the AWS Management Console.
- Navigate to EC2 Dashboard → Click "Launch Instance".
- Choose an Amazon Machine Image (AMI) (e.g., Ubuntu, Amazon Linux).
- Select an instance type (e.g - t3.micro for free tier).
- Configure security groups (allow SSH on port 22 for Linux).
- Launch the instance and download the key pair (.pem file) for SSH access.
🔹 Why automate this?
Manually launching instances is slow and error-prone. Automation ensures consistency and scalability.
2. Choosing the Right Terminal Based on Your OS
To connect to an EC2 instance, you need a terminal:
- Windows: Use Git Bash, PowerShell, or Windows Terminal.
- Mac/Linux: Use the built-in Terminal.
🔹 Example (SSH Command):
ssh -i "your-key.pem" ubuntu@
3. Connecting to a VM and Creating a File
Once connected via SSH, you can manage files:
Basic Linux Commands:
# Create a file
touch example.txt
# Edit the file
nano example.txt
# List files
ls
🔹 Why is this important?
Understanding basic file operations is crucial for scripting and automation.
4. Stopping and Terminating Instances to Avoid Billing
AWS bills for running instances. Always:
- Stop an instance if you want to restart it later (storage persists).
- Terminate to permanently delete it (storage is lost).
🔹 How to Stop/Terminate:
- Via AWS Console → EC2 Dashboard → Instance Actions.
-
Via AWS CLI:CopyDownload
bash
aws ec2 stop-instances --instance-ids i-1234567890 aws ec2 terminate-instances --instance-ids i-1234567890
⚠️ Warning: Terminating an instance cannot be undone!
5. Configuring AWS CLI and Access Keys
The AWS Command Line Interface (CLI) enables programmatic interaction with AWS services.
Steps to Configure AWS CLI:
- Install AWS CLI (see installation section below).
-
Run:bash
aws configure
-
Enter:
- AWS Access Key ID
- AWS Secret Access Key
-
Default region (e.g.,
us-east-1
) -
Default output format (e.g.,
json
)
🔹 Where to Get Access Keys?
- IAM Dashboard → Users → Security Credentials → Create Access Key.
6. AWS CLI vs. CloudFormation for AWS API Interaction
AWS CLI | AWS CloudFormation |
---|---|
Command-line tool for direct API calls | Infrastructure as Code (IaC) for templated deployments |
Best for quick tasks (e.g., start/stop instances) | Best for repeatable, scalable infrastructure |
Example:aws ec2 run-instances ...
|
Uses YAML/JSON templates for deployments |
🔹 When to Use Which?
- AWS CLI: Quick, one-off commands.
- CloudFormation: Managing complex, repeatable infrastructure.
7. Automating AWS Tasks Using Python (Boto3)
Boto3 is the AWS SDK for Python.
Example: Launching an EC2 Instance with Boto3
python
import boto3
ec2 = boto3.client('ec2')
response = ec2.run_instances(
ImageId='ami-0abcdef1234567890', # Replace with your AMI
InstanceType='t2.micro',
KeyName='your-key-pair',
MinCount=1,
MaxCount=1
)
print(response)
🔹 Why Boto3?
- Enables full automation of AWS tasks.
- Can be integrated into CI/CD pipelines.
8. Installing and Using AWS CLI
Installation:
-
Linux/macOS:bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install
Windows: Download from AWS CLI Install Guide.
Basic AWS CLI Commands:
bash
# List all EC2 instances
aws ec2 describe-instances
# Create a new key pair
aws ec2 create-key-pair --key-name MyKeyPair
# Delete a key pair
aws ec2 delete-key-pair --key-name MyKeyPair