How to specify the first run time AWS CloudWatch Rate Expression? - amazon-web-services

I have a Lambda Function that needs to be invoked biweekly on Sunday at 10am. I used the rate expression to set 14 days as the rate period. However from the official documentation, I couldn't find a way to specify the Sunday at 10AM part. How can I make that happen?

If you're looking to do this you need to use a cron expression instead for the CloudWatch event.
The cron schedule 0 10 ? * 1 * will cause the CloudWatch event to trigger every Sunday at 10 AM UTC. Be aware that regardless of your region that the cron will always evaluate in UTC so you may need to adjust the times to match your local region.
More information is available in the cron expressions documentation.

Related

How do I schedule a Lambda function to execute everyday only during specific dates?

Is there a way to schedule my Lambda to run everyday ONLY during specific dates ?
Example:
everyday between 1-July to 30-July
everyday between 1-Oct to 30-Dec
Also, could I use these dates to set the schedule expression with eventbridge - https://docs.aws.amazon.com/eventbridge/latest/userguide/example_eventbridge_PutRule_section.html?
You can use the below cron expression to trigger the lambda function between 1-July and to 30-July
0 0 1-30 7 ? *
Explanation of the cron expression:
The first field is for minutes (0).
The second field is for hours (0).
The third field is for days of the month (1-30).
The fourth field is for months (7, July).
The fifth field is for days of the week (? means every day).
The sixth field is for years (* means every year).
You can refer attached image:
You can set up a rule to run an AWS Lambda function on a schedule. Here is a tutorial that shows how to do it.
At a high level, here are the steps.
Create an AWS Lambda Function
Create a Rule via CloudWatch
Verify the Rule

Need to create a CloudWatch rule that runs only once at a specific date and time

I need to figure out the CRON expression to trigger a Lambda that runs 1 hour before a specified date and time.
Example:
I get a specific date and time from a SNS notification, say 20th May 2022, 4 pm UTC
Now, I need to create a CRON expression for 20th May 2022, 3 pm UTC (i.e., 1 hour before the specific date and time), so that the Lambda is triggered an hour before the time.
I have gone through AWS docs and understand that I need to create a CRON expression to do this. I need help in figuring out the expression that would perform this and run only once.
There are two variants to define schedule expressions. One is rate() that juts tells it how often to run, the other is cron(), which is a cron-like syntax. The full syntax for the cron call looks like this:
cron(Minutes Hours Day-of-month Month Day-of-week Year)
So in your example you can use:
cron(0 15 20 5 ? 2022)
Note that all fields accept * as the wildcard, except the Day-of-week one, which uses ?.
To actually generate this from an SNS event, you can create another Lambda function that listens to SNS events, reads the desired timestamp, and creates a new EventBridge rule with the timestamp converted to the cron format as the ScheduleExpression. Depending on the language that you use in the Lambda, use the appropriate AWS SDK to create the EventBridge rule and target. For example, if you use Python, you can use the boto3 put_rule method and then call put_target to attach the new rule to the other Lambda function that runs the scheduled code.
Also, I believe that scheduled events are only available on the default EventBridge, not on custom ones. I know this used to be a limitation before, they might have changed it in the meantime.

AWS Auto Scaling Group: is it possible to schedule an increase of capacity every first sunday of each months?

I'm using Auto Scaling groups which works pretty fine with some custom rules I've set, but I also noticed that I need to set the minimum values of servers to a certain, higher, amount every first Sunday of each month (we have an increase in requests at that period).
I saw the Scheduled actions in the "Automatic scaling" tab, but it does not appear to be possible to set longer than per week, even less setting something like "every first Sunday of each months".
Is this something possible in another way ? Maybe via some Cloudwatch settings with custom "cron" tasks? I'm not sure here.
You don't need to use an external service or a Lambda to do it. You can do it in the advance tab in AutoScaling option in AWS.
For your particular case, your crontab expression would be 0 14 ? * 1#1 * See the image below, it will be triggered every first Sunday of the month.
Edit: It only works in Event bridge cron expression.
Cron expression
You can create a different timer (e.g. for lambda called every morning/hour/whatever) and scale-up the scaling group from there if its the first sunday of the month.

