Skip to content

AWS CLI

Comprehensive AWS CLI commands and workflows for managing Amazon Web Services infrastructure, including EC2, S3, Lambda, and more.

Installation & Configuration

CommandDescription
aws configureConfigure AWS credentials and region
aws configure listShow current configuration
aws configure set region us-west-2Set default region
aws configure set output jsonSet output format
aws sts get-caller-identityVerify current identity
aws configure list-profilesList all configured profiles
aws configure --profile myprofileConfigure named profile

EC2 (Elastic Compute Cloud)

Instance Management

CommandDescription
aws ec2 describe-instancesList all instances
aws ec2 run-instances --image-id ami-12345 --instance-type t2.microLaunch instance
aws ec2 start-instances --instance-ids i-1234567890abcdef0Start instance
aws ec2 stop-instances --instance-ids i-1234567890abcdef0Stop instance
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0Terminate instance
aws ec2 reboot-instances --instance-ids i-1234567890abcdef0Reboot instance

Security Groups

CommandDescription
aws ec2 describe-security-groupsList security groups
aws ec2 create-security-group --group-name MySecurityGroup --description "My security group"Create security group
aws ec2 authorize-security-group-ingress --group-id sg-12345 --protocol tcp --port 80 --cidr 0.0.0.0/0Add inbound rule
aws ec2 revoke-security-group-ingress --group-id sg-12345 --protocol tcp --port 80 --cidr 0.0.0.0/0Remove inbound rule

Key Pairs

CommandDescription
aws ec2 describe-key-pairsList key pairs
aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pemCreate key pair
aws ec2 delete-key-pair --key-name MyKeyPairDelete key pair

S3 (Simple Storage Service)

Bucket Operations

CommandDescription
aws s3 lsList all buckets
aws s3 mb s3://my-bucketCreate bucket
aws s3 rb s3://my-bucketRemove empty bucket
aws s3 rb s3://my-bucket --forceRemove bucket and all contents
aws s3 ls s3://my-bucketList objects in bucket
aws s3 ls s3://my-bucket --recursiveList all objects recursively

File Operations

CommandDescription
aws s3 cp file.txt s3://my-bucket/Upload file
aws s3 cp s3://my-bucket/file.txt .Download file
aws s3 sync ./local-folder s3://my-bucket/Sync local folder to S3
aws s3 sync s3://my-bucket/ ./local-folderSync S3 to local folder
aws s3 rm s3://my-bucket/file.txtDelete file
aws s3 rm s3://my-bucket/ --recursiveDelete all files in bucket

Advanced S3 Operations

CommandDescription
aws s3api get-bucket-versioning --bucket my-bucketCheck versioning status
aws s3api put-bucket-versioning --bucket my-bucket --versioning-configuration Status=EnabledEnable versioning
aws s3api get-bucket-encryption --bucket my-bucketCheck encryption
aws s3api put-bucket-encryption --bucket my-bucket --server-side-encryption-configuration file://encryption.jsonEnable encryption

Lambda

Function Management

CommandDescription
aws lambda list-functionsList all functions
aws lambda create-function --function-name my-function --runtime python3.9 --role arn:aws:iam::123456789012:role/lambda-role --handler lambda_function.lambda_handler --zip-file fileb://function.zipCreate function
aws lambda update-function-code --function-name my-function --zip-file fileb://function.zipUpdate function code
aws lambda invoke --function-name my-function output.txtInvoke function
aws lambda delete-function --function-name my-functionDelete function

Function Configuration

CommandDescription
aws lambda get-function --function-name my-functionGet function details
aws lambda update-function-configuration --function-name my-function --timeout 30Update timeout
aws lambda update-function-configuration --function-name my-function --memory-size 256Update memory
aws lambda put-function-event-invoke-config --function-name my-function --maximum-retry-attempts 1Configure retries

IAM (Identity and Access Management)

User Management

CommandDescription
aws iam list-usersList all users
aws iam create-user --user-name myuserCreate user
aws iam delete-user --user-name myuserDelete user
aws iam get-user --user-name myuserGet user details
aws iam create-access-key --user-name myuserCreate access key
aws iam delete-access-key --user-name myuser --access-key-id AKIAIOSFODNN7EXAMPLEDelete access key

Role Management

CommandDescription
aws iam list-rolesList all roles
aws iam create-role --role-name MyRole --assume-role-policy-document file://trust-policy.jsonCreate role
aws iam attach-role-policy --role-name MyRole --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccessAttach policy to role
aws iam detach-role-policy --role-name MyRole --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccessDetach policy from role

Policy Management

