/aws-rds-7day-autostop

Automatically stops RDS instances started by the AWS policy that requires RDS to be stopped for a max of 7 days.

Apache License 2.0Apache-2.0

aws-rds-7day-autostop

Goal

Monitor RDS Events servers auto started due to 7-day rule and stop them. AWS auto starts RDS after a maxiumu allowed time being stopped of 7 days. This can go unnoticed generating signifant AWS charges. Image of CloudWatch

User Story

As an AWS Solution Architect I want to monitor for RDS being auto-started by AWS due to it exceed the maximum allows time being stopped so that I can stop them automatically to achieve the 'Well Architected' pillar of 'Cost Optimization'.

Image of CloudWatch

Acceptance Criteria

  1. Detect and stop RDS intance auto started by AWS
  2. Create a cloudformation template.yaml
  3. Provide a way to test for future development using a server named rds-stop-test

How does it work?

There are 3 components

1. Lambda listenting to SNS

A lambda that monitors events from RDS on an SNS topic and ignores all events except 'RDS-EVENT-0154'. It then triggers a Step Function. The event processed is, "RDS-EVENT-0154 The DB instance is being started due to it exceeding the maximum allowed time being stopped", all other events are ignored.

2. Step Function to orchestrate the process

The step function will call another lambda to actually stop the instance. We used a step function here for 2 reasons:

a. Server State

When we get the 'DB Started' event, the instance is not in a valid state to issue a stop so we use the step function to keep checking until we can stop it.

b. Limitation on Lambda execution time

Overall this operation can take longer that the max time for a lambda and the step function can run interatively as many times as we want. (So we try for about 2 hours before we give up). You can see in this cloudwatch log how the calls fail until the last one when the server is 'available'

3. Lambda to stop the RDS

The second lambda actually stop the instance via the API.

Example of api failing when server not 'available'

This cloudwatch log demonstrates why we needed to use the step function to sleep and retry interatively until the server is available.

Image of CloudWatch

Testing

If you create an rds names 'rds-stop-test', then all events will be processed for this instance to allow you to test simply by starting the instance, it should automatically be stopped by the step function.