Automating AWS Instance Launch & EBS Volume Attachment with CLI Commands

KRUSHNA PRASAD SAHOO
AWS Tip
Published in
4 min readMar 9, 2023

--

This guide provides a step-by-step approach to automate the process of launching an Amazon Linux instance with a key pair and security group, and attaching an EBS volume to it using AWS CLI commands.

Amazon Web Services (AWS) is a powerful cloud computing platform that provides a wide range of services to help individuals and organizations build and run their applications. The AWS Command Line Interface (CLI) is a powerful tool that allows you to interact with AWS services from your command line, making it easier to automate tasks and manage your resources.

In this guide, I will walk you through the steps to launch an Amazon Linux instance with a key pair and security group, as well as attach an EBS volume to it using AWS CLI commands.

Step 1: Download & install AWS CLI in your system.

To install AWS CLI, you can follow the steps mentioned in the official AWS documentation. If you have already AWS CLI installed in your system you can skip this step.

Step 2: Configure AWS CLI to use your account in AWS.

To configure AWS CLI, you can run the following command and provide your AWS Access Key ID and Secret Access Key when prompted.

> aws configure

Step 3: Create a key pair “my-aws-test-key” and a security group “my-aws-test-sg” in us-east-2 region.

Considering “us-east-2" region, I will be creating a key with name “my-aws-test-key” and a security group naming “my-aws-test-sg” in this step. You can go with any region & naming convention as per your choice.

  1. To create a key pair, run the following command:
> aws ec2 create-key-pair --key-name my-aws-test-key --region us-east-2 --query 'KeyMaterial' --output text > my-aws-test-key.pem

This command will create a key pair named “my-aws-test-key” in the “us-east-2” region and save the private key in a file named “my-aws-test-key.pem”.

2. To create a security group, run the following command:

> aws ec2 create-security-group --group-name my-aws-test-sg --description "My AWS Test Security Group" --region us-east-2

This command will create a security group named “my-aws-test-sg” in the “us-east-2” region.

Step 4: Launch an Amazon Linux instance with a name “my-aws-test-instance” using t2.micro, the key & security group just created.

To launch an Amazon Linux instance, run the following command :

> aws ec2 run-instances --image-id ami-0c94855ba95c71c99 --count 1 --instance-type t2.micro --key-name my-aws-test-key --security-group-ids sg-3124f321 --region us-east-2 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=my-aws-test-instance}]'

This command will launch an Amazon Linux instance with the instance type “t2.micro”, the key pair “my-aws-test-key”, and the security group “my-aws-test-sg” in the “us-east-2” region. It will also add a tag to the instance with the name “my-aws-test-instance”.

Step 5: Create a EBS Volume with name “my-aws-test-volume”and attach the created EBS volume to that instance just created.

  1. To create an EBS volume, run the following command :
> aws ec2 create-volume --size 1 --availability-zone us-east-2a --tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=my-aws-test-volume}]' --region us-east-2

This command will create an EBS volume with a size of 1 GB in the “us-east-2a” availability zone. It will also add a tag to the volume with the name “my-aws-test-volume”.

2. To attach the created EBS volume to the instance, run the following command:

> aws ec2 attach-volume --volume-id vol-2d127843 --instance-id i-09bc055cbcbdfba9b --device /dev/xvdf --region us-east-2

This command will attach the EBS volume to the instance at the device path “/dev/xvdf”.

congooo!!

Congratulations, you have completed the task of launching an Amazon Linux instance with a key pair and security group, as well as attaching an EBS volume to it using AWS CLI commands!

In this guide, I’ve covered the steps required to launch an Amazon Linux instance with a key pair and security group, and attach an EBS volume to it using AWS CLI commands. By following these steps, you can easily automate the process of launching instances and managing your resources on AWS. Whether you’re a seasoned AWS user or just getting started, the AWS CLI is a powerful tool that can help you streamline your workflows and make managing your resources faster and more efficient.

--

--