I'm trying to use a lambda function with cloudwatch event to power on/off an instance for 1 week a month.
I realised cron can't do week of month, so I started looking at using the lambda script to detect the week and turn on/off depending on that.
Couldn't get any of the python week of month code I came across to work.
So I figured try 3rd monday and 3rd sunday for two different functions to power on and off respectively.
Realised the error of my ways, as if the month started on a tues-sunday, the power off condition would occur before the power on condition.
So I figured I'd open the question up to others, any strategies to execute this? I've seen AWS scheduler, but I wanted to get more experience with lambda and cloudwatch, so I'm trying to do this myself.
CodePudding user response:
Your Question is very unclear, but i will try to answer it as best as i can.
Use the date module to detect what date it is. Youtube search: "how to use date module in python"
add what you want after the "if"
CodePudding user response:
The easiest method would be:
- Configure CloudWatch Events to trigger an AWS Lambda function once per day
- The Lambda function should:
- Check whether it is at the start of the 3rd week, if so then Start the instance
- Check whether it is at the start of the 4th week, if so then Stop the instance
This logic will take less than 1 second to execute, so don't think of it as wasteful to have it running every single day. Also, you will have complete control over the definition of "3rd week of the month".
