To save costs I would like to construct / deploy the whole stack (with Aurora and ECS) every working day before business hours and destruct / destroy after these hours.
I found a solution with an EC2 instance, running cron jobs, which executes native cdk commands to deploy and destroy the stack.
Is there a way (on AWS) to trigger these in a serverless kind (without running EC2 instance or something similar that needs to be managed by ourselves) ?
2
Answers
There may be multiple solutions. My approach would be to use a lambda to deploy and destroy assets. The lambda can be invoked using Eventbridge triggers, the eventbridge would be scheduled for start of weekday and end of weekday with inputs to indicate deploy or destroy. I haven’t tried this out, but you would need to package the cdk.json and other cdk code into the lambda. Refer https://github.com/aws/aws-cdk/issues/2637 for a Typescript version
I created a python version that you can use as a starting point
You can run a CodePipeline job with a scheduled event trigger, as described here.
For that, you would need to set up a pipeline first, which you can do as described here, or you can utilize the tools provided with CDK, namely CDK Pipelines.