Skip to content

Instantly share code, notes, and snippets.

@dmc5179
Created February 24, 2026 01:14
Show Gist options
  • Select an option

  • Save dmc5179/b835a44227c96e7cc977a396de863256 to your computer and use it in GitHub Desktop.

Select an option

Save dmc5179/b835a44227c96e7cc977a396de863256 to your computer and use it in GitHub Desktop.
Bash Script to tag AWS resources in bulk
#!/bin/bash
# Configuration
APP_CODE_VALUE=""
SERVICE_PHASE_VALUE=""
COST_CENTER_VALUE=""
REGION="us-east-1" # Change to your target region
TAG_SET="app-code=${APP_CODE_VALUE},service-phase=${SERVICE_PHASE_VALUE},cost-center=${COST_CENTER_VALUE}"
echo "Step 1: Gathering all resource ARNs in $REGION..."
# Fetch all ARNs using the Tagging API.
# We use --query to get just the ARNs and output as text.
RESOURCE_ARNS=$(aws resourcegroupstaggingapi get-resources \
--region "$REGION" \
--query 'ResourceTagMappingList[*].ResourceARN' \
--output text)
if [ -z "$RESOURCE_ARNS" ]; then
echo "No resources found to tag."
exit 0
fi
# Convert the space-separated string into an array
ARNS_ARRAY=($RESOURCE_ARNS)
TOTAL_COUNT=${#ARNS_ARRAY[@]}
echo "Found $TOTAL_COUNT resources. Starting tagging process..."
# The Tagging API allows a maximum of 20 resources per call.
# We will loop through the array in chunks of 20.
BATCH_SIZE=20
for ((i=0; i<$TOTAL_COUNT; i+=$BATCH_SIZE)); do
# Slice the array to get the current batch
BATCH=("${ARNS_ARRAY[@]:i:BATCH_SIZE}")
echo "Tagging batch $((i/BATCH_SIZE + 1)) of $(((TOTAL_COUNT + BATCH_SIZE - 1)/BATCH_SIZE))..."
aws resourcegroupstaggingapi tag-resources \
--region "$REGION" \
--resource-arn-list ${BATCH[@]} \
--tags "$TAG_SET"
# Optional: Brief sleep to avoid hitting API rate limits (Throttling)
sleep 0.5
done
echo "Bulk tagging complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment