Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/bin/bash
#
# Action
# checks AWS events and show lists of events.
#
# Precondition
# - Install aws cli
# - aws configured and create .aws/config.
# - Install jq
#
# Authored by @kenzo0107
set -eu
readonly AWS_CONFIG=~/.aws/config
function get_aws_events_by_profile() {
local _profile=$1
local _ret
local _ret_ec2
local _ret_rds
_ret_ec2=$(aws --profile $_profile ec2 describe-instance-status --query "InstanceStatuses[?Events!=null]" |
jq -r '.[] |
[
.InstanceId,
.AvailabilityZone,
.Events[].Code,
.Events[].Description,
.Events[].NotBefore,
.Events[].NotAfter
] | @csv')
_ret_rds=$(aws --profile $_profile rds describe-pending-maintenance-actions | jq -r '.PendingMaintenanceActions[]|[.ResourceIdentifier, .PendingMaintenanceActionDetails[].Action, .PendingMaintenanceActionDetails[].Description]|@csv')
if [ -z "$_ret_ec2" -a -z "$_ret_rds" ]; then
return
fi
if echo "$_ret_ec2" | grep -v 'Completed' >/dev/null; then
_ret_ec2=$(echo "$_ret_ec2" | grep -v 'Completed')
_ret="$_ret_ec2"
fi
if echo "$_ret_rds" | grep -v 'Completed' >/dev/null; then
_ret_rds=$(echo "$_ret_rds" | grep -v 'Completed')
_ret="${_ret}${_ret_rds}"
fi
if [ -z "$_ret" ]; then
return
fi
echo "$_ret"
}
profiles=$(grep 'profile' $AWS_CONFIG | sed -e "s/\[profile \(.*\)\]/\1/g")
for a in ${profiles[@]}; do
echo ">>> profile:$a"
get_aws_events_by_profile $a
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment