AWS CLI BASH Cheat Sheet

Count the number of EC2 instances

aws ec2 describe-instances | jq -r '.[] | length'

 

Show running instances as CSV line data

aws ec2 describe-instances \
  --filter 'Name=tag:Name,Values=api' \
           'Name=instance-state-name,Values=running' | \
  jq -r '.Reservations[].Instances[] | [.InstanceId, .PrivateIpAddress, .Tags[].Value] | @csv'

 

Instances status, (InstanceState.Name field).

aws ec2 describe-instance-status \
  --include-all-instances --instance-ids $INSTANCE 2> /dev/null | \
  jq -r '.InstanceStatuses[].InstanceState.Name'

 

Instances without an owner

aws ec2 describe-instances \
  --query "Reservations[].Instances[].{ID: InstanceId, Tag: Tags[].Key}" --output json | \
  jq -c '.[]' | grep -vi owner | \
  jq -r '.ID' | awk -v ORS=' ' '{ print $1  }' | sed 's/ $//'

 

EC2 instances without expires tag: (aws ec2 terminate-instances --instance-ids)

aws ec2 describe-instances \
  --query "Reservations[].Instances[].{ID: InstanceId, Tag: Tags[].Key}" --output json | \
  jq -c '.[]' | grep -vi expires | \
  jq -r '.ID' | awk -v ORS=' ' '{ print $1   }' | sed 's/ $//'

Count the number of S3 buckets

aws s3 ls | wc -l

 

When was the user created

aws iam get-user | jq -r ".User.CreateDate[:4]"

 

Creating EC2 Instances…

Step 1: Find the right AMI (this is slow, ’cause there are a *lot* of AMIs) and hold it in an environment variable:

export AMI_ID=$(aws ec2 describe-images --owners amazon | jq -r ".Images[] | { id: .ImageId, desc: .Description } | select(.desc?) | select(.desc | contains(\"Amazon Linux 2\")) | select(.desc | contains(\".NET Core 2.1\")) | .id")

Step 2: Create a key pair, and hold on to it in a file:

aws ec2 create-key-pair --key-name aurora-test-keypair > keypair.pem

Step 3: Create the instance using the AMI and the key pair, and hold onto the result in a file:

aws ec2 run-instances --instance-type t2.micro --image-id $AMI_ID --region us-east-1 --subnet-id <your_subnet_id> --key-name keypair --count 1 > instance.json

Step 4: Grab the instance Id from the file:

export INSTANCE_ID=$(jq -r .Instances[].InstanceId instance.json)

Step 5: Wait for the instance to spin-up, then grab it’s IP address and hold onto it in an environment variable:

export INSTANCE_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --output text --query 'Reservations[*].Instances[*].PublicIpAddress')

How much data is in each of my buckets

for bucket in $(aws s3api list-buckets --query "Buckets[].Name" --output text); do aws cloudwatch get-metric-statistics --namespace AWS/S3 --metric-name BucketSizeBytes --dimensions Name=BucketName,Value=$bucket Name=StorageType,Value=StandardStorage --start-time $(date --iso-8601)T00:00 --end-time $(date --iso-8601)T23:59 --period 86400 --statistic Maximum | echo $bucket: $(numfmt --to si $(jq -r ".Datapoints[0].Maximum // 0")); done;

How much does my bucket cost

for bucket in $(aws s3api list-buckets --query "Buckets[].Name" --output text); do aws cloudwatch get-metric-statistics --namespace AWS/S3 --metric-name BucketSizeBytes --dimensions Name=BucketName,Value=$bucket Name=StorageType,Value=StandardStorage --start-time $(date --iso-8601)T00:00 --end-time $(date --iso-8601)T23:59 --period 86400 --statistic Maximum | echo $bucket: \$$(jq -r "(.Datapoints[0].Maximum //
 0) * .023 / (1024*1024*1024) * 100.0 | floor / 100.0"); done;

 

 

Tieing it all together


show_stopped()
{
  local PROFILE=${1:-london}
  aws ec2 describe-instances   --filter  'Name=instance-state-name,Values=stopped' \
                               --profile ${PROFILE:-london} | \
                               jq -r '.Reservations[].Instances[] | [.InstanceId,  .Tags[].Value] | @csv'
}

show_running()
{
  local PROFILE=${1:-london}
  aws ec2 describe-instances   --filter  'Name=instance-state-name,Values=running' \
                               --profile ${PROFILE:-london} | \
                               jq -r '.Reservations[].Instances[] | [.InstanceId,  .Tags[].Value] | @csv'
}

start_instance()
{
  local PROFILE=${1:-london}
  # Show stopped instances
  show_stopped ${PROFILE}

}

show_instances()
{
  local GREP=${1:-""}
  local lst_PROFILES=$(aws-profile $GREP)
 for PROFILE in ${lst_PROFILES} ; do 
    echo $PROFILE
    aws ec2 describe-instances --profile ${PROFILE} | \
      jq -r '.Reservations[].Instances[] |   select(.LaunchTime > "2015-01-28") |   select(.State.Code != 48) |   [.InstanceId, .State.Name, (.Tags[]|select(.Key=="Name")|.Value) ]| @csv'
  done
}

start_instance()
{
  local PROFILE=${PROFILE:-london}
  local INSTANCE=${1:-""}
  if [[ $(show_stopped | grep $INSTANCE) ]] ; then
    aws ec2 start-instances --instance-ids $INSTANCE
  else
    if [[ $(show_instances | grep $INSTANCE) ]] ; then
      echo "Not stopped"
    else
      echo "Cannot find instance $INSTANCE in $(aws-city $PROFILE)"
    fi
  fi
}

stop_instance()
{
  local PROFILE=${PROFILE:-london}
  local INSTANCE=${1:-""}
  if [[ $(show_running | grep $INSTANCE) ]] ; then
    aws ec2 stop-instances --instance-ids $INSTANCE
  else
    if [[ $(show_instances | grep $INSTANCE) ]] ; then
      echo "Not stopped"
    else
      echo "Cannot find instance $INSTANCE in $(aws-city $PROFILE)"
    fi
  fi
}

no_owner()
{
  local GREP=${1:-""}
  local lst_PROFILES=$(aws-profile $GREP)
  for PROFILE in ${lst_PROFILES} ; do 
    echo $PROFILE
    aws ec2 describe-instances --profile $PROFILE --query "Reservations[].Instances[].{ID: InstanceId, Tag: Tags[].Key}" --output json | jq -c '.[]' | grep -vi owner | jq -r '.ID' | awk -v ORS=' ' '{ print $1  }' | sed 's/ $//'
  done
}