tech, volunteers, public safety, collective intelligence, articles, tools, code and ideas
In part 5, we amended our Lambda to write to an S3 bucket; and in this part, we’ll explore triggers to run it regularly.
Most Lambdas aren’t invoked the way we’re testing ours… Instead, triggers cause them to run when certain events happen (perhaps a new file in an S3 bucket with data to work on, a timer, or some other input).
The AWS EventBridge service can provide scheduled trigger events for a variety of services, including Lambdas.
We’re going to create a trigger that will run our Lambda once per day, for the city of our choice.
First we’ll create it, and schedule it to run every 5 minutes. This will allow us to test that it’s working. Then, we’ll go back and revise it to run on a daily basis.
Create rule
trigger-sample-lambda-daily
"London, UK"
Correction to the screenshot above: you must enclose
"London, UK"
in quotation marks in order for it to be accepted as a JSON string.
Create
to create your trigger.Having created the trigger, it’ll run every 5 minutes, so wait up to 5 minutes to view the logs.
/aws/lambda/SampleLambdaFunction
group.Next, we’ll check S3 for an ouptut file.
sample-lambda-storage
bucket.Actions
menu button to ensure it has the content you expect.Now that we’re satisfied that the scheduled event is working, it’s time to adjust it to run a little less frequently.
trigger-sample-lambda-daily
Edit
to modify it, and head down to the Define pattern section.You can read more about scheduling expressions to figure out the best for your use.
Cron expressions are pretty nuanced, and you can learn about their syntax in the Schedule Expressions for Rules documentation.
We’re going to use one of the example cron strings:
15 12 * * ? *
This can be interpreted as a set of positional parameters:
Minutes | Hours | Day of month | Month | Day of week | Year |
---|---|---|---|---|---|
15 | 12 | * | * | ? | * |
It will run at 12:15pm (UTC) every day.
15 12 * * ? *
As soon as you provide an expression, the schedule section will display the next 10 upcoming trigger times to help you check your cron expression. If it’s correct, you’ll see it scheduled to run at 12:15:00 GMT on each of the next 10 days.
Update
to confirm the change.The event has now been altered to run every day at 12:15pm GMT.
Congratulations! You’ve created a Lambda that checks the weather at your chosen city, using a secret API key, and then stores that information in an S3 bucket. You’ve scheduled an event that will trigger it every day at 12:15pm, and checked that it’s working.
There are plenty of other services your Lambda could interact with, and AWS provide a number of AWSSDK.*
nuget library packages to help you communicate with other AWS services, too.
For instance, you might consider establishing a database in RDS and then storing the temperature information for your chosen city there so that you can access it and reason about it.
Hopefully this tutorial has given you a little more information and experience than the usual “hello world” examples, and hopefully some insights into how you could build useful tools using AWS and .NET technologies.