Skip to content

Instantly share code, notes, and snippets.

@muhannad0
Created January 8, 2024 05:49
Show Gist options
  • Save muhannad0/45725e53805a4cba0da51a49aefd6c00 to your computer and use it in GitHub Desktop.
Save muhannad0/45725e53805a4cba0da51a49aefd6c00 to your computer and use it in GitHub Desktop.
quick-aws-cli-scripts

AWS CLI scripts for quick tasks

Get all the EC2 instances with a specific Name tag, and add/update tags on those resources

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId, Tags[?Key==`Name` && starts_with(Value, `dev`)]]' --output json | jq -r '.[][] | select(.[1] | length > 0) | .[0]' | \
while read instance_id; do
  aws ec2 create-tags --resources "$instance_id" --tags Key=Environment,Value=development
done
  • jq used to filter out resources that have empty tags and only output the instance ID.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment