We can manage EC2 instances from the command-line using aws-cli. We can create, start, stop, reboot, terminate, modify and do a lot with EC2 instances using aws-cli. Click here to learn more about managing EC2 instances from using the aws-cli.

In this article, I will show you several commands to operate EC2 instances and this can be a guide to get started with aws-cli to manage EC2 instances from the terminal. It is assumed that you are already aware of EC2 service on AWS. Click here if you want to learn to create an EC2 instance from the AWS console. We will not go into detail about EC2 instances.

Pre-requisites

  1. AWS Account  (Create if you don’t have one).
  2. Basic understanding of EC2 Instance (Click here to learn to create an EC2 instance from the AWS Console).
  3. AWS IAM user with AmazonEC2FullAccess policy attached to it and its access and secret keys (Click here to learn to create an IAM User).
  4. AWS CLI installed on your local machine.
  5. Key-Pair in the desired region.

What will we do?

  1. Check aws cli and export aws access & secret key on your local machine.
  2. Create an EC2 instance using aws cli.
  3. Perform the basic operation on the EC2 instance using aws-cli.

Check aws cli and export aws access & secret key on your local machine.

Check the version of aws-cli you have on your machine. If you do not have it, then refer to the official documentation here to install the aws-cli on your local machine

aws --version

If you execute the following command, you will get an error as you have not configured access to your AWS account in the terminal.

aws sts get-caller-identity

Export AWS IAM user access and secret keys on you terminal

export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=

This time, you can check your identity by executing the following command

aws sts get-caller-identity

<img alt="Check aws cli version and export aws keys" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_2021-04-18_at_61107_pm.png607f14b542837.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="264" loading="lazy" src="data:image/svg xml,” width=”750″>

Create an EC2 instance using aws cli.

To create a new EC2 instance, execute the following command

aws ec2 run-instances --image-id ami-0a0d71ff90f62f72a --count 1 --instance-type t2.micro --key-name howtoforge-test --security-group-ids sg-7fa4d512 --subnet-id subnet-ae1a35c7 --region eu-west-3

In the above command, change all the values assigned to the following options. 

  1. –image-id = ID of the AMI
  2. –count = Number of instances
  3. -instance-type = Type of the EC2 instance to be created
  4. –key-name = Name of the existing key in the specified region
  5. –security-group-ids = ID of the existing security group in the specified region
  6. –subnet-id = ID of the subnet in which EC2 instance is to be created
  7. –region = Region in which the instance is to be created.

<img alt="Create EC2 instance using aws cli" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_2021-04-18_at_61134_pm.png607f14b6a9f0f.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="117" loading="lazy" src="data:image/svg xml,” width=”750″>

You can go to the AWS EC2 console and see your new EC2 instance getting created.

<img alt="EC2 instance being created" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_2021-04-18_at_60456_pm.png607f14b8405b6.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="137" loading="lazy" src="data:image/svg xml,” width=”750″>

Perform basic operations on the EC2 instance using aws-cli.

Before you modify your instance, you need to first stop it.

Get the ID of the instance to be stoped

aws ec2 describe-instances --filters Name=instance-state-name,Values=running  --region eu-west-3 | grep InstanceId

If you try to modify the instance before you stop it, the operation will not be permited.

aws ec2 modify-instance-attribute --instance-id i-0f9a0305493735b13 --instance-type "{"Value": "m1.small"}" --region eu-west-3

You can stop the instance by specifying the Instance ID and region.

aws ec2 stop-instances --instance-id i-0f9a0305493735b13 --region eu-west-3

<img alt="Stop the instance" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/stop_the_instance.png607f14b8a6c4a.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="253" loading="lazy" src="data:image/svg xml,” width=”750″>

You can see in the console that the instance is being stopped.

<img alt="Instance being stopped" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_2021-04-18_at_61957_pm.png607f14ba5232e.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="137" loading="lazy" src="data:image/svg xml,” width=”750″>

If you want to change/modify the instance type of the instance we created, execute the following command.

aws ec2 modify-instance-attribute --instance-id i-0f9a0305493735b13 --instance-type "{"Value": "t2.small"}" --region eu-west-3

You can now start the instance by executing the following command.

aws ec2 start-instances --instance-id i-0f9a0305493735b13 --region eu-west-3

<img alt="Modify the instance" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_2021-04-18_at_62412_pm.png607f14bc5ceac.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="240" loading="lazy" src="data:image/svg xml,” width=”750″>

Check in the EC2 console, if the instance type is changed or not.

<img alt="Modified the instance type" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_2021-04-18_at_62406_pm.png607f14bd7c5bc.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="122" loading="lazy" src="data:image/svg xml,” width=”750″>

It is always a good idea to delete your resource when you no longer need them.Advertisement

To delete the instance, execute the following command by replacing the instance id with the valid ID

aws ec2 terminate-instances --instance-id i-0f9a0305493735b13 --region eu-west-3

<img alt="Terminate the instance" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_2021-04-18_at_62951_pm.png607f14c0c2a5f.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="261" loading="lazy" src="data:image/svg xml,” width=”750″>

Go to the console and see your instance is terminated.

<img alt="Terminated the instance" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_2021-04-18_at_63021_pm.png607f14c135e00.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="126" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

We can manage EC2 instances from the terminal the same way we manage them from the AWS Console. In this article, we saw basic commands to create, stop, modify, start and terminate the EC2 instance from the terminal using the aws-cli command-line utility. You can use this guide as a starting point to get started with managing EC2 instances from the terminal.