AWS EC2 Command Examples (with examples)
1. Display information about a specific instance
aws ec2 describe-instances --instance-ids instance_id
Motivation: This command allows you to retrieve detailed information about a specific EC2 instance. It can be useful when you want to check the instance’s status, network details, attached volumes, or security groups.
Arguments:
--instance-ids
(required): The ID(s) of the EC2 instance(s) you want to describe.
Example Output:
{
"Reservations": [
{
"Instances": [
{
"InstanceId": "i-0123456789abcdef0",
"InstanceType": "t2.micro",
"State": {
"Name": "running",
"Code": 16
},
"PublicIpAddress": "1.2.3.4",
...
}
]
}
]
}
2. Display information about all instances
aws ec2 describe-instances
Motivation: This command provides an overview of all EC2 instances within your AWS account. It can be useful when you need to manage and monitor multiple instances simultaneously or analyze their attributes collectively.
Arguments: None
Example Output:
{
"Reservations": [
{
"Instances": [
{
"InstanceId": "i-0123456789abcdef0",
"InstanceType": "t2.micro",
"State": {
"Name": "running",
"Code": 16
},
"PublicIpAddress": "1.2.3.4",
...
},
...
]
}
]
}
3. Display information about all EC2 volumes
aws ec2 describe-volumes
Motivation: This command provides details about all Elastic Block Store (EBS) volumes in your AWS account. It helps you get an overview of your storage resources, such as volume size, availability zone, and attachment status.
Arguments: None
Example Output:
{
"Volumes": [
{
"VolumeId": "vol-0123456789abcdef0",
"VolumeType": "gp2",
"Size": 8,
"AvailabilityZone": "us-west-2a",
...
},
...
]
}
4. Delete an EC2 volume
aws ec2 delete-volume --volume-id volume_id
Motivation: This command allows you to delete an EC2 volume that is no longer needed. Removing unnecessary volumes helps save costs and declutter your AWS storage.
Arguments:
--volume-id
(required): The ID of the volume you want to delete.
Example Output: None
5. Create a snapshot from an EC2 volume
aws ec2 create-snapshot --volume-id volume_id
Motivation: This command enables you to create a point-in-time snapshot of an EC2 volume. Snapshots are crucial for data backup, disaster recovery, and creating new volumes from existing data.
Arguments:
--volume-id
(required): The ID of the volume from which to create the snapshot.
Example Output:
{
"SnapshotId": "snap-0123456789abcdef0",
"State": "pending",
...
}
6. List available AMIs (Amazon Machine Images)
aws ec2 describe-images
Motivation: This command provides a list of available Amazon Machine Images (AMIs) in your AWS account. AMIs are templates for launching EC2 instances, and this command helps you identify the available options for various operating systems and software configurations.
Arguments: None
Example Output:
{
"Images": [
{
"ImageId": "ami-0123456789abcdef0",
"Name": "Amazon Linux 2 AMI",
"Platform": "Linux",
...
},
...
]
}
7. Show a list of all available EC2 commands
aws ec2 help
Motivation: This command provides a comprehensive list of available EC2 subcommands. It can be useful when exploring the functionalities of the aws ec2
CLI and looking for additional commands beyond the common ones.
Arguments: None
Example Output: Help information displaying all available EC2 subcommands.
8. Show help for a specific EC2 subcommand
aws ec2 subcommand help
Motivation: This command displays detailed help information for a specific EC2 subcommand. It helps you understand the purpose, arguments, and usage of a particular command, allowing for efficient utilization of the AWS EC2 CLI.
Arguments:
subcommand
(required): The specific EC2 subcommand you want to get help for.
Example Output: Help information for the specified EC2 subcommand, displaying its usage, options, and examples.