Skip to content

Instantly share code, notes, and snippets.

@enriquemanuel
Last active October 7, 2019 17:44
Show Gist options
  • Save enriquemanuel/ebdb3caf7605af66d787025b165a6228 to your computer and use it in GitHub Desktop.
Save enriquemanuel/ebdb3caf7605af66d787025b165a6228 to your computer and use it in GitHub Desktop.
Tagging AWS Resources via Bash
#!/bin/bash
# CloudWatch Logs
for log in `aws logs describe-log-groups | jq -r ".logGroups[].logGroupName" | grep appeals`; do
env=""
if echo "$log" | grep -q "dev"; then
env="dev"
elif echo "$log" | grep -q "prod"; then
env="prod"
else
env="utility"
fi
aws logs tag-log-group --log-group-name $log --tags Owner=me,ProjectShort=Lalaland,ProjectName=lala,Environment=${env}
done
# RDS
for instance in `aws rds describe-db-instances --query 'DBInstances[*].[DBInstanceArn]' | jq -r ".[]" | grep appeals`; do
env=$(echo $instance | cut -d "-" -f 9);
aws rds add-tags-to-resource --resource-name $instance --tags Key=Owner,Value=me Key=ProjectShort,Value=Lalaland Key=ProjectName,Value=lala Key=Environment,Value=${env}
done
# RDS Snapshots
for snapshot in `aws rds describe-db-snapshots --query 'DBSnapshots[*].DBSnapshotArn' | jq -r ".[]" | grep appeals`; do
env=$(echo $instance | cut -d "-" -f 9);
aws rds add-tags-to-resource --resource-name $snapshot --tags Key=Owner,Value=me Key=ProjectShort,Value=Lalaland Key=ProjectName,Value=lala Key=Environment,Value=${env}
done
# lambdas
for function in `aws lambda list-functions --query 'Functions[*].FunctionArn' | jq -r ".[]" | grep appeals`; do
env="all"
aws lambda tag-resource --resource $function --tags Owner=me,ProjectShort=Lalaland,ProjectName=lala,Environment=${env}
done
# EC2 AMIs
for amiName in `aws ec2 describe-images --owners 008577686731 --query 'Images[*].[Name]' | jq -r ".[]" | grep appeals`; do
for ami in `aws ec2 describe-images --owners 008577686731 --filters Name=name,Values=$amiName --query 'Images[*].[ImageId]' --output text`; do
aws ec2 create-tags --resources $ami --tags Key=Owner,Value=me Key=ProjectShort,Value=Lalaland Key=ProjectName,Value=lala Key=Environment,Value=${env}
done
done
# S3
## Buckets and Objects
for bucket in `aws s3api list-buckets --query "Buckets[].Name" | jq -r ".[]" | grep appeals`; do
aws s3api put-bucket-tagging --bucket $bucket --tagging "TagSet=[{Key=Owner,Value=me},{Key=ProjectShort,Value=Lalaland},{Key=ProjectName,Value=lala},{Key=Environment,Value=all}]"
done
# DMS
aws dms add-tags-to-resource --resource-arn arn:aws-us-gov:dms:us-gov-west-1:account:task:GTEUY32FJFAAKNMDO5L6DX2OLY --tags Key=Owner,Value=me Key=ProjectShort,Value=laland Key=ProjectName,Value=lala Key=Environment,Value=prod
# ECR
aws ecr tag-resource --tags Key=Owner,Value=me Key=ProjectShort,Value=laland Key=ProjectName,Value=lala Key=Environment,Value=prod --resource-arn arn:aws-us-gov:ecr:us-gov-west-1:account:repository/repo
# SQS (not working in govcloud)
aws sqs tag-queue --tags Owner=me,ProjectShort=laland,ProjectName=lala,Environment=staging --queue-url $queue https://us-gov-west-1.queue.amazonaws.com/account/sqs_url
# SNS
aws sns tag-resource --resource-arn arn:aws-us-gov:sns:us-gov-west-1:account:arn-resource --tags Key=Owner,Value=me Key=ProjectShort,Value=laland Key=ProjectName,Value=lala Key=Environment,Value=prod
# Elasticache
aws elasticache add-tags-to-resource --tags Key=Owner,Value=me Key=ProjectShort,Value=Laland Key=ProjectName,Value=lala Key=Environment,Value=prod --resource-name
# CW Events
for event in `aws events list-rules | jq -r ".Rules[].Arn" | grep appeals`; do
env=""
if echo "$event" | grep -q "dev"; then
env="dev"
elif echo "$event" | grep -q "prod"; then
env="prod"
elif echo "$event" | grep -q "uat"; then
env="utility"
fi
aws events tag-resource --resource-arn $event --tags Key=Owner,Value=me Key=ProjectShort,Value=Laland Key=ProjectName,Value=lala Key=Environment,Value=$env
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment