Cloudwatch Rule

Cloudwatch rules can be added in Cloudwatch which invokes targets (lambda function, sns) based on events or at a scheduled time or rate. For example, a lambda function can be configured to run every 5 minutes or once a day or a specific schedule can be specified using cron expression. Also, an event pattern can be specified on an AWS service - for example, a lambda function can be run when a S3 bucket is created or when an object in S3 is deleted or for example when a snapshot for an ec2 instance is created.

Rules can be created in Cloudwatch by clicking on Rules from the Cloudwatch menu and then creating the rule. In this example, a lambda function will be called when any ec2 snapshot is taken but failed.

Note that "Event Pattern" is selected and then the service name - ec2 - is selected and then depending on the service type, different options will appear. For ec2, there are options to selects events like snapshot created or shared and then to select the outcome like snapshot creation failed or succeeded. When the rule is set and the lambda function is defined from the targets section, the lambda function will be called whenever an ec2 instance's snapshot creation fails.

Rules can be setup to run at a scheduled rate like every 5 minutes. This can be setup by choosing the schedule option and setting the schedule to specified minutes, hours and days.

When the rate is set, the lambda function will be called at the specified rate.

Rules can also be setup to run at a specific date / time by using cron expressions. Cron expressions have 6 required fields separated by white space. The fields include minutes, hour, day of month, day of week, month and year. Note both day of week and day of month cannot be specified and time needs to be specified in UTC. Here are few examples of the scheduled time.

0 6 * * ? *Run everyday at 6am UTC
30 6 * * ? *Run everyday at 6:30am UTC
0 6 ? * MON-FRI *Run every Monday to Friday at 6am UTC
0/15 * * * ? *Run every 15 minutes
0/15 18-20 * * MON-FRI *Run every 15 minutes between 6pm and 8pm on Monday to Friday