Create a EC2 Instances using key pair and security groups and attach the EBS volume to existing Instances using AWS CLI
--
This blog aims to explain the process of provisioning an AWS EC2 Instance with an external EBS Volume created & attached to it using the AWS CLI Package!
In the Dynamic & Rapid World Today, Automation has created its own image, & to survive in this world, it is very important to know the automation part.
The very first step towards automation is using the CLI of most of the programs because it gives one a real scripting feel. One more benefit of using CLI instead of WebUI is it consumes less internet bandwidth & resources. In addition to that, it saves a lot of time, if commands are known to you.
Therefore, to learn CLI in this world is a must & this blog is written to teach you some of the basics of the AWS CLI Package, which is the official CLI for AWS.
Prerequisite for this task!
- Familiar with AWS basic services.
At the end of this blog, you will be able to create a key-pair for AWS EC2 instances, create a security group that will be attached to EC2 Instances, Launch an EC2 Instance, create one extra EBS Volume, & attach that to the EC2 Instance created.
Let’s start building the setup using AWS CLI:
First of all, to create a key-pair, execute the command given below.
aws ec2 create-key-pair --key-name <name of the key> > <name of the Key File>.pemFor example: “aws ec2 create-key-pair --key-name CLIKey > CLIKey.pem”
The above-shown command will create one KeyPair with the name mentioned in the command, & save the Key into the file that is mentioned in the command. This KeyPair will be used in the future to access the EC2 Instance
Now, let’s create one security group which will be attached to the EC2 Instance which will be created in the next step.
To create a security group, execute the command given below.
aws ec2 create-security-group --group-name <name of the Security Group> --description “Any Description abou the security group“For example: aws ec2 create-security-group --group-name CLISG --description "Basic Security group created using AWS CLI! "
The above-shown command will create one Security Group with the name & description mentioned in the command.
After the creation of KeyPair & Security Group, let us create EC2 Instance now.
To create the same, execute the command shown below.
aws ec2 run-instances --image-id <AMI Image ID> --key-name <Key Name> --instance-type <Type Of Instance> --security-groups <Name of the Security Group> --tag-specifications 'ResourceType=instance,Tags=[{Key=<Any Key>,Value=<Any Value>}]
'For Example: aws ec2 run-instances --image-id ami-052c08d70def0ac62 --key-name CLIKey --instance-type t2.micro --security-groups CLISG --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=CLI-EC2-Instance}]
The above-shown command will create one AWS EC2 Instance with the specified resources.
Now, let us create one extra EBS Volume for persistent Storage.
To accomplish the same, execute the command given below.
aws ec2 create-volume --size <Size in GiB> --availability-zone <Name of the Availability Zone where EC2 Instance is launched>For Example: aws ec2 create-volume --size 1 --availability-zone ap-south-1a
The above-shown command will create one EBS Volume of size 1 GiB in availability zone “ap-south-1a”.
As a final step for this blog, let us attach this volume to the EC2 Instance which we have already created above.
To accomplish the same, execute the command given below.
aws ec2 attach-volume --volume-id <Volume ID to be attached> --instance-id <Instance ID to which this volume has to be attached> --device <Device Name>For Example: aws ec2 attach-volume --volume-id vol-0c266c523d78050de --instance-id i-0c7ef601cda90170c --device /dev/xvdh
The above-shown command will attach the created EBS Volume to the AWS Instance.
After running the command, the output will be like this:
The above-shown command will attach the EBS Volume created & will attach that to the EC2 Instance!
I hope my article explains each and everything related to the topic with all the deep concepts and explanations. Thank you so much for investing your time in reading my blog & boosting your knowledge. If you like my work, then I request you to give an applaud to this blog!