Last active
August 7, 2024 10:33
-
-
Save huevos-y-bacon/00fc7ea52728ca45cfd1e3bac26b7185 to your computer and use it in GitHub Desktop.
AWS WAFR - Export AWS Well-Architected Tool Workload ARNs to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Export AWS Well-Architected Tool Workload ARNs to CSV | |
usage(){ | |
echo "EXPORT WELL-ARCHITECTED TOOL WORKLOAD ARNs TO CSV | |
ARGUMENTS: | |
-d | --days \${number of days} - number of days to include (since last update). Default 3650 | |
-u | --upload | --arns - output APN portal upload-friendly CSV of ARNs only | |
-h | --help - this output | |
PREREQUISITES: | |
- awscli | |
- jq | |
- awsume (or another method of getting a session token) | |
BEFORE YOU RUN THIS: | |
- Pipe the output to a file (csv) to open it in Excel | |
- Use e.g. 'awsume' to change to an IAM user (not role) profile, then | |
- Run 'aws-get-session-token' to get a session token using MFA, which whould allow | |
running 'wellarchitected' API calls against the WAT account | |
EXAMPLES: | |
- Extract ARNs for workload updated in the last 90 days, in the format | |
uploadable to the APN portal: | |
$(basename "$0") -d 90 -u | |
- Extract ARNs for workload updated in the last 90 days, in the format | |
uploadable to the APN portal, output to CSV file | |
$(basename "$0") -d 90 -u > wat-workloads-\$(date +%F).csv | |
- Extract CSV table of all workloads, including NAME and DATE | |
$(basename "$0") | |
KNOWN ISSUES: | |
- Maximum number workloads that can be output at once is 50 | |
" | |
} | |
arns(){ | |
aws wellarchitected list-workloads \ | |
--query 'WorkloadSummaries[?UpdatedAt>=`'"${STARTDATE}"'`].{ARN:WorkloadArn}' \ | |
--out json \ | |
--max-results 50 \ | |
--no-cli-pager \ | |
| jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' | |
} | |
full(){ | |
aws wellarchitected list-workloads \ | |
--query 'WorkloadSummaries[?UpdatedAt>=`'"${STARTDATE}"'`].{NAME:WorkloadName, ARN:WorkloadArn, UPDATED:UpdatedAt}' \ | |
--out json \ | |
--max-results 50 \ | |
--no-cli-pager \ | |
| jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' | |
} | |
STARTDATE=$(date -v-3650d +"%Y-%m-%d") | |
POSITIONAL_ARGS=() | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-d|--days) | |
DAYS="$2" | |
STARTDATE=$(date -v-${DAYS}d +"%Y-%m-%d") | |
shift # past argument | |
shift # past value | |
;; | |
-u|--upload|upload|--arns|arns) | |
UPLOAD=1 | |
shift # past argument | |
# shift # past value# | |
;; | |
--help|help|-h) | |
usage | |
exit 0 | |
;; | |
-*|--*) | |
echo "Unknown option $1" | |
exit 1 | |
;; | |
*) | |
POSITIONAL_ARGS+=("$1") # save positional arg | |
shift # past argument | |
;; | |
esac | |
done | |
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters | |
>&2 echo -e "START DATE=$STARTDATE\n" | |
if [[ -n $UPLOAD ]]; then arns; else full; fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment