We use AWS EventBridge Rules when we have to schedule the invocation of the Lambda function or to set a cron job on Lambda.
AWS EventBridge rules are sets of conditions that you define to route events from an event source to one or more targets. When an event matches the conditions of a rule, EventBridge routes the event to the specified targets. You can use rules to filter and transform data, and to route events to different targets based on the data.
So before getting into the details be familiar with some technical terms such as:
- EventBus: An event bus is a pipeline that receives events. Rules associated with the event bus evaluate events as they arrive. Each rule checks whether an event matches the rule’s criteria. You associate a rule with a specific event bus, so the rule only applies to events received by that event bus.
- Events: An event indicates a change in an environment such as an AWS environment
- Targets: A target is a resource or endpoint that EventBridge sends an event to when the event matches the event pattern defined for a rule.
You can read more about it here.
To achieve this, we will use the AWS Python SDK (BOTO3) to access the Amazon EventBridge.
Let’s get started.
First, create an EventRule with the default EventBus
INSTALL THE BOTO3 USING PIP.
import boto3 client = boto3.client("events") # Create a EventRule response = client.put_rule( Name='string', # a unique name for rule ScheduleExpression='string', # cron(0 20 * * ? *)" or "rate(5 minutes) State='ENABLED'|'DISABLED', # Indicates whether the rule is enabled or disabled. Description='string', # description of the rule Tags=[ # list of key-value pairs to associate with the rule { 'Key': 'string', 'Value': 'string' }, ], EventBusName='string' # name or ARN of the event bus, If you omit this, the default event bus is used )
In the above code:
- Firstly, we have imported the AWS SDK.
- Created an object of EventBridge service.
- Created a new EventRule with the put_rule function
Now, attach a Lambda that will be triggered when an event matches the Rule
response = client.put_targets( Rule='string', # name of the rule EventBusName='string', # name or ARN of the event bus, If you omit this, the default event bus is used Targets=[ # list of targets to update or add to the rule. { 'Id': 'string', # Id of the target 'Arn': 'string', # target lambda function ARN 'RoleArn': 'string', # specify a RoleArn with proper permissions 'Input': 'string', # Valid JSON text passed to the target }, ], )
Normally, we would like to set a cron job that goes on until it is disabled, but what if we want to schedule and the event is invoked only once at a particular time like we want to schedule an Email or Notification
So in this type of use case, we delete our rule after the invocation of the Lambda function. It helps us in managing the number of rules associated with our AWS account.
To delete the rule, first, we remove all the targets which are attached to our EventRule as explained in the code block below
{ # code of the task which we what to run when the targeted Lambda invoked } response = client.remove_targets( Rule='string', # name of the rule EventBusName='string', # name or ARN of the event bus, If you omit this, the default event bus is used Ids=[ # list of IDs of the targets to remove from the rule. 'string', ], Force=True|False )
Note: If this is a managed rule, created by an Amazon Web Services service on your behalf, you must specify Force
as True
to remove targets. This parameter is ignored for rules that are not managed rules. You can check whether a rule is a managed rule by using DescribeRule
or ListRules
and checking the ManagedBy
field of the response.
You can read more about it here.
The above code will come inside the targeted Lambda, First, we do our task and then we remove all attached targets.
Finally, We delete our Event rule with the code below
response = client.delete_rule( Name='string', # name of the rule EventBusName='string', # name or ARN of the event bus, If you omit this, the default event bus is used Force=True|False )
And that’s it! Happy Coding!! 😎