AWS CLI Commands For AWS EC2 (Amazon Elastic Compute Cloud)

AWS CLI Commands For AWS EC2 (Amazon Elastic Compute Cloud)

In this post we will learn, how to use AWS CLI commands to configure or setup AWS EC2 service (Amazon Elastic Compute Cloud) with different supplementary components.

AWS EC2 is one of the most fundamental IaaS (Infrastructure as a Service) service provided by AWS. It allow users to rent raw compute power in shape of virtual machine on which they can run their own applications. It provides secure, scalable, re-sizable & robust computing capacity in cloud.

It is very reasonable to developers & small startups. We can rent EC2 as per requirements and we can terminate it whenever it no longer required within minutes instead of days & hours. We can create number of instances in EC2 and will pay for that only.

There are multiple ways in which EC2 instance can be created and deployed into AWS cloud.
  • AWS Management Web Console

Below are the EC2 components which we need to understand.
  • AWS EC2 Key Pair
  • Security Groups
  • VPC & Subnets
  • Elastic IP Address

Key Pair: 
We need to create Key Pair, it allows us to connect to our EC2 instance securely.

Lets see how to create new Key Pair using AWS CLI, You can use command below to create keypair.

>> aws ec2 create-key-pair --key-name MyKeyPair

Once key has been generated AWS does not provide access private key part of key pair. So we need to download private key on the time of key pair generation for future use and keep it in safe & secure place.

Lets create key pair and save key details in a file securely.

>> aws ec2 create-key-pair --key-name MyKeyPair --output text > test.pem

After enter you will see testkey.pem file has been created in root path, lets type 'ls' command to check file

>> ls
OutPut>> testkey.pem

This file contains our private key and make sure you will save it in secure location.

Security Groups: 
It controls which ports and protocols allow or disallows traffic towards EC2 instance. These groups are similar to Firewall in which we can add some networking rules to control traffics.

Lets create security groups using AWS CLI.

>> aws ec2 create-security-groups

There are two types of security groups we can create

EC2 Classic and EC2-VPC

EC2 Classic is the old way to create group, in this our instances run into single slab network that we shared with other AWS customers. Recently AWS released a new version of EC2 security groups service in which our instances are runs into virtual private cloud. That means it is logically isolated from other AWS customers. Hence EC2-VPC is more secure then EC2-classic.

To create EC2-VPC security group we need VPC is which can be get from default VPC created by AWS. We can get this ID from AWS Management Console.

>> aws ec2 create-security-group --group-name testgroup --description "Test Description" --vpc-id 'EnterVPCID'

Output>> {

"GroupId" : "sg-3....."

Lets verify newly security group created as follows.

>> aws ec2 describe-security-groups --group-ids EnterGroupIDHere

Once you execute this it will display the security group which we created just now.

AMI (Amazon Machine Image): 
It is Amazon machine image and template for EC2 instance, which provides the information about baseline operating system image that is require to launch EC2 instance. Using AMI we can pre-load the desired OS and server software on EC2 instance. This also includes launch permission that control which AWS account can use AMI to launch instance. Using this AMI we can launch various type of EC2 instances like various type of linux, windows and other server applications like webserver, databases server. You can search for AMI that needs the criteria for your EC2 instance.

To create AMI we need  AMI ID which we can get that from AWS Management Console.

VPC (Virtual Private Cloud) & Subnets: 
These are basic buildings blocks of networking infrastructure within AWS cloud. All EC2 instances are assign to one of these VPC and subnets for secure communication with other AWS components. We will use default VPC and Subnets for further used.

Deploy AWS EC2 Instance:
Now we are ready to launch AWS new ec2 instance with information we prepared above. Lets see how we can use all these information and create instance using AWS CLI.

>> aws ec2 run-instances --image-id enterAmiIdHere --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids enterSecurityGroupIdHere --subnet-id enterSubnetIdHere

Lets see all these in details one by one.

image-id : It is AMI id which is by default created by AWS for our account, we can get it from AWS Management Console.
instance-type: Type of instance we want to create
key-name: This is key which we created earlier
security-group-ids: This is security group id which we created
subnet-id: This is default subnet id, we can get it from AWS Management Console.

Full deployment of EC2 instance will get some time. You can verify it by using AWS CLI command as follow.

>> aws ec2 describe-instances

It will show status pending as it will take some time to activate instance.

Elastic IP Address:
We can create elastic ip address of our EC2 instance so that it will have a static ip address forever. Elastic ip address is the public ip address which reachable from Internet. We can associate any elastic ip address with any EC2 instance. If we don't have elastic ip address then whenever we reboot or shutdown and start EC2 instance, it will create new DNS ip address for each reboot which not recommended and user friendly.

Lets see how to create elastic ip address and associate to EC2 instance.

>> aws ec2 allocate-address




Note: We need to store our allocation ID to associate with EC2 instance.

Now lets associate new IP address to our EC2 instance.

>> aws ec2 associate-address -- instance-id enterInstanceIdHere --allocation-id enterAllocationIdHere

OutPut>> { "AssociationId":"eipassoc-el7-------" }

To verify everything have setup as expected we can run below command

>> aws ec2 describe-instances

and you will see all the information of our newly created EC2 instance and elastic ip address.

What is user data:
This is usually used when we launches EC2 instances. It is used to provides some custom user data to instance that can to perform common automated configuration task and even run script after the instance start.

>> aws ec2 run-instances --image-id enterImageIDHere -- count 1 -- instance-type t2.micro --key-name MyKeyPair --security-group-ids enterSecurityGroupIdHere --subnet-id enterSubnetIdHere -- user-data "sudo apt-get install Nginx"

This command will install Nginx server automatically as our EC2 instance launches within EC2 cloud.

How to terminate EC2 instance using AWS CLI:
>> aws ec2 terminate-instances --instance-id enterInstanceIdHere

Once you execute this command you will see a termination message in output like below.

Output>> { "TerminatingInstances":{{ "InstanceId":"InstanceIdWillDisplayHere" }} }

Watch Video:

AWS CLI Commands For AWS EC2 (Amazon Elastic Compute Cloud) AWS CLI Commands For AWS EC2 (Amazon Elastic Compute Cloud) Reviewed by Web Technology Funda on 11:14:00 AM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!