Starting AWS EC2 Instance with the help of AWS CLI

 

Starting AWS EC2 Instance with the help of AWS CLI



This blog will discuss how to launch an EC2 instance and connect an EBS volume using AWS CLI. Amazon Web Services (AWS) provides a robust set of command-line tools that enable you to automate your infrastructure and manage your resources efficiently.

Let’s get started!

STEP 1: Install and Configure the AWS CLI

download and run the AWS CLI MSI installler for windows (64-bit)

msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi
No alt text provided for this image

To check/confirm the installation, run the “aws — version” command in your terminal


STEP 2: Let's Authenticate our AWS account using the IAM user we created, provide the Access key, secret key & Region, using this cmd.

aws configure
No alt text provided for this image

STEP 3: Create a key pair

To create a new key pair using AWS CLI, you can use the following command:

aws ec2 create-key-pair --key-name test2
No alt text provided for this image
No alt text provided for this image

STEP 4: Create a security group

To create a new security group using AWS CLI, you can use the following command:

aws ec2 create-security-group --description "SSH protocol allowed" --group-name myawssg
No alt text provided for this image
No alt text provided for this image

Step 5: Allowing SSH through the firewall

aws   ec2   authorize-security-group-ingress  --group-id  sg-0b7e72437431ef58e  --protocol tcp --port 22 --cidr 0.0.0.0/0
No alt text provided for this image

Step 6: Verifying Security group from CLI

# aws ec2 describe-security-groups --group-id sg-0b7e72437431ef58e
No alt text provided for this image

STEP 7: Launch your instance

now that you have created a key pair and security group, you can launch your ec2 instance. you can use the following command

aws ec2 run-instances --security-group-ids sg-0b7e72437431ef58e --instance-type t2.micro  --count 1  --image-id ami-0d81306eddc614a45   --key-name test2
No alt text provided for this image
No alt text provided for this image

This command will launch an EC2 instance with the Amazon Linux 2 AMI, using the t2.micro instance type.


STEP 8: Create an EBS Volume

Now, after launching the instance, we are creating a new Volume of 1 GiB

to create a new EBS volume using AWS CLI, you can use the following command

# aws ec2 create-volume --availability-zone ap-south-1a --volume-type gp2 --size 1
No alt text provided for this image

STEP 9: Attach the EBS Volume to the Instance

Once the EBS volume is created, you can attach it to the instance using the following command

aws  ec2   attach-volume   --instance-id  i-06f4d5ac53af98dff   --volume-id  vol-015fa2bea0195d5b9  --device   /dev/sda1
No alt text provided for this image
No alt text provided for this image

This command will attach the EBS volume with the ID “vol-015fa2bea0195d5b9” to the instance with the ID “i-06f4d5ac53af98dff” as device /dev/sdf.

And that’s it! You have now created a key pair and security group, launched an EC2 instance and attached an EBS volume to it using AWS CLI.


Step 10: Verifying EBS attachment

No alt text provided for this image

We are all done now!

Conclusion

So, In this blog, we saw a few easy steps on how we can launch ec2 instances using AWS CLI. Thanks for being with me till the end. If you find this blog helpful do share it with your friends.

THANK YOU!!!

Comments

Post a Comment