AWS cheat sheet

From Coolscript
Jump to navigation Jump to search

Identity

aws sts get-caller-identity

SSO Login

aws sso login --profile ProfileName

Secrets

  • List secret
aws secretsmanager list-secrets
  • Get secret
aws secretsmanager get-secret-value --secret-id "SecretName"

Instances

  • List
aws ec2 describe-instances
  • Filter by ip address
aws ec2 describe-instances --filters Name=private-ip-address,Values=x.x.x.x
  • Query by ID, Time and IP
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId, LaunchTime, PrivateIpAddress]' --output text
  • Query by Tags, ID, VolumeID
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value,InstanceId,BlockDeviceMappings[*].Ebs.VolumeId]'
  • Query by ClusterName, show Ami ImageId
aws ec2 describe-instances --filters "Name=tag:SomeTag,Values=SomeVal" --query 'Reservations[*].Instances[*].ImageId' --output text
  • Query by ClusterName, show Ami ImageId,PrivateIp
aws ec2 describe-instances --filters "Name=tag:SomeTag,Values=SomeVal" --query 'Reservations[*].Instances[*].[ImageId, PrivateIpAddress]' --output text

EKS

  • Update Cluster in Kubeconfig
aws eks update-kubeconfig --name ClusterName
  • List clusters
aws eks list-clusters
  • List Nodegroups on Clustername
aws eks list-nodegroups --cluster-name ClusterName
  • Describe Nodegroup
aws eks describe-nodegroup --cluster-name ClusterName --nodegroup-name NodegroupName
  • Describe Nodegroup, Query created at
aws eks describe-nodegroup --cluster-name ClusterName --nodegroup-name NodegroupName | jq '.nodegroup.createdAt' | tr -d '"'

Volumes

  • Filter by state
aws ec2 describe-volumes --filters Name=status,Values=in-use
aws ec2 describe-volumes --filters Name=status,Values=available
  • List by Tag
aws ec2 describe-volumes --filters "Name=SomeTagName,Values=Tag-Value"
  • Count available disks
aws ec2 describe-volumes --filters Name=status,Values=available --query 'Volumes[*].{ID:VolumeId}' --output text | wc -l
  • Sum of all available disks
aws ec2 describe-volumes --filters Name=status,Values=in-use | jq "[.Volumes[].Size] | add"
  • Filter by state=in-use, query IDs, AZ, Size
aws ec2 describe-volumes --filters Name=status,Values=in-use --query 'Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,AZ:AvailabilityZone,Size:Size}' --output text
  • Delete volumes with state available
for vol in $(aws ec2 describe-volumes  | jq -r ".Volumes[] | select(.State==\"available\") | .VolumeId"); do echo $vol ; $(aws ec2 delete-volume --volume-id $vol) ; done
  • List/Loop by create date after 2023-09-29
for vol in $(aws ec2 describe-volumes --query "Volumes[?(CreateTime>'2023-09-29')].[VolumeId]" --output text); do echo "$vol" ; done
  • List by create date before 5 days or before 1 month
aws ec2 describe-volumes --query "Volumes[?(CreateTime<'$(date --date='-5 day' '+%Y-%m-%d')')].[VolumeId, CreateTime]" --output text
aws ec2 describe-volumes --query "Volumes[?(CreateTime<'$(date --date='-1 month' '+%Y-%m-%d')')].[VolumeId, CreateTime]" --output text
  • Loop by create date before 5 days
for vol in $(aws ec2 describe-volumes --query "Volumes[?(CreateTime<'$(date --date='-5 day' '+%Y-%m-%d')')].[VolumeId]" --output text); do echo "Volume $vol" ; done

Autoscaling

  • List
aws autoscaling describe-auto-scaling-groups

Parameter Store

aws ssm get-parameters-by-path --path "/somepath" --recursive --query "Parameters[*].Name"


AMI Image

  • List by AmiID
aws ec2 describe-images --image-ids ami-xyz
  • List by AmiID, Return Creation Date
aws ec2 describe-images --image-ids ami-xyz --query 'Images[*].[CreationDate]' --output text

S3

  • List all files/objects within a bucket
aws s3api list-objects --bucket my.bucket.name --output json
  • Sum of size and length
aws s3api list-objects --bucket my.bucket.name --output json --query "[sum(Contents[].Size), length(Contents[])]"

Reference