Home Blogs Launch an EC2 instance and EBS volume, and attach the volume to the instance using the CLI

Launch an EC2 instance and EBS volume, and attach the volume to the instance using the CLI

by Anup Maurya
13 minutes read
Launch an EC2 instance and EBS volume, and attach the volume to the instance using the CLI

Launch an EC2 instance and an EBS volume, and then attach the volume to the instance using the AWS Command Line Interface (CLI).

Step 1: Install and Configure the AWS CLI

If you haven’t already, install and configure the AWS CLI on your machine. You can refer to the official AWS CLI documentation for instructions on installation and configuration.

Installation Guide for AWS CLI for Linux , Windows and Mac Machine .

Step 2: Launch an EC2 Instance

Use the following AWS CLI command to launch an EC2 instance:

aws ec2 run-instances --image-id <image_id> --instance-type <instance_type> 

Replace `<image_id>` with the ID of the Amazon Machine Image (AMI) you want to use for the instance. Specify the `<instance_type>` based on your requirements. Provide the `<key_pair_name>` that corresponds to an existing key pair for SSH access. Set `<security_group_id>` to the ID of the security group allowing inbound SSH access, and `<subnet_id>` to the ID of the subnet where you want to launch the instance.

Step 3: Create an EBS Volume

Next, create an EBS volume using the following command:

aws ec2 create-volume --availability-zone ap-south-1b --volume-type gp2 --size 1

Specify the `<availability_zone>` where you launched the EC2 instance. Set the `<size>` of the volume in GiB according to your needs.

Step 4: Attach the Volume to the Instance

To attach the EBS volume to the EC2 instance, use the following command:

aws ec2 attach-volume --instance-id i-0f7145d73c31fa51 --volume-id vol-023e4f786db78b76 --device /dev/sda1

Replace `<volume_id>` with the ID of the EBS volume created in the previous step. Specify the `<instance_id>` of the EC2 instance you launched. Set the `<device_name>` to the desired device name such as `/dev/xvdf`.

That’s it! You have now launched an EC2 instance, created an EBS volume, and attached the volume to the instance using the AWS CLI. You can further explore the AWS CLI documentation to learn about additional options and functionalities available for EC2 and EBS.

related posts

Leave a Comment