AWS CLI Cheat Sheet for Professionals

AWS CLI Cheat Sheet for Professionals

Top 100 AWS CLI Commands

Below is a categorized list of 100 commonly used AWS CLI commands across various services, selected for their relevance in penetration testing and security assessments.

S3 (Simple Storage Service)

CommandDescription
aws s3 lsList all S3 buckets
aws s3 ls s3://bucketnameList contents of a specific bucket
aws s3 mb s3://bucketnameCreate a new bucket
aws s3 rb s3://bucketnameRemove an empty bucket
aws s3 cp localfile s3://bucketname/Upload a file to a bucket
aws s3 cp s3://bucketname/object localfileDownload a file from a bucket
aws s3 sync localdir s3://bucketname/Sync a local directory to a bucket
aws s3 sync s3://bucketname/ localdirSync a bucket to a local directory
aws s3 rm s3://bucketname/objectDelete an object from a bucket
aws s3 presign s3://bucketname/objectGenerate a pre-signed URL for an object

EC2 (Elastic Compute Cloud)

CommandDescription
`aws ec2 describe-instancesList all EC2 instances
aws ec2 run-instances --image-id ami-123 --count 1 --instance-type t2.micro --key-name MyKey --security-group-ids sg-123 --subnet-id subnet-123Launch a new instance
aws ec2 terminate-instances --instance-ids i-123Terminate an instance
aws ec2 start-instances --instance-ids i-123Start an instance
aws ec2 stop-instances --instance-ids i-123Stop an instance
aws ec2 describe-volumesList all EBS volumes
aws ec2 create-security-group --group-name MySG --description "My SG"Create a security group
aws ec2 authorize-security-group-ingress --group-id sg-123 --protocol tcp --port 22 --cidr 0.0.0.0/0Add an ingress rule to a security group
aws ec2 describe-security-groupsList all security groups
aws ec2 describe-subnetsList all subnets

IAM (Identity and Access Management)

CommandDescription
aws iam list-usersList all IAM users
aws iam create-user --user-name BobCreate a new IAM user
aws iam delete-user --user-name BobDelete an IAM user
aws iam attach-user-policy --user-name Bob --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccessAttach a policy to a user
aws iam list-groupsList all IAM groups
aws iam create-group --group-name MyGroupCreate a new IAM group
aws iam add-user-to-group --user-name Bob --group-name MyGroupAdd a user to a group
aws iam list-policiesList all IAM policies
aws iam get-policy --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccessGet details of a policy
aws iam create-role --role-name MyRole --assume-role-policy-document file://trust-policy.jsonCreate an IAM role

VPC (Virtual Private Cloud)

CommandDescription
aws ec2 describe-vpcsList all VPCs
aws ec2 create-vpc --cidr-block 10.0.0.0/16Create a new VPC
aws ec2 delete-vpc --vpc-id vpc-123Delete a VPC
aws ec2 describe-subnetsList all subnets
aws ec2 create-subnet --vpc-id vpc-123 --cidr-block 10.0.1.0/24Create a subnet
aws ec2 delete-subnet --subnet-id subnet-123Delete a subnet
aws ec2 describe-route-tablesList all route tables
aws ec2 create-route-table --vpc-id vpc-123Create a route table
aws ec2 associate-route-table --route-table-id rtb-123 --subnet-id subnet-123Associate a route table with a subnet
aws ec2 describe-internet-gatewaysList all internet gateways
aws ec2 create-internet-gatewayCreate an internet gateway
aws ec2 attach-internet-gateway --internet-gateway-id igw-123 --vpc-id vpc-123Attach an internet gateway to a VPC

Lambda

CommandDescription
aws lambda list-functionsList all Lambda functions
aws lambda create-function --function-name MyFunction --runtime python3.8 --role arn:aws:iam::123456789012:role/lambda-role --handler lambda_function.lambda_handler --zip-file fileb://function.zipCreate a Lambda function
aws lambda invoke --function-name MyFunction output.txtInvoke a Lambda function
aws lambda update-function-code --function-name MyFunction --zip-file fileb://new_function.zipUpdate a Lambda function’s code
aws lambda delete-function --function-name MyFunctionDelete a Lambda function

DynamoDB

CommandDescription
aws dynamodb list-tablesList all DynamoDB tables
aws dynamodb create-table --table-name MyTable --attribute-definitions AttributeName=Id,AttributeType=S --key-schema AttributeName=Id,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5Create a DynamoDB table
aws dynamodb put-item --table-name MyTable --item '{"Id": {"S": "1"}, "Name": {"S": "John"}}'Insert an item into a table
aws dynamodb get-item --table-name MyTable --key '{"Id": {"S": "1"}}'Retrieve an item from a table
aws dynamodb scan --table-name MyTableScan all items in a table
aws dynamodb delete-item --table-name MyTable --key '{"Id": {"S": "1"}}'Delete an item from a table
aws dynamodb delete-table --table-name MyTableDelete a DynamoDB table

CloudFormation

CommandDescription
aws cloudformation create-stack --stack-name MyStack --template-body file://template.yamlCreate a CloudFormation stack
aws cloudformation describe-stacks --stack-name MyStackDescribe a stack
aws cloudformation update-stack --stack-name MyStack --template-body file://new_template.yamlUpdate a stack
aws cloudformation delete-stack --stack-name MyStackDelete a stack
aws cloudformation list-stacksList all stacks

RDS (Relational Database Service)

CommandDescription
aws rds describe-db-instancesList all RDS instances
aws rds create-db-instance --db-instance-identifier MyDB --db-instance-class db.t2.micro --engine mysql --master-username admin --master-user-password password --allocated-storage 20Create an RDS instance
aws rds delete-db-instance --db-instance-identifier MyDB --skip-final-snapshotDelete an RDS instance
aws rds start-db-instance --db-instance-identifier MyDBStart an RDS instance
aws rds stop-db-instance --db-instance-identifier MyDBStop an RDS instance

SQS (Simple Queue Service)

CommandDescription
aws sqs list-queuesList all SQS queues
aws sqs create-queue --queue-name MyQueueCreate a new queue
aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue --message-body "Hello"Send a message to a queue
aws sqs receive-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/MyQueueReceive a message from a queue
aws sqs delete-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue --receipt-handle <handle>Delete a message from a queue
aws sqs delete-queue --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/MyQueueDelete a queue

SNS (Simple Notification Service)

CommandDescription
aws sns list-topicsList all SNS topics
aws sns create-topic --name MyTopicCreate a new topic
aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:MyTopic --protocol email --notification-endpoint myemail@example.comSubscribe to a topic
aws sns publish --topic-arn arn:aws:sns:us-east-1:123456789012:MyTopic --message "Hello"Publish a message to a topic
aws sns delete-topic --topic-arn arn:aws:sns:us-east-1:123456789012:MyTopicDelete a topic

CloudWatch

CommandDescription
aws cloudwatch describe-alarmsList all CloudWatch alarms
aws cloudwatch put-metric-alarm --alarm-name MyAlarm --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 80 --comparison-operator GreaterThanThreshold --dimensions Name=InstanceId,Value=i-1234567890abcdef0 --evaluation-periods 2 --alarm-actions arn:aws:sns:us-east-1:123456789012:MyTopicCreate a metric alarm
aws cloudwatch delete-alarms --alarm-names MyAlarmDelete an alarm
aws cloudwatch get-metric-statistics --metric-name CPUUtilization --namespace AWS/EC2 --statistics Average --start-time 2023-01-01T00:00:00Z --end-time 2023-01-02T00:00:00Z --period 3600 --dimensions Name=InstanceId,Value=i-1234567890abcdef0Get metric statistics

EBS (Elastic Block Store)

CommandDescription
aws ec2 describe-volumesList all EBS volumes
aws ec2 create-volume --size 10 --availability-zone us-east-1a --volume-type gp2Create a new volume
aws ec2 attach-volume --volume-id vol-123 --instance-id i-123 --device /dev/sdfAttach a volume to an instance
aws ec2 detach-volume --volume-id vol-123Detach a volume
aws ec2 delete-volume --volume-id vol-123Delete a volume

ECS (Elastic Container Service)

CommandDescription
aws ecs list-clustersList all ECS clusters
aws ecs create-cluster --cluster-name MyClusterCreate a new cluster
aws ecs register-task-definition --family MyTask --container-definitions file://container.jsonRegister a task definition
aws ecs run-task --cluster MyCluster --task-definition MyTaskRun a task
aws ecs list-tasks --cluster MyClusterList tasks in a cluster
aws ecs stop-task --cluster MyCluster --task <task-arn>Stop a task

EKS (Elastic Kubernetes Service)

CommandDescription
aws eks list-clustersList all EKS clusters
aws eks create-cluster --name MyCluster --role-arn arn:aws:iam::123456789012:role/EKSRole --resources-vpcme-vpc-config subnetIds=subnet-123,subnet-456,securityGroupIds=sg-123Create a new cluster
aws eks describe-cluster --name MyClusterDescribe a cluster
aws eks delete-cluster --name MyClusterDelete a cluster

API Gateway

CommandDescription
aws apigateway get-rest-apisList all REST APIs
aws apigateway create-rest-api --name MyAPICreate a new REST API
aws apigateway get-resources --rest-api-id <api-id>List resources for an API
aws apigateway create-resource --rest-api-id <api-id> --parent-id <parent-id> --path-part {proxy+}Create a resource
aws apigateway put-method --rest-api-id <api-id> --resource-id <resource-id> --http-method ANY --authorization-type NONEAdd a method to a resource
aws apigateway put-integration --rest-api-id <api-id> --resource-id <resource-id> --http-method ANY --type HTTP --integration-http-method ANY --uri http://example.comAdd an integration to a method

20 Real-World Examples

These examples demonstrate practical applications of AWS CLI commands in penetration testing and security management scenarios. Each example includes commands and a brief explanation of their use.

1. Create an S3 Bucket and Upload a File

Commands:

aws s3 mb s3://mybucket aws s3 cp localfile.txt s3://mybucket/

Explanation: Creates a new S3 bucket and uploads a file, useful for testing bucket permissions and data exposure.

2. List All S3 Buckets

Command:

aws s3 ls

Explanation: Lists all S3 buckets in the account, helping identify misconfigured or publicly accessible buckets.

3. Download a File from S3

Command:

aws s3 cp s3://mybucket/file.txt localfile.txt

Explanation: Downloads a file from an S3 bucket, useful for verifying access controls.

4. Sync a Local Directory with an S3 Bucket

Command:

aws s3 sync localdir s3://mybucket/

Explanation: Synchronizes a local directory with an S3 bucket, ideal for backing up data or testing sync permissions.

5. Launch an EC2 Instance

Command:

aws ec2 run-instances --image-id ami-123 --count 1 --instance-type t2.micro --key-name MyKey --security-group-ids sg-123 --subnet-id subnet-123

Explanation: Launches a new EC2 instance, useful for setting up test environments or simulating attacker infrastructure.

6. Terminate an EC2 Instance

Command:

aws ec2 terminate-instances --instance-ids i-123

Explanation: Terminates an EC2 instance, ensuring cleanup after testing to prevent unauthorized access.

7. Describe All EC2 Instances

Command:

aws ec2 describe-instances

Explanation: Lists all EC2 instances, helping identify running instances and their configurations.

8. Create a Security Group and Add a Rule

Commands:

aws ec2 create-security-group --group-name MySG --description "My SG" aws ec2 authorize-security-group-ingress --group-id sg-123 --protocol tcp --port 22 --cidr 0.0.0.0/0

Explanation: Creates a security group and adds an SSH ingress rule, useful for testing network access controls.

9. Create an IAM User and Attach a Policy

Commands:

aws iam create-user --user-name Bob aws iam attach-user-policy --user-name Bob --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess

Explanation: Creates an IAM user and grants S3 read-only access, ideal for testing least privilege principles.

10. List All IAM Users

Command:

aws iam list-users

Explanation: Lists all IAM users, helping identify unauthorized or dormant accounts.

11. Create a Lambda Function

Command:

aws lambda create-function --function-name MyFunction --runtime python3.8 --role arn:aws:iam::123456789012:role/lambda-role --handler lambda_function.lambda_handler --zip-file fileb://function.zip

Explanation: Creates a Lambda function, useful for testing serverless application security.

12. Invoke a Lambda Function

Command:

aws lambda invoke --function-name MyFunction output.txt

Explanation: Invokes a Lambda function, allowing testers to verify function behavior and outputs.

13. Create a DynamoDB Table

Command:

aws dynamodb create-table --table-name MyTable --attribute-definitions AttributeName=Id,AttributeType=S --key-schema AttributeName=Id,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

Explanation: Creates a DynamoDB table, useful for testing data storage security.

14. Insert an Item into DynamoDB

Command:

aws dynamodb put-item --table-name MyTable --item '{"Id": {"S": "1"}, "Name": {"S": "John"}}'

Explanation: Inserts an item into a DynamoDB table, allowing testers to verify data access controls.

15. Get an Item from DynamoDB

Command:

aws dynamodb get-item --table-name MyTable --key '{"Id": {"S": "1"}}'

Explanation: Retrieves an item from a DynamoDB table, useful for testing read permissions.

16. Create a CloudFormation Stack

Command:

aws cloudformation create-stack --stack-name MyStack --template-body file://template.yaml

Explanation: Creates a CloudFormation stack, ideal for deploying test infrastructure reproducibly.

17. Describe a CloudFormation Stack

Command:

aws cloudformation describe-stacks --stack-name MyStack

Explanation: Describes a CloudFormation stack, helping verify deployed resources.

18. Create an RDS Database Instance

Command:

aws rds create-db-instance --db-instance-identifier MyDB --db-instance-class db.t2.micro --engine mysql --master-username admin --master-user-password password --allocated-storage 20

Explanation: Creates an RDS database instance, useful for testing database security configurations.

19. List All RDS Database Instances

Command:

aws rds describe-db-instances

Explanation: Lists all RDS instances, helping identify database configurations and potential vulnerabilities.

20. Set Up a CloudWatch Alarm for an EC2 Instance

Command:

aws cloudwatch put-metric-alarm --alarm-name CPUAlarm --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 80 --comparison-operator GreaterThanThreshold --dimensions Name=InstanceId,Value=i-123 --evaluation-periods 2 --alarm-actions arn:aws:sns:us-east-1:123456789012:MyTopic

Explanation: Sets up a CloudWatch alarm to monitor CPU utilization, critical for detecting performance issues during testing.

Additional Resources

For further exploration, consult the following resources:

Security Best Practices

  • Least Privilege: Ensure IAM policies grant only necessary permissions, as demonstrated in the IAM examples.
  • Secure Configurations: Use secure settings for S3 buckets (e.g., block public access) and EC2 security groups (e.g., restrict ingress rules).
  • Monitoring: Implement CloudWatch alarms and logs to detect unauthorized access or anomalies.
  • Cleanup: Terminate test instances and delete unused resources to prevent exposure, as shown in the EC2 and S3 examples.

This cheat sheet equips penetration testers with the tools to manage and secure AWS environments effectively, aligning with enterprise security standards and compliance requirements.

Key Citations