AWS Cloudwatch Rule Schedule Cron Expression to Skip 2 Hours in a Day

I want to trigger an AWS Lambda function, using Cloudwatch Rules and the requirement is as follows.
Condition 1: Trigger Daily
Condition 2: Every 5 mins
Condition 3: It should NOT trigger between 11PM and
1AM every day (Maintenance Window).
I read the documentation on crons and am unable to come up with a Cron expression to fulfill condition 3. I came up with the following expression.
0/5 ## * * ? *
How can I fulfill condition 3 mentioned above? I have left it as ## on the cron expression. I am well aware of the timezones.
You can utilize cron online tools such as
https://crontab.guru/
http://www.cronmaker.com/
Here is the expression that I created as per your requirement
*/5 1-23 * * *
I'm answering my own question. Regular cron expressions are not applicable for AWS Cloudwatch Rules. AWS Cloudwatch Rules have a cron expression of SIX (6) Required Fields. (Link to Documentation). The answer to this question is as follows. I will present different scenarios, because in some cases, the timezone is important and we might have to skip different hours instead of Midnight.
Both of the below scenarios are working and I tested in the AWS Cloudwatch Rules.
Scenario 1: Trigger Daily - Every 5 Mins - Skip 11PM to 1AM.
0/5 1-22 * * ? *
Explanation: First field mentions, it should be triggered only in 0th and 5th Minutes. Second field mentions that it should be triggered from 1st Hour to 22nd Hour. Hence, after 22:55 the next trigger will be 1:00. It will skip from 23:00 to 00:55.
Scenario 2: Trigger Daily - Every 5 Mins - Skip 5PM to 7PM
0/5 0-16,19-23 * * ? *
Explanation: First field mentions, it should be triggered only in 0th and 5th Minutes. Second field mentions that it should be triggered from 0th Hour to 16th Hour and from the 19th Hour to 23rd Hour. Hence, after 16:55 the next trigger will be 19:00. It will skip from 17:00 to 18:55.
This command can help you :
*/5 1-23* * * * /usr/bin/php /home/username/public_html/cron.ph >/dev/null 2>&1
Reference : crontab run every 15 minutes between certain hours
The answer is already given but few thing that I can add, and the https://crontab.guru/ create confusion as AWS cron expression is consist of 6 figures while crontab consist of 5, which just create confusion as mentioned by #keet
So first you can ignore the year section and check your cron express in crontab but again crontab does not understand ? mark when it comes to days of week.
So my suggestion is to use AWS console for expression testing which will list next 10 triggers.
So hope it helps someone else to know about the next trigger.
For more details, you check AWS CronExpressions

How to write a cron schedule for US Market Hours

I am looking to schedule a Lambda function on AWS using CloudWatch to trigger every minute during US market hours (9:30am - 4pm EDT).
I believe the only way to do this is using cron, and I'm not extremely familiar with cron and its different flavours (AWS seems to allow some non-standard features like MON-FRI).
So far, I have worked out that:
cron(0/1 14-21 ? * MON-FRI *)
seems to work for 9am-4pm (given I am using a London timezone region, this includes the 5hr time difference).
How do I get this to start at 9:30am EDT? And is there a way for cron to automatically keep up to date with daylight savings time changes?
How do I get this to start at 8:30am EDT?
It isn't possible using standard cron syntax (including in CloudWatch Events) to express 8:30-4pm.
To work around this limitation, you could have two expressions
30-59 14 ? * MON-FRI *
* 15-21 ? * MON-FRI *
or you could account for the logic in your code.
And is there a way for cron to automatically keep up to date with daylight savings time changes?
Cron will use the local system time when determining when to run jobs. E.g. if you use cron on Linux, it will use whatever /etc/timezone (debian) says. The time zone files (as provided by IANA) have the time change info encoded. With CloudWatch Events, all times are UTC. You'd need to correct for time zone/time change in your code or use cron on an OS with the EST/EDT offset.
You can use CloudWatch Events to trigger the lambda function when desired.
It also supports scheduled expressions that supports cron syntax and is in UTC timezone.