AWS CLI Setup and Launch EC2 Instance with KeyPair and Security Group with AWS CLI

Ayush Kumar singh
2 min readOct 18, 2020

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.

Prerequisites

  • A 64-bit version of Windows XP or later.
  • Admin rights to install software

Download AWS CLI v2 on Your System

Windows: Download_Link
Linux: Read_Here
Mac: Read_Here

Configure AWS CLI

You Need Your AWS Access_key and Secret Key to configure AWS_CLI.

What is access_key or secret_key?
Access keys consist of an access key ID and secret access key, which are used to sign programmatic requests that you make to AWS. If you don’t have access keys, you can create them from the AWS Management Console.

How to Create Access Keys?
Check this AWS Official Documentation: Here

Setup Completed!

Now, Let see How to Create a KeyPair, Security Group with SSH access, an EBS Storage, Instance with Same KeyPair, SecurityGroup and EBS.

Creating A Key-Pair:

aws ec2 create-key-pair
--key-name keyName

--query 'KeyMaterial'
--output text >
MyKeyname.pem

This will save your Key to MyKeyname.pem file.

Creating a Security Group:

aws ec2 create-security-group 
--group-name my-securityGrp_name
--description “My security group Description” > securitygrp.txt

This Creates a Security Group with Name my-securityGrp_name with no Rules for Ingress and all traffic allowed for Egress.
Security Group ID is saved in securitygrp.txt

Adding Ingress Rules

aws ec2 authorize-security-group-ingress 
--group-name my-securityGrp_name
--protocol tcp
--port 22
--cidr 0.0.0.0/0

SSH rule Added to our Security Group.
For Egress Rules :
authorize-security-group-egress in the same Format

Creating an EBS Storage:

aws ec2 create-volume 
--availability-zone ap-south-1a
--size 10
--volume-type gp2 > ebsstorage.txt

New EBS Storage Made!
Volume ID saved in ebsstorage.txt

Launch Instance With Our Key, Security grp:

aws ec2 run-instances 
--image-id ami-052c08d70def0ac62
--instance-type t2.micro
--key-name awscli_test
--security-group-ids sg-0ea8bc926de524409
--subnet-id subnet-daaba8b2
--count 1
--associate-public-ip-address > instance_details.txt

Instance Created with our KeyPair, Security Grp.
All Details About Instance Are stored in instance_details.txt

Attach the EBS storage to Instance:

aws ec2 attach-volume 
--device /dev/sdh
--instance-id i-04da4f901953a2c79
--volume-id vol-0e70d0798c7815cb3

Our EBS Is Now attached to Instance!

Done! Enjoy AWS_CLI!

Thanks For Reading 🙏

Connect With Me:
LinkedIn: Here

--

--