Skip to content

Instantly share code, notes, and snippets.

@hanksudo
Last active January 12, 2020 15:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanksudo/4621d6b45bfc36ca4c920a40625d4425 to your computer and use it in GitHub Desktop.
Save hanksudo/4621d6b45bfc36ca4c920a40625d4425 to your computer and use it in GitHub Desktop.
AWS CLI note

AWS CLI Note

aws-cli - examples

STS - AWS Security Token Service

# whoami
aws sts get-caller-identity

IAM

aws iam list-users
aws iam list-users --output table

# List keys
aws iam list-access-keys

# List users by ARN
aws iam list-users --output json | jq -r '.Users[].Arn'

EC2

# Elastic IPs
aws ec2 describe-addresses| jq '.'
aws ec2 describe-addresses| jq '.Addresses[] | {PublicIp,PrivateIpAddress,Tags}'

aws ec2 describe-instances | jq '.Reservations[].Instances[] | {InstanceId,InstanceType}'

# List all of your  instances that are currently stopped, and the reason for the stop
aws ec2 describe-instances --filters Name=instance-state-name,Values=stopped --output json | jq -r '.Reservations[].Instances[].StateReason.Message'

S3

aws s3 ls
aws s3 ls $BUCKET_NAME

# Create bucket
aws s3api create-bucket --bucket $BUCKET --region ap-southeast-1

# Get bucket's region
aws s3api get-bucket-location --bucket $BUCKET

# Count objects
aws s3api list-objects --bucket $BUCKET --output json --query "[length(Contents[])]"

# Force delete bucket (even not empty)
aws s3 rb s3://$BUCKET --force --region `aws s3api get-bucket-location --bucket BUCKETNAME | jq '.LocationConstraint' -r`

# Recursively copy a directory and its subfolders from your PC to Amazon S3
aws s3 cp my_folder s3://$BUCKET -- recursive [--region us-west-2]

# List the sizes of an S3 bucket and its contents
aws s3api list-objects --bucket $BUCKET --output json --query "[sum(Contents[].Size), length(Contents[])]"

# Move S3 bucket to different location
aws s3 sync s3://oldbucket s3://newbucket --source-region us-west-1 --region us-west-2

# Remove file with specify extension name
s3cmd --recursive ls s3://$BUCKET | awk '{ print $4 }' | grep ".DS_Store" | xargs s3cmd del
s3cmd --recursive ls s3://$BUCKET | awk '{ print $4 }' | grep ".keep" | xargs s3cmd del

aws s3 rm s3://$BUCKET --recursive --exclude "*.jpg" --exclude "*.png"  --exclude "*.mp4" --exclude "*.m4a" --exclude "*.txt" --exclude "*.mp3"

# Update static files
aws s3 sync static/data s3://$BUCKET/data --delete --region ap-northeast-1 --acl public-read --cache-control max-age=604800

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment