Creating Key Pair, Security group and launching EC2 Instance with external EBS Volume attached using AWS CLI
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. AWS Command Line Interface ( AWS CLI ) allows us to interact with its web-services platform in a more flexible way. It comes with multiple number of commands which we as users can use and implement in a more customized way and get rid of the burden of doing things manually.
TASK DESCRIPTION:-
* Create a key pair
* Create a security group
* Launch an instance using the above created key pair and security group.
* Create an EBS volume of 1 GB
* The final step is to attach the above created EBS volume to the instance you created in the previous steps
Tools Required:-
1. AWS command line interface
2.Terraform
Firstly check whether AWS CLI is installed or not using the following command
aws --version
AWS CLI version has successfully installed in our Windows . Now, we need to login to our AWS account from the command line using the command given below:-
aws configure
Now, create a new key pair "arthtaskkey" using CLI
aws ec2 create-key-pair --key-name "arthtaskkey"
Create Security group " arthsg" using the following command :-
aws ec2 create-security-group --group-name arthsg --description "Allow SSH"
To Set Inbound or Ingress Rule to authorize the outside traffic ,we use the security group id of the above group using the following command:-
aws ec2 authorize-security-group-ingress --group-id sg-0f852fc29b7c737cf --protocol tcp --port 22 --cidr 0.0.0.0/0
We can see the above Information about Security Group in CLI using the following command:-
aws ec2 describe-security-group --group-id sg-0f852fc29b7c737cf
Now, launch EC2 instance with the help of following command:-
aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --key-name arthtaskkey --security-group-ids sg-0f852fc29b7c737cf --count 1
We have to create one EBS Volume and attach it to the EC2 Instance that we have launched
aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone ap-south-1a
Attach EBS Volume to EC2 Instance using the following command:-
aws ec2 attach-volume --volume-id vol-006ec1c8ea4d80926 --instance-id i-0e62ed895633249b3 --device /dev/sdh
To get the output of the command in other formats like in my case I have used yaml format thus use --output parameter
aws ec2 describe-instances --output
Finally, we have to terminate the EC2 Instance and detaching the EBS Volume with the help of following commands :-
aws ec2 detach-volume --volume-id vol-0d1ff11448d823e5f
aws ec2 terminate-instances --instance-ids i-042a78baa35a391d1