CommandDescription
aws iam list-policiesList all policies
aws iam create-policy --policy-name MyPolicy --policy-document file://policy.jsonCreate policy
aws iam delete-policy --policy-arn arn:aws:iam::123456789012:policy/MyPolicyDelete policy
aws iam get-policy --policy-arn arn:aws:iam::123456789012:policy/MyPolicyGet policy details

CloudFormation

Stack Management

CommandDescription
aws cloudformation list-stacksList all stacks
aws cloudformation create-stack --stack-name my-stack --template-body file://template.yamlCreate stack
aws cloudformation update-stack --stack-name my-stack --template-body file://template.yamlUpdate stack
aws cloudformation delete-stack --stack-name my-stackDelete stack
aws cloudformation describe-stacks --stack-name my-stackGet stack details
aws cloudformation describe-stack-events --stack-name my-stackGet stack events

Template Operations

CommandDescription
aws cloudformation validate-template --template-body file://template.yamlValidate template
aws cloudformation estimate-template-cost --template-body file://template.yamlEstimate costs
aws cloudformation get-template --stack-name my-stackGet stack template

RDS (Relational Database Service)

Database Instance Management

CommandDescription
aws rds describe-db-instancesList all DB instances
aws rds create-db-instance --db-instance-identifier mydb --db-instance-class db.t3.micro --engine mysql --master-username admin --master-user-password mypassword --allocated-storage 20Create DB instance
aws rds start-db-instance --db-instance-identifier mydbStart DB instance
aws rds stop-db-instance --db-instance-identifier mydbStop DB instance
aws rds delete-db-instance --db-instance-identifier mydb --skip-final-snapshotDelete DB instance

Database Snapshots

CommandDescription
aws rds describe-db-snapshotsList all snapshots
aws rds create-db-snapshot --db-instance-identifier mydb --db-snapshot-identifier mydb-snapshotCreate snapshot
aws rds restore-db-instance-from-db-snapshot --db-instance-identifier mydb-restored --db-snapshot-identifier mydb-snapshotRestore from snapshot
aws rds delete-db-snapshot --db-snapshot-identifier mydb-snapshotDelete snapshot

VPC (Virtual Private Cloud)

VPC Management

CommandDescription
aws ec2 describe-vpcsList all VPCs
aws ec2 create-vpc --cidr-block 10.0.0.0/16Create VPC
aws ec2 delete-vpc --vpc-id vpc-12345678Delete VPC
aws ec2 describe-subnetsList all subnets
aws ec2 create-subnet --vpc-id vpc-12345678 --cidr-block 10.0.1.0/24Create subnet

Route Tables

CommandDescription
aws ec2 describe-route-tablesList route tables
aws ec2 create-route-table --vpc-id vpc-12345678Create route table
aws ec2 create-route --route-table-id rtb-12345678 --destination-cidr-block 0.0.0.0/0 --gateway-id igw-12345678Add route
aws ec2 associate-route-table --subnet-id subnet-12345678 --route-table-id rtb-12345678Associate route table

CloudWatch

Metrics and Alarms

CommandDescription
aws cloudwatch list-metricsList all metrics
aws cloudwatch get-metric-statistics --namespace AWS/EC2 --metric-name CPUUtilization --dimensions Name=InstanceId,Value=i-1234567890abcdef0 --statistics Average --start-time 2023-01-01T00:00:00Z --end-time 2023-01-02T00:00:00Z --period 3600Get metric statistics
aws cloudwatch describe-alarmsList all alarms
aws cloudwatch put-metric-alarm --alarm-name cpu-mon --alarm-description "Alarm when CPU exceeds 70%" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 70.0 --comparison-operator GreaterThanThresholdCreate alarm

Logs

CommandDescription
aws logs describe-log-groupsList log groups
aws logs create-log-group --log-group-name my-log-groupCreate log group
aws logs describe-log-streams --log-group-name my-log-groupList log streams
aws logs get-log-events --log-group-name my-log-group --log-stream-name my-log-streamGet log events

Best Practices

Security

  1. Use IAM Roles: Prefer IAM roles over access keys for EC2 instances
  2. Least Privilege: Grant minimum required permissions
  3. MFA: Enable multi-factor authentication for sensitive operations
  4. Rotate Keys: Regularly rotate access keys and passwords

Cost Optimization

  1. Resource Tagging: Tag all resources for cost tracking
  2. Right Sizing: Monitor and adjust instance sizes based on usage
  3. Reserved Instances: Use reserved instances for predictable workloads
  4. Spot Instances: Use spot instances for fault-tolerant workloads

Automation

  1. CloudFormation: Use Infrastructure as Code for reproducible deployments
  2. AWS CLI Scripts: Automate repetitive tasks with shell scripts
  3. AWS SDK: Use AWS SDKs for application integration
  4. CI/CD: Integrate AWS CLI into continuous integration pipelines