Complete Guide: Launching EC2 Instances, Attaching EBS Volumes, and Creating SNS Topics with AWS CLI and API

ByteBusterX
4 min readJul 16, 2023

--

  1. Launching an EC2 Instance and an EBS Volume, and Attaching the Volume to the Instance using the AWS Command Line Interface (CLI):

Step 1: Launch an EC2 instance:

aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type t2.micro --key-name my-key-pair --security-group-ids sg-xxxxxxxx --subnet-id subnet-xxxxxxxx
AWS cli running instance

Replace ami-xxxxxxxx with the appropriate Amazon Machine Image (AMI) ID, my-key-pair with the name of your key pair, sg-xxxxxxxx with the security group ID, and subnet-xxxxxxxx with the subnet ID.

Step 2: Create an EBS volume:

aws ec2 create-volume --availability-zone ap-south-1a --size <gb> --volume-type gp2 --region <your_region>
Creating EBS volume

Replace ap-south-1a with the desired availability zone and 10 with the desired size in GB.

volume created

Step 3: Attach the EBS volume to the EC2 instance:

aws ec2 attach-volume --volume-id vol-xxxxxxxx --instance-id i-xxxxxxxx --device /dev/sdf
Attaching EBS to EC2

Replace vol-xxxxxxxx with the volume ID created in the previous step, i-xxxxxxxx with the instance ID of the launched EC2 instance, and /dev/sdf with the desired device name.

Note: remeber always make volume in same availblity zone

Checking the status of Instant launched
showing attached

2. Launching an EC2 Instance and an EBS Volume, and Attaching the Volume to the Instance Programmatically using API (boto3):

Here’s an example Python code snippet using the boto3 library to achieve the same

import boto3
from boto3 import Session
from boto3 import ec2


# Replace these values with your actual AWS access key ID and secret access key
aws_access_key_id = 'xxxxxxxxxxxxxxxxxxx'
aws_secret_access_key = 'xxxxxxxxxxxxxxxxx'
aws_region = 'ap-south-1' # Replace with your desired AWS region


# Initialize the EC2 client with your credentials and region
ec2 = boto3.client(
'ec2',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=aws_region,
)

# Specify the desired availability zone
availability_zone = 'ap-south-1a'

# Launch an EC2 instance in the specified availability zone
instance = ec2.run_instances(
ImageId='ami-03b31136fc503b84a',
InstanceType='t2.micro',
MinCount=1,
MaxCount=1,
Placement={'AvailabilityZone': availability_zone}
)

# Get the instance ID
instance_id = instance['Instances'][0]['InstanceId']

# Wait for the instance to be running
ec2.get_waiter('instance_running').wait(InstanceIds=[instance_id])

# Create an EBS volume in the same availability zone as the EC2 instance
volume = ec2.create_volume(
AvailabilityZone=availability_zone,
Size=2 # Volume size in GiB
)

# Get the volume ID
volume_id = volume['VolumeId']

# Wait for the volume to be available
ec2.get_waiter('volume_available').wait(VolumeIds=[volume_id])

# Attach the volume to the instance
ec2.attach_volume(
Device='/dev/sdf', # The device name to attach the volume to
InstanceId=instance_id,
VolumeId=volume_id
)
Run without any error on VsCode
Created EC2 instance
EBS volume created
Created volume attached

Make sure to replace the placeholder values with the appropriate values for your use case.

Note: Make sure the AZ of the both EC2 and EBS will be same neither it can’t be attached for example in this code we used ap-south-1a .

3. Creating an SNS (Simple Notification Service) using the AWS Command Line Interface (CLI):

To create an SNS topic using the AWS CLI, follow these steps:

Step 1: Create an SNS topic:

aws sns create-topic --name MyTopic
SNS topic

Replace MyTopic with the desired name for your SNS topic.

Step 2: Subscribe to the SNS topic:

aws sns subscribe --topic-arn arn:aws:sns:ap-south-1:123456789012:MyTopic --protocol email --notification-endpoint your-email@example.com
example

Replace arn:aws:sns:ap-south-1:123456789012:MyTopic with the ARN (Amazon Resource Name) of your SNS topic, and your-email@example.com with your email address.

Step 3: Publish a message to the SNS topic:

aws sns publish --topic-arn arn:aws:sns:ap-south-1:123456789012:MyTopic --message "Hello, world!"
publishing message

Replace arn:aws:sns:ap-south-1:123456789012:MyTopic with the ARN of your SNS topic, and "Hello, world!" with the desired message.

Message recieved

These commands assume you have AWS CLI properly configured with valid credentials and the necessary permissions to create SNS topics.

--

--

ByteBusterX
ByteBusterX

Written by ByteBusterX

"Tech enthusiast exploring cybersecurity and networking. Sharing insights through the power of words. Join me in the world of tech and discovery. 📚✍️

No responses yet