Using AWS CLI to launch an EC2 Instance - TASK 3 (ARTH)
Hey Everyone ! In AWS CSA And Developer Training by Vimal Daga Sir, we, learners, are given a task.
Task Description - AWS
✔ 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.
All the above steps must be performed using AWS CLI.
---------------------------------------------------------------------------------------------------------------
So, let's start !!
Step 1 : Go to AWS CLI Documentation page and download it for Windows.
Step 2 : Create an IAM User in AWS Management Console. For this, you need an AWS account.
▶Go to services tab and search for IAM.
▶In IAM Dashboard, go to Users and click on Add User.
▶Give appropriate name (in capitals), select Programmatic Access and then click on Permissions.
▶Next, go to Attach existing policies directly and select AdministratorAccess.
▶Next, Add NAME Tag and give description like USERNAME in capitals.
▶Next, Click on Create User.
▶Next, Check whether user created successfully. If yes, download .csv file which contain Access Id and secret Access Id of your user.
Step 3 : Configuring AWS CLI in Windows.
▶Install the AWSCLIV2.msi file which we downloaded earlier.
▶Go to command prompt. Check aws version by command aws --version.
▶Type command aws configure.
▶Give Access Id and Secret Access Id from the .csv file which we downloaded earlier. In region give the aws region you are in and in output format give json.
Step 4 : Creating a Key Pair.
▶In command prompt, type the command aws ec2 create-key-pair --key-name ANYNAME
▶If we check in EC2 Dashboard in AWS Console, we will see our key pair enlisted there.
Step 5 : Creating a Security Group.
▶In command Prompt, type command aws ec2 create-security-group --group-name ANYNAME --description "DESCRIPTION"
▶We can check our created security group in AWS Console.
▶Then, type command aws ec2 authorize-security-group-ingress --group-name YOUR_SG_NAME --group-id YOUR_SG_ID --protocol tcp --port ANY_FREE_PORT --cidr 0.0.0.0/0 for authorizing security group with proper inbound rules(protocol, port number and IP).
Step 6 : Launching an EC2 Instance using above created key pair and security group
▶In command Prompt, type command aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --security-group-ids YOUR_SG_ID --key-name YOUR_KEY_NAME --subnet-id YOUR_SUBNET_ID
▶We can check our new instance in AWS Console.
Step 7 : Creating an EBS Volume of 1 GB
▶In command Prompt, type command aws ec2 create-volume --availability-zone YOUR_AVAILABILITY_ZONE --size 1 --volume-type gp2
▶We can check our newly created EBS Volume in AWS Console.
Step 8 : Attaching the above created EBS Volume to the Instance we created.
▶In command Prompt, type command aws ec2 attach-volume --instance-id YOUR_INSTANCE_ID --volume-id YOUR_VOLUME_ID --device /dev/sdh
▶We can now check the status of volume that we created earlier.
---------------------------------------------------------------------------------------------------------------
In this way, We can launch an EC2 Instance via AWS CLI and also attach VOLUME to it.
This marks the end of my TASK of AWS CLI.
Thanks for sharing informative article. Really appreciable 😊
Keeping EC2 instances in prime condition will be your bread and butter.