‫So let's go ahead
‫and practice integrating Lambda with EventBridge.
‫So I'm going to create a function
‫called lambda-demo-eventbridge.
‫Scroll down.
‫And for the runtime I will choose Python 3.9.
‫Then I will create this function
‫and will make sure this function is being invoked
‫by EventBridge.
‫So to do so, let's also explore the EventBridge console
‫while this function is being created.
‫So I'll go under Rules, and I need to create a new rule.
‫Now I'll call this one InvokeLambdaEveryMinute.
‫And we need to select an Event bus,
‫we'll have the default Event bus.
‫And here we have two options.
‫The first one is to use a rule with an event pattern.
‫And this is when you want to
‫match events happening within AWS.
‫For example, code get committed to code commit
‫or an EC2 instance get terminated,
‫these kind of things.
‫And we won't go through this, we'll keep it simple.
‫We'll use Schedule.
‫And as you can see if you use Schedule,
‫it prompts you to go to EventBridge Scheduler,
‫which you can play with in your own time
‫and it's a fun exercise to do,
‫but right now I'm going to click on the bottom left
‫to continue to create rule
‫because this will create a rule
‫as an EventBridge rule and not as an EventBridge Scheduler.
‫But capability-wise, we are doing the exact same thing.
‫But let's click on Continue to create rule
‫because I wanna show you something specific
‫around resource policies.
‫So here we define the schedule pattern.
‫Do we want a Cron expression
‫or do we want to have a schedule
‫that is being run regularly?
‫And I want my schedule to be run every one minute.
‫I click on Next.
‫And next we have to choose a target of our rule.
‫So the target of our rule has to be Lambda, of course.
‫So I'll choose a Lambda function.
‫And here I will choose my lambda-demo-eventbridge.
‫We can configure a specific version in areas
‫if you wanted to, as well as additional settings
‫around dead-letter queues and max attempts and so on.
‫So this is going to invoke my Lambda function.
‫And as you can see here,
‫EventBridge will automatically configure
‫the proper permissions for the selected targets.
‫And we'll see this in a moment as well.
‫So let's click on Next.
‫Next.
‫And we are good to go.
‫So let's create this rule.
‫So now my Invoke Lambda Every Minute rule is created.
‫And if I go back to Lambda, as you can see right now,
‫there is no triggers on the left-hand side.
‫But if I decide to refresh this page,
‫now we have EventBridge as a source to my Lambda function.
‫If I click on it, as you can see,
‫we have the trigger right here,
‫which corresponds to the rule I have created
‫which is of the rate one minute.
‫So how does this EventBridge rule invoke Lambda function?
‫Well, if you go under permissions now,
‫and we look at the resource-based policy statements,
‫there is a statement that get added,
‫and I click on view policy.
‫And if you have a look at this statement,
‫it says allow the principle being the EventBridge service
‫to invoke our Lambda function.
‫And the resource ARN has to be our Lambda-demo-eventbridge
‫which is very same function.
‫And then the condition is for the ARN to be like,
‫and this is for the source ARN,
‫to be like the ARN of a rule on EventBridge,
‫meaning that only our EventBridge rule
‫has the right to invoke our Lambda function,
‫which is what we want.
‫So that's perfect.
‫So because we have a rate of one minute
‫and I've been most likely talking for one minute,
‫we can go under Monitor and look at the logs
‫and the invocations.
‫Now there's a chance that we don't see anything right here
‫because it's too early,
‫but if I click on View CloudWatch logs
‫I am taken directly into the CloudWatch console,
‫and from there I should be seeing some invocations.
‫So yes, we are in the log group Lambda-demo-eventbridge.
‫I scroll down, I have a log stream,
‫and right now I see my Lambda function is being invoked.
‫Now I'm going to make it a little bit more interesting.
‫Under the code in here, I'm going to print the events,
‫which is going to print what is going to be
‫sent to our Lambda function.
‫So I deploy this and it's going to update my function.
‫And now I just need to wait another minute
‫to see some events in my log.
‫So now it's been over a minute.
‫And if I go into here and refresh my log,
‫and actually we need to have a new log stream
‫because we really put the function.
‫So I go one level up and choose the latest log stream.
‫Now I see that in my log.
‫The event gets printed to the console.
‫So we see version zero.
‫This is the idea of the event.
‫The detail type is that it's a schedule event.
‫The source is aws.events.
‫Here is my accounts.
‫Here's the time.
‫Here's the region that this got invoked in.
‫And here's the resources that invoked our Lambda function.
‫And for detail, we have nothing
‫because we haven't provided any specific JSON.
‫But as you can see, we now have some information
‫around this invocation and everything is working.
‫So to just stop this hands-on,
‫just make sure to disable your rule
‫and you'll be good to go.
‫Okay, so let's it
‫we've seen how to integrate Lambda with EventBridge.
‫I hope you liked it and I will see you in the next lecture.