Skip to content

Instantly share code, notes, and snippets.

@stephanfuchs
Forked from weyderfs/awscli_jq_tricks.sh
Created June 10, 2024 07:06
Show Gist options
  • Select an option

  • Save stephanfuchs/57b2efd7540432b2514b16a604e5cde8 to your computer and use it in GitHub Desktop.

Select an option

Save stephanfuchs/57b2efd7540432b2514b16a604e5cde8 to your computer and use it in GitHub Desktop.
AWS CLI JQ Tricks - Outputs likes to export for CSV file
#List AMI's group by AMIId, Description and sorting to turns easy identify duplicates
aws ec2 describe-snapshots --region <the-region> --owner-id <123456789> | jq -r '.Snapshots[] | [.SnapshotId,.Description] | join(" ")' | sort
#List / Describe AWS Regions group by RegionName
aws ec2 describe-regions --region us-east-1 | jq -r '.Regions[].RegionName'
#List DyamoDB Table Names
aws dynamodb list-tables --region sa-east-1 | jq -r '.TableNames[]' | sort
#List EC2 Instances group by Tag Name and InstanceType
aws ec2 describe-instances --region sa-east-1 | jq '.Reservations[].Instances[] | [.InstanceType,.Tags[].Value] | join(";")'
#List EC2 AMI's group by ID and TagName handling with null values
aws ec2 describe-images --owners <owner-id> --region sa-east-1 | jq '.Images[] | [.ImageId,.Tags[]?.Value] | join(";")'
#List Elasticache Clusters
aws elasticache describe-cache-clusters --region sa-east-1 | jq -r '.CacheClusters[].CacheClusterId' | sort
#List IAM Policies Custom Managed
aws iam list-policies --scope Local | jq -r '.Policies[] | .PolicyName'
#List IAM Roles
aws iam list-roles | jq -r '.Roles[] | .RoleName'
#List IAM Users
aws iam list-users | jq -r '.Users[] | .UserName'
#List Lambda Function by Name
aws lambda list-functions --region sa-east-1 | jq -r '.Functions[] | .FunctionName'
#Listing RDS Instances group by Name and InstanceType
aws rds describe-db-instances --region sa-east-1 | jq -r '.DBInstances[]|[.DBInstanceIdentifier,.DBInstanceClass] | join(";")'
#List RDS Snapshots group by SnapshotIdentifier and InstanceIdentifier
aws rds describe-db-snapshots --region sa-east-1 | jq '.DBSnapshots[] | [.DBSnapshotIdentifier,.DBInstanceIdentifier] | join (";")'
#List SNS Subscription group by ARN and Endpoint
aws sns list-subscriptions --region sa-east-1 | jq -r '.Subscriptions[] | [.SubscriptionArn,.Endpoint] | join(";")'
#List SNS Topic group by Name
ws sns list-subscriptions --region sa-east-1 | jq -r '.Subscriptions[].TopicArn' | cut -d: -f6 | sort
#List SQS Queues group by Name
aws sqs list-queues --region sa-east-1 | jq -r '.QueueUrls[]' | cut -d/ -f5
#List Load Balancers group by LBName and DNSName
aws elb describe-load-balancers --region sa-east-1 | jq -r '.LoadBalancerDescriptions[] | [.LoadBalancerName,.DNSName] | join(";")
#List EC2 Volumes group by TagsName, VolumeId and InstanceId
aws ec2 describe-volumes --region sa-east-1 | jq '.Volumes[] | {Name:.Tags[]?.Value,Specs:([.Attachments[].VolumeId,.Attachments[].InstanceId] | join(";"))} | join(";")'
#List S3 Buckets showing only Name
aws s3 ls | awk '{for(i=3;i<=NF;++i)print $i}'
#List Transit Gateways by TagName
aws ec2 describe-transit-gateways --region sa-east-1 | jq -r .'TransitGateways[] | .Tags[]?.Value'
#Delete CloudFormation Stacks
for i in $(aws cloudformation list-stacks |jq -r '.StackSummaries[] | {StackName} | join(" ")')
do echo aws cloudformation delete-stack --stack-name $i #remove echo to delete
done
#Get Buckets s3 and concatenating it with its policy
for x in $(aws s3 ls | awk '{for(i=3;i<=NF;++i)print $i}')
do
echo aws s3api get-bucket-policy-status --bucket $x
done
#Get SQS Queue Name
aws sqs list-queue-tags --region sa-east-1 --queue-url <queue-url>| jq -r '.Tags.Name'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment