Launching an EC2 Instance via AWS CLI

B.V.Rohan Bharadwaj
4 min readOct 21, 2020

AWS Command Line Interface (CLI) is a tool to manage our AWS services, with just one tool to download and configure, we can maintain multiple AWS services from the CLI and also automate them via scripts.

Source for installing AWS CLI on Windows system:

To confirm the installation we can run the above cmd..

Once installation is done , create an IAM user. This allows you to configure your AWS CLI :

aws configure 

You may enter the credentials provided within the excel sheet from AWS (which consists of the above details)

Task Objectives:

1️⃣ Create a key pair

2️⃣ Create a security group and add a rule with port 22 [SSH]

3️⃣ Launch an instance using the above created key pair and security group.

4️⃣ Create an EBS volume of 1 GB.

5️⃣ Attach the above created EBS volume to the instance.

Creating a Key-Pair:

A key pair, consisting of a private key and a public key, is a set of security credentials that you use to prove your identity when connecting to an instance. Amazon EC2 stores the public key, and you store the private key. You use the private key, instead of a password, to securely access your instances.

To create a key-pair, we use:

aws ec2 create-key-pair --key-name <key_name>

→Creating a Security Group:

AWS Security Groups are associated with EC2 instances and will provide security at the protocol and port access level. Each security group , works in the same way as a firewall,containing a set of rules that filter traffic coming into and out of an EC2 instance.

To create an SG we can use:

aws ec2 create-security-group --description "<desc>" --group-name "<security_group_name>"

and if we go and check in the WebUI/Console

If we wish to know the rules available , we can use the cmd:

aws ec2 authorize-security-group-ingress help 
Keep scrolling to know more

Using that, we know what to do now :

aws ec2 authorize-security-group-ingress --group-id <sg-id> --protocol tcp --port <port_of_choice_ex:22 -cidr  0.0.0.0/0>

→Launch an EC2 Instance:

Amazon EC2 provides resizable, secure compute capacity in the cloud via a VM.It is designed to make web-scale cloud computing easier for developers.

In order to launch an ec2 instance we require the following :

↦image-id

↦instance-type

↦subnet-id

↦security-group-ids

↦key-name

↦count

aws ec2 run-instances --image-id <ami> --count <num> --instance-type <type.size> --key-name <Key_Pair_name> --security-group-ids <sg-id> --subnet-id <subnet-id>

→Create an Elastic Block Storage volume

Elastic Block Storage, provides highly available block-level storage volumes for use with Amazon Elastic Compute Cloud (EC2) instances. Amazon EBS enables us to keep data persistently on a file system, even after we shut down our EC2 instance.

To create a volume we require the following:

↦size

↦availability-zone

aws ec2 create volume --availability-zone <AZ_of_choice> --size <size_in_GB>

→Attach the volume to the EC2 instance

Now we’ll attach the volume which we created, to the instance

for doing so, we require:

↦volume-id

↦instance-id

↦device

aws ec2 attach-volume --device <dev> --instance-id <inst_id> --volume-id <vol_id>

Pros of using AWS CLI over the WebUI:

  • Easier to install:One huge benefit of AWS CLI is that installation is smooth, quick, simple, and standardized.
  • Supports all Amazon Web Services: The AWS CLI, by contrast, lets you control all the services from one simple tool.
  • Saves time: GUIs are great when we are just learning the basics of a system. Most users find it faster and easier to use the AWS CLI once they reach a certain level of proficiency.As it can run multiple instances or services in one shot

Thank you for the time!

--

--