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.
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'.
- Detect and stop RDS intance auto started by AWS
- Create a cloudformation template.yaml
- Provide a way to test for future development using a server named rds-stop-test
There are 3 components
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.
The step function will call another lambda to actually stop the instance. We used a step function here for 2 reasons:
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.
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'
The second lambda actually stop the instance via the API.
This cloudwatch log demonstrates why we needed to use the step function to sleep and retry interatively until the server is available.
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.