Working with the AWS CLI for EC2

The AWS CLI (Amazon Web Services Command Line Interface) is a powerful tool that enables users to manage AWS services, including EC2 (Elastic Compute Cloud) instances, directly from the command line. This unified tool provides an efficient way to automate cloud infrastructure management tasks and streamline operations without using the AWS Management Console.

The AWS CLI allows users to automate complex tasks, manage EC2 instances programmatically, and integrate AWS operations into scripts and workflows. This makes it an essential tool for DevOps professionals and system administrators working with AWS infrastructure.

Setting Up AWS CLI

Before using the AWS CLI, you must install and configure it on your local machine. The AWS CLI is compatible with Windows, Linux, and macOS operating systems.

Installation on Linux (Ubuntu/Debian)

$ sudo apt-get update
$ sudo apt-get install awscli

After installation, verify that the CLI is working properly:

$ aws --version

The output should display the AWS CLI version number if installed correctly.

Configuration

Configure the AWS CLI with your account credentials:

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: json

You'll need your AWS Access Key ID and Secret Access Key from your AWS account dashboard, along with your preferred default region and output format.

Creating an EC2 Instance

Use the run-instances command to launch a new EC2 instance:

$ 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}]'

Key parameters explained:

  • --image-id Specifies the Amazon Machine Image (AMI) ID to use

  • --instance-type Defines the hardware resources (CPU, memory, storage)

  • --key-name Name of the key pair for SSH access

  • --security-group-ids Security groups to attach to the instance

  • --subnet-id Subnet where the instance will be launched

  • --tag-specifications Tags to apply to the instance (optional)

The command returns detailed information about the created instance, including instance ID, public/private IP addresses, and current state.

Listing EC2 Instances

Use the describe-instances command to view all EC2 instances in your account:

$ aws ec2 describe-instances

This command returns comprehensive information about all instances, including instance ID, type, state, IP addresses, and associated resources.

Managing EC2 Instance States

Stopping an EC2 Instance

Stop a running instance using the stop-instances command:

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

Starting an EC2 Instance

Restart a stopped instance with the start-instances command:

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

Terminating an EC2 Instance

Permanently delete an instance using the terminate-instances command:

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

The --instance-ids parameter accepts multiple instance IDs separated by spaces to perform batch operations.

Common Use Cases

Operation Command Purpose
Launch Instance run-instances Create new EC2 instance
List Instances describe-instances View instance details
Stop Instance stop-instances Shutdown running instance
Start Instance start-instances Boot stopped instance
Terminate Instance terminate-instances Permanently delete instance

Conclusion

The AWS CLI provides a powerful command-line interface for managing EC2 instances and other AWS resources. It enables automation, scripting, and efficient cloud infrastructure management through simple commands. Mastering these basic EC2 operations through the CLI is essential for effective AWS cloud administration and DevOps workflows.

Updated on: 2026-03-17T09:01:38+05:30

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements