Skip to content

Instantly share code, notes, and snippets.

@marcaurele
Last active March 7, 2024 10:32
Show Gist options
  • Save marcaurele/82acf52b1d7ea369b25270b91802038f to your computer and use it in GitHub Desktop.
Save marcaurele/82acf52b1d7ea369b25270b91802038f to your computer and use it in GitHub Desktop.

AWS cli Cheat Sheet

AMI

Get the Linux 2023 images for x86_84 sorted by the latest:

aws ec2 describe-images --filters "Name=name,Values=al2023-ami-*" Name=architecture,Values=x86_64 Name=virtualization-type,Values=hvm Name=root-device-type,Values=ebs --query 'sort_by(Images, &CreationDate)[-7:]'

EC2

  • by id: aws ec2 describe-instances --instance-ids i-xxxxx
  • by tags: aws ec2 describe-instances --filters Name=tag:role,Values=server Name=tag:Stack,Values=production
  • by private ip: aws ec2 describe-instances --filters Name=private-ip-address,Values=
  • scale: aws ec2 modify-instance-attribute --instance-id=i-xxxxxx --instance-type '{"Value": "m6i.2xlarge"}'
  • extract only one attribute, for example PrivateIpAddress: aws ec2 describe-instances --query "Reservations[*].Instances[*].PrivateIpAddress"

Specials

Get the instance types:

aws ec2 describe-instance-types --filters Name=current-generation,Values=true | \
  jq ".InstanceTypes | .[] | {name: .InstanceType, memory_in_mib: .MemoryInfo.SizeInMiB, vcpus: .VCpuInfo.DefaultVCpus, disk_in_gb: .InstanceStorageInfo.TotalSizeInGB}"

or

aws ec2 describe-instance-types --filters Name=current-generation,Values=true | \
  jq -c '.InstanceTypes | reduce .[] as $i ({}; .[$i.InstanceType] = { memory_in_mib: $i.MemoryInfo.SizeInMiB, vcpus: $i.VCpuInfo.DefaultVCpus, disk_in_gb: $i.InstanceStorageInfo.TotalSizeInGB})'

ECS

Batch

  • get queues: aws batch describe-job-queues
  • list jobs per queue: aws batch list-jobs --job-queue default
  • list waiting jobs (default = RUNNING): aws batch list-jobs --job-queue default --status RUNNABLE
  • JQ extension to turn timestamp to datetime: jq '.jobSummaryList[].createdAt |= (. / 1000 | round | todateiso8601) | .jobSummaryList[].startedAt |= (if . > 0 then (. / 1000 | round | todateiso8601) else "na" end) | .jobSummaryList[].stoppedAt |= (if . > 0 then (. / 1000 | round | todateiso8601) else "na" end)'
  • fetch details about a job based on its uuid: aws ecs describe-tasks --cluster --tasks
  • search jobs by their name: aws batch list-jobs --job-queue default --filters name=JOB_NAME,values=

Services

Compared services between regions:

$ wget https://api.regional-table.region-services.aws.a2z.com/index.json
$ jq -r '.prices[] | select(.id | endswith(":eu-central-1")) .attributes."aws:serviceName"' < index.json | sort > eu-central-1
$ jq -r '.prices[] | select(.id | endswith(":eu-central-2")) .attributes."aws:serviceName"' < index.json | sort > eu-central-2
$ diff -u eu-central-1 eu-central-2

Jobs

Quickly scan job results to see queue time more than 5 minutes:

aws batch list-jobs --job-queue default --job-status SUCCEEDED | \
  jq '.jobSummaryList[] | . += {"queuedMinutes": ((.startedAt - .createdAt)/6000)|round, "createdDateTime": ((.createdAt/1000) | round | todateiso8601)} | select(.queuedMinutes > 5)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment