AWS CLI: LAUNCHING AND ATTACHING INSTANCE WITH EBS VOLUME

The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.
TASK DESCRIPTION
1. Create a key pair
2. Create a security group
3. Launch an instance using the above created key pair and security group.
4. Create an EBS volume of 1 GB.
5. The final step is to attach the above created EBS volume to the instance you created in the previous steps.
Steps to be followed for using AWS CLI
- Creating an account in AWS and log in using the root account
- Create an IAM account by going into services
- Click on add user, after giving the required user name and password click on “Next: Permissions”
- Click on add user to group and here I selected power user access permission. After that click on create group
- Click on attach, here adding tags is optional and click on “Next Review”
- Then after clicking on create user your IAM account will be created
- Here we will be using our command prompt in windows to logging into our account rather than WebUI to launch and run the instances.
- The first prerequisite for using this is that you should be having AWS CLI version 2 downloaded to your PC. This can be directly done by searching in google awscli download and clicking the link corresponding to it.
- Check whether the AWS CLI is installed or not using the following command → aws-version

How to login with AWS Account through CLI
- To use this, enter
aws configure
at the command line, and then press Enter. The AWS CLI outputs lines of text, prompting you to enter additional information. Enter each of your access keys in turn, and then press Enter. Then, enter an AWS Region name in the format shown, press Enter, and then press Enter a final time to skip the output format setting. The final Enter command is shown as replaceable text because there is no user input for that line.
# aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE AWS
Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY Default region name [None]: us-east-1
Default output format [None]: ENTER
2. Now you have logged in to AWS account

3. For commands which you are not sure you may take the help of aws help command to know about further commands
4. Create a key pair using the below mentioned command
aws ec2 create-key-pair --key-name <Key_NAME>
In my case I have given the key name as AWSKEY
So you will be getting a key pair with KeyName “AWSKEY” and KeyPairId.

It will be better to write it down in notepad which will be easier later while launching an instance
5. Create a security group using the below command
aws ec2 create-security-group --group-name MySecurityGroup --description "My security group"

Here I have created a security group named Task.
Now the next step will be the launching the instance but before that it will be better to write the ami id of the image we wish to launch better to write and keep everything in notepad also the SG id, key name, subnet_id etc.
6. To launch an Amazon EC2 instance using the AMI you selected, use the run-instances command. You can launch the instance into a virtual private cloud (VPC), or if your account supports it, into EC2-Classic.
Initially, your instance appears in the pending
state, but changes to the running
state after a few minutes.
$ aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-903004f8 --subnet-id subnet-6e7f829e
Output

After running this command we can check through WebUI weather it is working or not.

This was the instance which i created in N. Virginia region
7. Creating a volume
aws ec2 create-volume --volume-type gp2 --size 1--availability-zone us-east-1a
output
{
"AvailabilityZone": "us-east-1a",
"Tags": [],
"Encrypted": true,
"VolumeType": "gp2",
"VolumeId": "vol-1234567890abcdef0",
"State": "creating",
"Iops": 100,
"SnapshotId": "",
"CreateTime": "YYYY-MM-DDTHH:MM:SS.000Z",
"Size": 1
}
It can also be seen from WebUI that our volume has been created.
8. Attaching the EBS volume using the command
aws ec2 attach-volume --volume-id vol-1234567890abcdef0 --instance-id i-01474ef662b89480 --device /dev/sdfOutput{
"AttachTime": "YYYY-MM-DDTHH:MM:SS.000Z",
"InstanceId": "i-01474ef662b89480",
"VolumeId": "vol-1234567890abcdef0",
"State": "attaching",
"Device": "/dev/sdf"
}
To confirm you can go into volume and then in description and see that the volume have been attached to your instance.
Thanks for reading!!
Hoping for your valuable comments…..