Working with the AWS CLI for EC2


Among the well-known cloud computing platforms is AWS (Amazon Web Services). Elastic Compute Cloud, or AWS EC2, is a well-known Amazon service. The Amazon CLI (Command Line Interface) is a potent tool that enables users to interact with the EC2 service via the command line in order to effectively administer EC2 instances. In-depth coverage of the Amazon CLI is provided in this article, along with examples of how to carry out various EC2 operations like establishing, listing, initiating, and terminating EC2 instances.

The Amazon CLI can be used by users to automate challenging tasks and simplify cloud infrastructure management. Customers can use the command line to manage their EC2 instances and other AWS resources by using the Amazon CLI.

Setting Up AWS CLI

You must first build up an AWS CLI environment on your local PC before utilizing Amazon CLI. Windows, Linux, and macOS are just a few of the operating systems that are compatible with the Amazon CLI. Via the AWS website, the AWS CLI package can be downloaded and installed. You must configure the Amazon CLI using your AWS account credentials after installation.

Open the terminal or command prompt, and type the following command to configure the Amazon CLI −

$ sudo apt-get update

$ sudo apt-get install awscli

Use the next command to test that the CLI is active or not after installation −

$ aws --version

The output should contain the AWS CLI version number if everything install properly.

$ aws configure
AWS Access Key ID [None]: Enter your own AWS access key
AWS Secret Access Key [None]: Enter your own AWS Secret access key
Default region name [None]: us-east-1
Default output format [None]: json

Our Amazon Web Services access key ID, secret access key, default region name, and default output format will be required in order to run this command. Your AWS account dashboard contains both your secret access key and AWS access key ID.

Creating an EC2 Instance

The run-instances command is required to create an EC2 instance using AWS CLI. The following command launches a fresh Amazon Linux 2 AMI, t2.micro instance type, and default VPC −

$ aws ec2 run-instances --image-id ami-0c55b159cbfafe1f0 --instance-type t2.micro --key-name my-key-pair --security-group-ids sg-0a123456789abcdef --subnet-id subnet-0a123456 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=my-instance}]'

The Amazon Machine Image (AMI) ID that will be utilised to launch the instance is provided by the —image-id argument.

The instance type, which identifies the hardware resources of the instance, is provided by the —instance-type option.

The name of the key pair to use to connect to the instance is provided by the —key-name argument.

The security groups to which we want to attach the instance are identified by their IDs using the —security-group-ids option.

The ID of the subnet in which the instance should be launched is provided by the —subnet-id argument.

The tags that should be applied to the instance are provided by the —tag-specifications option.

It is not necessary to use the —tag-specifications option.

The run-instances command returns a variety of information about the generated instance, including the instance ID, public IP address, private IP address, and many others..

Listing EC2 Instances

The describe-instances command displays a list of all the EC2 instances presently running in your account. The following command displays a list of all EC2 instances that are currently running in the default region −

$ aws ec2 describe-instances

The describe-instances command returns a variety of information about the instances, including instance ID, instance type, public IP address, private IP address, and many others.

Stopping an EC2 Instance

The stop-instances command can be used to terminate a running EC2 instance. The EC2 instance supplied by the following command is terminated −

$ aws ec2 stop-instances --instance-ids i-1234567890abcdef0

The—instance-ids parameter specifies the IDs of the instances to be terminated.

The stop-instances command delivers information about the halted instance, such as the instance ID and current status, among other things.

Starting an EC2 Instance

The start-instances command can be used to restart a previously halted EC2 instance. The command below launches the requested EC2 instance −

$ aws ec2 start-instances --instance-ids i-1234567890abcdef0

The —instance-ids option specifies the IDs of the instances that we want to run.

The start-instances command returns a variety of information about the launched instance, including the instance ID, current status, and many others.

Terminating an EC2 Instance

The terminate-instances command can be used to terminate an EC2 instance. The command below terminates the specified EC2 instance −

$ aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

The —instance-ids option specifies the IDs of the instances to be terminated.

The terminate-instances command returns a variety of information about the terminated instance, including the instance ID, current status, and many others.

Conclusion

The Amazon CLI is a command-line interface that allows customers to control AWS resources such as EC2 instances. This versatile application provides customers with flexibility and automation, allowing them to save time and streamline their cloud infrastructure administration. Setting up the Amazon CLI and using it to execute actions like creating, listing, initiating, and terminating EC2 instances were covered in this article.

Updated on: 08-May-2023

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements