Skip to content

Instantly share code, notes, and snippets.

@pecigonzalo
Last active July 14, 2022 14:07
Show Gist options
  • Save pecigonzalo/13117015d0f89415655d33272979f77e to your computer and use it in GitHub Desktop.
Save pecigonzalo/13117015d0f89415655d33272979f77e to your computer and use it in GitHub Desktop.
Undo pending RDS changes
#!/usr/bin/env bash
set -euo pipefail
NAME="$1"
description=$(aws rds describe-db-instances --db-instance-identifier "$NAME")
instance_type=$(echo $description | jq -r '.DBInstances[0] | .DBInstanceClass')
allocated_storage=$(echo $description | jq -r '.DBInstances[0] | .AllocatedStorage')
storage_type=$(echo $description | jq -r '.DBInstances[0] | .StorageType')
pending_changes=$(echo $description | jq -r '.DBInstances[0].PendingModifiedValues')
pending_instance_type=$(echo $pending_changes | jq -r '.DBInstanceClass')
pending_allocated_storage=$(echo $pending_changes | jq -r '.AllocatedStorage')
pending_storage_type=$(echo $pending_changes | jq -r '.StorageType')
if [[ $storage_type == "io1" ]]; then
storage_iops=$(echo $description | jq -r '.DBInstances[0] | .Iops')
pending_storage_iops=$(echo $pending_changes | jq -r '.Iops')
fi
pending() {
echo "Instance: ${NAME} pending - type: ${pending_instance_type:-} storage: ${pending_allocated_storage:-} storage_type: ${pending_storage_type:-} storage_iops: ${pending_storage_iops:-}"
}
current() {
echo "Instance: ${NAME} current - type: ${instance_type:-} storage: ${allocated_storage:-} storage_type: ${storage_type:-} storage_iops: ${storage_iops:-}"
}
ask() {
read -p "Continue (y/n)?" choice
case "$choice" in
y | Y) ;;
n | N) echo "Exiting" && exit 0 ;;
*) echo "Invalid" && exit 1 ;;
esac
}
current
pending
# Get a comparable string (Super hacky)
if [[ $storage_type == "io1" ]]; then
pending_total="${pending_instance_type}-${pending_allocated_storage}-${pending_storage_type}-${pending_storage_iops:-}"
no_change_total="null-null-null-null"
else
pending_total="${pending_instance_type}-${pending_allocated_storage}-${pending_storage_type}"
no_change_total="null-null-null"
fi
if [[ $pending_total == $no_change_total ]]; then
echo "No pending changes for ${NAME}"
exit 0
fi
if [[ $storage_type == "io1" ]]; then
echo aws rds modify-db-instance \
--db-instance-identifier "$NAME" \
--db-instance-class "$instance_type" \
--storage-type "$storage_type" \
--allocated-storage "$allocated_storage" \
--iops "$storage_iops" \
--apply-immediately
ask
aws rds modify-db-instance \
--db-instance-identifier "$NAME" \
--db-instance-class "$instance_type" \
--storage-type "$storage_type" \
--allocated-storage "$allocated_storage" \
--iops "$storage_iops" \
--apply-immediately
else
echo aws rds modify-db-instance \
--db-instance-identifier "$NAME" \
--db-instance-class "$instance_type" \
--storage-type "$storage_type" \
--allocated-storage "$allocated_storage" \
--apply-immediately
ask
aws rds modify-db-instance \
--db-instance-identifier "$NAME" \
--db-instance-class "$instance_type" \
--storage-type "$storage_type" \
--allocated-storage "$allocated_storage" \
--apply-immediately
fi
current
pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment