In today's blog post, we're focusing on how to use EventBridge Rules to schedule the execution of our Lambda function. EventBridge is a serverless event bus service that makes connecting applications easy. In this post, we'll specifically look at how to set up an Event Rule that triggers our Lambda function, which runs a Python script.
What is EventBridge?
EventBridge is an AWS service that acts like a middleman to route messages between different parts of your application. It's an event bus that catches events from your software components and routes them to where they need to go. In our example today, we'll focus on using the "Rule" component of EventBridge. This Rule will specify when our Lambda function should execute our Python script.
A rule matches incoming events and sends them to targets for processing. A single rule can send an event to multiple targets, which then run in parallel. Rules are based either on an event pattern or a schedule. An event pattern defines the event structure and the fields that a rule matches. Rules that are based on a schedule perform an action at regular intervals. We will be focusing on the latter.
How do Lambda and Event Rule Fit Together?
So, we've got a Lambda function ready to run our Python script, but how do we make sure it runs on a set schedule? That's where the Event Rule comes in handy. We create an Event Rule with a specific schedule, like "every Monday at 7 AM," and then set our Lambda function as the target for this rule.
When the clock hits the time we've set in the rule, the Event Rule triggers the Lambda function.
Create your Lambda Function
To keep things simple, let's create a straightforward Python script that just prints a message. What's cool is that this message is something we'll send to the Lambda function when it's triggered by the Event Rule. This way, you can see that not only can the rule trigger the function, but it can also send some data for the function to process.
IAM Roles and Policies
To get started, head over to the AWS Lambda service. Remember, you'll also need the right IAM roles and policies set up for this to work. This ensures that our Lambda function has the permissions it needs to execute and interact with other AWS services.
At a minimum, you'll need the following permissions for your Lambda Execution Role.
lambda:InvokeFunction- Allows the function to be invoked.
logs:CreateLogGroup- Allows the function to create a new log group in CloudWatch.
logs:CreateLogStream- Let the function create a log stream within a log group.
logs:PutLogEvents- Allows the function to write logs to a log stream.
By default, Lambda will automatically create an execution role with permissions to upload logs to Amazon CloudWatch Logs.
Understanding the Python Script
In our Python script, we define a function called
lambda_handler. This function is what AWS Lambda will call when the function is triggered. It takes two arguments,
- lambda_handler - This is the entry point for our Lambda function. Whenever our function is triggered, this is the method AWS will call.
- event - This parameter is a dictionary that contains the data passed to the handler. In our example, the Event Rule is configured to send a message, which is also a dictionary. More on this later.
- context - This parameter contains runtime information about the function and the invocation request but we're not using it here.
When the function is triggered by the Event Rule, the
event parameter will have the message we defined in the Event Rule. In this example, it will contain a dictionary with the key
'message' and the value
'hello world!'. The line
print(event['message']) will print this message,
hello world!, to the logs. This demonstrates that you can pass information into your Lambda function through the Event Rule.
So, let's talk about setting up the Event Rule in EventBridge. We use a cron expression to specify when the rule triggers our Lambda function. In this example, we want the function to run every day at 8 AM. The cron expression for this is
cron(0 8 * * ? *).
Next, we set the Lambda function we just created as the target for this rule. AWS makes our lives easier by automatically creating the necessary permissions.
Last but not least, we'll also include a JSON payload that has a message key with the value 'Hello World!'. This payload is what gets passed to our Lambda function as the
event parameter, allowing our function to act based on this message.
Testing and Verification
I know, waiting until 8 AM to see if everything works as expected can be a drag. So for testing, I changed the schedule to trigger 5 minutes from the current time. You can do this too if you're as impatient as I am!
After the function runs, go to the 'Monitor' tab within the Lambda function on the AWS console. Here, you'll find log messages. If everything is set up correctly, you should see our 'Hello World!' message printed out. This shows that the Event Rule not only triggered the Lambda function but also successfully passed the JSON payload.
I hope this walk-through helps you understand how to trigger a Lambda function using an EventBridge Rule, all while passing in a simple payload. It's a straightforward but powerful way to automate tasks in AWS. If you have any questions or run into issues, feel free to drop a comment.
If you want to learn how to create all of the above via Terraform, please check out my other blog post below.