[AWS CLI] Launch an EC2 instance and attach an EBS volume to it

[AWS CLI] Launch an EC2 instance and attach an EBS volume to it

Here is a step by step guide to how to launch an amazon image Instance using AWS CLI and attach a volume to it.

There a few things you need to get ready before using AWS CLI on your operating system.

  1. An IAM user account with required permission such as PowerUserAccess.
  2. AWS installed in the Operating System.

LAUNCHING AWS INSTANCE

STEP 1: Configure AWS and log to the user you need to Enter the Access key ID, Access key, region name, and output format (JUST PRESS ENTER IT WILL TAKE THE DEFAULT VALUE).

No alt text provided for this image

STEP 2: Next step is to create a key pair and note the "KeyName" and "KeyPairId"

aws ec2 create-key-pair --key-name <PutYourKeyNameHere>
No alt text provided for this image

and check the info using

aws ec2 describe-key-pairs
No alt text provided for this image

STEP 3: Now the next step is to create a security group

BELOW IMAGE SHOWS THE GENERAL SYNOPSIS

No alt text provided for this image

Now to create the security group use

aws ec2 create-security-group --group-name <EnterAnyName> --description "<YourDescription>"
No alt text provided for this image

Now I will add SSH access to the security group using the command

aws ec2 authorize-security-group-ingress --group-id sg-06af7b6d08b0c9cc3 --protocol tcp --port 22 --cidr 0.0.0.0/0

STEP 4: Next we will launch an instance

Here is the synopsis to launch an instance on AWS CLI

No alt text provided for this image

Use the below command to launch the instance

aws ec2 run-instances --image-id <Image ID e.g. ami-0e306788ff2473ccb> --count <countINT> --instance-type <InstanceType e.g. t2.micro> --key-name <YourKeyPairName> --security-group-ids <YourSecurityGroupID> --subnet-id <YourSubnet>
No alt text provided for this image

Hurray you're done launching an instance on AWS using AWS CLI

ATTACHING VOLUME TO THE INSTANCE

Now we need to create a volume and attach it to the instance that we have created and attach the volume to the instance.

Here I will create a volume of 1 GiB and attach it to the instance ID

Here is the synopsis of the create volume command

No alt text provided for this image

STEP 1: To create a volume (I'm creating a volume of 1GiB in size)

aws ec2 create-volume --availability-zone <YOurAvailabilityZone> --volume-type gp2 --size <YourSizein INT>
No alt text provided for this image

STEP 2: attach the volume to the instance use command:

aws ec2 attach-volume --volume-id <YourVolumeID> --instance-id <YourInstanceID> --device /dev/sdf
No alt text provided for this image

And that's all you've successfully attached the volume to the instance.

No alt text provided for this image




Avadhut Shinde

Aspiring Software Engineer

4 年

Congratulations ?Suman Sourav

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了