Basic Guide to launch AWS EC2 instance using CLI

Pankhuri Sharma
5 min readOct 25, 2020

NOTE: In this article, 2 hyphens back to back are printed as “ — “.

AWS CLI

To communicate with the AWS services we have 3 ways:

  • WebUI: It is using of AWS Management Console
  • SDK: It is customized with code
  • AWS CLI: The AWS Command Line Interface (AWS CLI) is an open-source tool that enables you to interact with AWS services using commands in your command-line shell.

There are 2 versions of AWS CLI, I am using the latest version Version 2. To use this tool, firstly we need to download it & to check whether it is successfully installed using “aws — version”

To work on the AWS CLI, we need to do firstly know a little bit about what we want to do & we can learn AWS services with the help of AWS Management Console when we are comfortable with it, switch to CLI. To get used to working on the CLI, we need to know the 3 basic commands, which will help:

  • “aws help”: To know about AWS service name
  • “aws <command> help”: To know what the command requires
  • “aws <command> <subcommand> help”: To know further about the requirement of the subcommand

To use aws CLI, we need to first access our aws account using the access key & secret key & we can access our keys from the IAM service after we have our keys

We can use the “aws configure” command & enter our access key, secret key & default region name to log in to our account from CLI

Creating a key-pair using CLI

Firstly search for something with ‘create’, in “ aws ec2 help”, where we will find out: “create-key-pair”, then explore it using “aws create-key-pair help”, this command will give all the detailed output about what this will do, what its syntax is along with the examples.

Using the above method, we find out one way to create a key pair is using the command: “aws ec2 create-key-pair — key-name MyCLIKey”.

OUTPUT OF create-key-pair

The output includes the KeyFingerprint, KeyMaterial, KeyName, KeyPairId. It is important to save the KeyMaterial in a file with pem extension. Remember to remove all “\n” and add a new line instead of it. My KeyPairId is:

key-0d7230b48b6f54fb4”

Creating a security group

We can search “aws ec2 help” for something with ‘create’ & we will find out: “create-security-group”. Using “aws create-security-group help”, we will find out the syntax, detailed description & examples. Thus one way to create a security group is using:

“aws ec2 create-security-group — description All_traffic — group-name SecGrp”.

OUTPUT of create-security-group

The output of the command would be a security group id, which is :

sg-064f0f41e02962ff0"

As this security group does not have any inbound rule, which we can check from “ aws ec2 describe-security-groups --group-names SecGRP” & IpPermissions we get is empty.

Since it has no inbound rules we won’t be able to connect to any instance using it too, so we need to add an inbound rule according to our requirement.

~ Adding inbound rule :

We will search “aws ec2 help” & this helps in finding out about “authorize-security-group-ingress”, using “aws ec2 authorize-security-group-ingress help”, we will know what we require. So one way to create an inbound rule is:

“aws ec2 authorize-security-group-ingress — group-id sg-064f0f41e02962ff0 — group-name SecGrp — protocol all — port 0–65525 — cidr 0.0.0.0/0”

OUTPUT of inbound rule addition

There is no output of this command, we can check whether the inbound rule is added using “aws ec2 describe-security-groups — group-names SecGRP” & noting the details in “IpPermissions”

Launching an instance using the above-created key-pair & security group

To launch on OS using CLI, collect all the information (IDs) and note them down in a text file. Then note down all the subcommands using the help. By this approach, we will find out about “aws ec2 run-instances ” & using help we will know about all the subcommands. Finally, one way to create an aws instance is:

“aws ec2 run-instances — image-id ami-0e306788ff2473ccb — instance-type t2.micro — count 1 — key-name MyCLIKey — security-group-ids sg-064f0f41e02962ff0”

OUTPUT of launch of ec2-instance
Seeing instance from AWS Management console

The output of ec2-instance will include all the details regarding the instance, like instance-id : i-0219987e735aa54e1, image_type, keyName, etc.

Creating an EBS volume of 1GiB

Since EBS is a zonal service, we must note down the zone name, in “aws ec2 create-volume help”, we will find out all the subcommands, which leads to finding one way to create an EBS volume:

“aws ec2 create-volume — availability-zone ap-south-1a — volume-type gp2 — size 1”

OUTPUT of create-volume

The output includes details of the new EBS volume such as VolumeID :vol-049111ecf27fc6af2, size, VolumeType etc.

Attaching the new EBS volume to the instance

Using the “aws ec2 help”, we will find out about the attach-volume command & using its help we will find out about various subcommands. Here the device is the device name for the attachment. Available device names depend on the instance type; for example, use /dev/sd[f-p] for Linux instances. One way to attach volume is :

“aws ec2 attach-volume — device /dev/sdf — instance-id i-0219987e735aa54e1 — volume-id vol-049111ecf27fc6af2”

Attaching EBS volume
Checking the attachment from AWS Management Console

Thank you everyone for reading this technical blog.🤗

Tags

#awscloud #awscli #aws #vimaldaga #righteducation #educationredefine #rightmentor #worldrecordholder #linuxworld #makingindiafutureready #righeudcation #arthbylw #awsbylw

--

--