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
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
}'
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
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'