Skip to content

Instantly share code, notes, and snippets.

@hyavari
Created February 16, 2024 22:56
Show Gist options
  • Save hyavari/9d55f1fe4e0f608eaa8f5c415cc6a73a to your computer and use it in GitHub Desktop.
Save hyavari/9d55f1fe4e0f608eaa8f5c415cc6a73a to your computer and use it in GitHub Desktop.
CloudWatch Metric Importer
#!/bin/bash
# @Author: Hossein Yavari
# @Date: 2024-02-16
# @Description: This script helps you to move CloudWatch metric filters from one account to another.
#importCmd="aws logs put-metric-filter --region <region> --profile <profile> --cli-input-json <input_file>"
#exportCmd="aws logs describe-metric-filters --region <region> --profile <profile> --log-group-name <log_group> --output json > <output_file>"
# Define usage function
usage() {
echo "Usage: $0 [options] [arguments]"
echo ""
echo "Options:"
echo " -i Import the metric filters from the JSON file"
echo " --input <json_file> --profile <sso_profile_name> --region <region>"
echo " -e Export the metric filters from the source account and import them into the destination account"
echo " --profile <sso_profile_name> --region <region> --log-group <log_group_name>"
echo " --dst-profile <destination_sso_profile_name> --dst-region <destination_region>"
echo " -h, ? Help"
echo "Arguments:"
echo " --input <json_file> The JSON file containing the metric filters to be moved"
echo " --profile <sso_profile_name> The name of the SSO profile to use for the source account"
echo " --region <region> The region where the metric filters are located"
echo " --log-group <log_group_name> The name of the log group where the metric filters are located"
echo " --dst-profile <destination_sso_profile_name> The name of the SSO profile to use for the destination account"
echo " --dst-region <destination_region> The region where the metric filters will be imported"
exit 1
}
help() {
echo "This script helps you to move CloudWatch metric filters from one account to another."
echo "It uses the AWS CLI to export the metric filters from the source account and import them into the destination account."
echo "Also, the script can take the JSON file as input, which contains the metric filters to be moved."
echo ""
usage
}
importFilters() {
json_file=$1
profile=$2
region=$3
json=$(cat "$json_file")
echo "$json" | jq -c '.metricFilters[]' | while IFS= read -r filter; do
# Remove the "creationTime" field
filter=$(echo "${filter}" | jq 'del(.creationTime)')
# Construct the command
input=$(echo "${filter}" | jq -c | jq -R)
command="aws logs put-metric-filter --region $region --profile $profile --cli-input-json $input"
eval "${command}"
done
}
exportFiltersAndImport() {
profile=$1
region=$2
log_group=$3
dst_profile=$4
dst_region=$5
# Export the metric filters
output_file="metric_filters.json"
command="aws logs describe-metric-filters --region $region --profile $profile --log-group-name $log_group --output json > $output_file"
eval "${command}"
if [ ! -f "$output_file" ]; then
echo "Failed to export the metric filters"
exit 1
fi
# Import the metric filters
importFilters "$output_file" "$dst_profile" "$dst_region"
if [ -f "$output_file" ]; then
rm "$output_file"
fi
}
# Check if the number of arguments is correct
if [ "$#" -eq 0 ]; then
usage
fi
if [[ "$*" == *'?'* ]]; then
help
fi
# Parse command-line options
while getopts ":i:e:h?" opt; do
case $opt in
h)
help
;;
i)
import=true
;;
e)
export=true
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
esac
done
shift "$((OPTIND - 2))"
# Parse command-line arguments
if [[ -n "$import" ]]; then
while [[ $# -gt 0 ]]; do
arg="$1"
if [[ $2 == "" ]]; then
echo "Missing argument for $arg" >&2
usage
fi
case $arg in
--input)
input="$2"
shift 2
;;
--profile)
profile="$2"
shift 2
;;
--region)
region="$2"
shift 2
;;
*)
echo "Invalid argument: $arg" >&2
exit 1
;;
esac
done
if [[ -z "$input" ]] || [[ -z "$profile" ]] || [[ -z "$region" ]]; then
echo "Missing required arguments" >&2
usage
fi
echo "Importing '${input}' to '${region}' using profile '${profile}'"
importFilters "$input" "$profile" "$region"
else if [[ -n "$export" ]]; then
while [[ $# -gt 0 ]]; do
arg="$1"
if [[ $2 == "" ]]; then
echo "Missing argument for $arg" >&2
usage
fi
case $arg in
--profile)
profile="$2"
shift 2
;;
--region)
region="$2"
shift 2
;;
--log-group)
log_group="$2"
shift 2
;;
--dst-profile)
dst_profile="$2"
shift 2
;;
--dst-region)
dst_region="$2"
shift 2
;;
*)
echo "Invalid argument: $arg" >&2
exit 1
;;
esac
done
if [[ -z "$profile" ]] || [[ -z "$region" ]] || [[ -z "$log_group" ]] || [[ -z "$dst_profile" ]] || [[ -z "$dst_region" ]]; then
echo "Missing required arguments" >&2
usage
fi
echo "Exporting '${log_group}' from '${region}' using '${profile}' and importing to '${dst_region} using '${dst_profile}'"
exportFiltersAndImport "$profile" "$region" "$log_group" "$dst_profile" "$dst_region"
else
echo "Invalid option: -$OPTARG" >&2
usage
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment