Skip to content

Instantly share code, notes, and snippets.

@BondAnthony
Last active January 29, 2024 19:12
Show Gist options
  • Save BondAnthony/81ca5f70dba9e1c9537e0d0bf7ee63aa to your computer and use it in GitHub Desktop.
Save BondAnthony/81ca5f70dba9e1c9537e0d0bf7ee63aa to your computer and use it in GitHub Desktop.
Those useful AWS cli commands
for i in $(aws ec2 describe-regions | jq -r '.Regions[].RegionName'); do
echo "Region ${i} --------"
aws ec2 describe-vpcs --region ${i} | jq '.Vpcs[].CidrBlock'
aws ec2 describe-subnets --region ${i} | jq '.Subnets[].CidrBlock'
done

Fetch Volumes

Return volumes with a state of available and tag name kubernetes.io/created-for/pv/name. Further filtering of the final output is done by extracting additional data from the response.

aws ec2 describe-volumes \
--region us-east-1 \
--filters \
    Name=status,Values=available \
    Name=tag-key,Values=KubernetesCluster | \
jq -r '.Volumes[] |
    {
        volume: .VolumeId,
        State: .State,
        Size: .Size,
        pvc: .Tags[]|
            select(
                .Key=="kubernetes.io/created-for/pv/name"
            )|
            .Value,
        pvc_name: .Tags[]|
            select(
                .Key=="kubernetes.io/created-for/pvc/name"
            )|
            .Value,
        namespace: .Tags[]|
            select(
                .Key=="kubernetes.io/created-for/pvc/namespace"
            )|
            .Value
    }'

Find Stopped Instances

This will search AWS for all stopped instances and format the output in table with headers.

aws ec2 describe-instances \
  --region "$region" \
  --filters "Name=instance-state-name,Values=stopped" \
  --query 'Reservations[].Instances[].{ID:InstanceId,Type:InstanceType,AZ:Placement.AvailabilityZone,State:State.Name,TransitionReason:StateTransitionReason,Name:Tags[?Key==`Name`].Value | [0],ClusterID:Tags[?Key==`ClusterID`].Value | [0]}' \
  --output table

Snapshots

When you have 10s of 1,000s of snapshots sitting in AWS the console becomes unusable. Hopefully you utilized tagging because that's the only efficent way of finding information in AWS. This will return all the snapshots with a tag of Cluster and value mycluster-1. This will also simplify the output to the snapshotID, time, name, and volumeID

aws ec2 describe-snapshots \
  --region us-east-1 \
  --filters Name=tag:Cluster,Values="mycluster-1" \
  --query 'Snapshots[].{Time:StartTime,Name:Description,ID:SnapshotId,VolumeID:VolumeId}' \
  --output table

and when you need to filter the output just add sort_by() with the right properties

aws ec2 describe-snapshots \
  --region eu-west-1 \
  --filters Name=tag:Cluster,Values="mycluster-1" \
  --query 'Snapshots[].{Time:StartTime,Name:Description,ID:SnapshotId,VolumeID:VolumeId} | sort_by([], &Time)' \
  --output table

Return a tag value in the output.

--query 'Snapshots[].{Time:StartTime,Name:Tags[?Key==`Name`]| [0].Value,ID:SnapshotId,VolumeID:VolumeId} | sort_by([], &Time)'

JQ output to a single line give the input is an object.

jq -r '.| "\(.name)|\(.port)"'

JQ find the size of a policy document in CloudFormation

yq . cfn.yaml |jq '.Resources | to_entries | .[].value | select(.Type=="AWS::IAM::ManagedPolicy") | .Properties.PolicyDocument | tostring | length'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment