Last active
February 20, 2019 13:22
-
-
Save xlbruce/fb795f8087d4daac019438e9df90c5a7 to your computer and use it in GitHub Desktop.
Start or stop an RDS Instance as a cron on AWS
This file contains hidden or 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
#!/bin/bash | |
maintenance_name=start-rds | |
rds_instance_id=rds-instance-name | |
dummy_ec2_id=i-00000000000000 | |
task=AWS-StartRdsInstance # or AWS-StopRdsInstance | |
service_role=arn:aws:iam::00000000:role/MyRoleName | |
profile=default | |
region=us-east-1 | |
cron_expression='cron(4 0 ? * MON-FRI *)' # From monday to friday at 07:00 PM (UTC) | |
window_id=$(aws ssm create-maintenance-window --name "${maintenance_name}" \ | |
--schedule "${cron_expression}" \ | |
--duration 4 \ | |
--cutoff 1 \ | |
--allow-unassociated-targets \ | |
--region $region \ | |
--profile $profile \ | |
| grep WindowId | cut -f4 -d'"') | |
echo "Maintenance Window ID: ${window_id}" | |
aws ssm register-task-with-maintenance-window --window-id $window_id \ | |
--targets Key=InstanceIds,Values=${dummy_ec2_id} \ | |
--task-arn $task \ | |
--service-role-arn $service_role \ | |
--task-type AUTOMATION \ | |
--task-invocation-parameters "{\"Automation\":{\"Parameters\":{\"InstanceId\":[\"${rds_instance_id}\"]}}}" \ | |
--max-concurrency 2 \ | |
--max-errors 50 \ | |
--profile $profile \ | |
--region $region | |
if ! [ $? -eq 0 ]; then | |
echo "Rolling back..." | |
aws ssm delete-maintenance-window --window-id $window_id | |
exit 1; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment