Skip to content

aws cli cheat sheet

AWS Cli Cheat Sheet

Get caller identity / get current user details

aws sts get-caller-identity

Quick create a bucket

BUCKET_NAME=my-bucket-$(date +%s)
aws s3 mb $BUCKET_NAME

Replace a File in s3, from local to s3

aws s3 cp C:\Users\sanand\filename.ext s3://bucket-name/path/filename.ext
or
aws s3 cp C:\Users\sanand\filename.ext s3://bucket-name/path/

Sync node of of one local folder to node of another folder in s3

aws s3 sync C:\Users\sanand\path1 s3://bucket-name/folder1/folder2/folder3/ 

Sync node of of one folder in s3 to node of another folder in s3

aws s3 sync s3://bucket-name-1/folder1/folder2/folder3/ s3://bucket-name-2/folder1/folder2/folder3/ 

Empty a s3 bucket

$BUCKET_NAME="bucket-name"
aws s3 rm s3://$BUCKET_NAME/ --recursive

Remove a s3 bucket

$BUCKET_NAME="bucket-name"
aws s3 rm s3://$BUCKET_NAME/ --recursive
aws s3 rb s3://$BUCKET_NAME

Copy a folder from local repo to s3 bucket or s3 folder

 aws s3 cp $LOCAL_PATH\ s3://$BUCKET_NAME/folder1/folder2/ --recursive

Expand disk aws ec2

INSTANCE_ID=
VOLUME_ID=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID | jq -r '.Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId')

aws ec2 modify-volume --size 40 --volume-id $VOLUME_ID

Get Internal Ip of ec2 with a specific tag

aws ec2 describe-instances --filters Name=tag:created_by,Values=shivam   | jq -r '.Reservations[]?.Instances[]? | .PrivateIpAddress'

Execute Lambda Remotely

aws lambda invoke \
    --cli-binary-format raw-in-base64-out \
    --function-name FUNCTION_NAME \
    --log-type Tail \
    --payload '{"key": "value"}' \
    response.json

Iterate through all files in an s3 bucket and validate all cloudformation templates

yml_objects=$(aws s3api list-objects --bucket $BUCKET_NAME --prefix $ENVIRONMENT_NAME_ALIAS/templates --query "Contents[].Key" --output json --profile $PROFILE)


for file in $(echo $yml_objects | jq -r '.[]'); do
  if [[ $file == *.yml ]]; then
    TEMPLATE_URL="https://${BUCKET_NAME}.s3.amazonaws.com/${file}"
    echo "URL: $TEMPLATE_URL"
    aws cloudformation validate-template --template-url "${TEMPLATE_URL}" --profile $PROFILE 1>/dev/null
  fi
done