Get started with AMAZON EVENTBRIDGE, build your SERVERLESS event-driven app with AWS SAM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello world and welcome to another episode of today we are going to talk about event bridge what it is and how to get started with it with some event bridge was launched two years ago in july 2019 i made a video as soon as it came out and then i made a couple more videos of new features it got but i think that video needs a refresher because it has developed and people started using it a lot for serverless applications it has become one of those services that big organizations are taking into and using for developing their serverless applications in combination with step functions now you can do quite a lot of decoupling of your microservices and your serverless applications i talk a lot about step functions i have many videos about functions but i have very little information about eventbridge so today we are solving that so the first question you might be wondering is like what is eventbridge well eventbridge is one of those aws managed services for messaging it connects many to many so many uh functions too many queues or whatever may need to make and we will go on and talk a little bit more what it connects and that many to many allows you to connect multiple services microservices external services together now you might be wondering uh why i want something many too many it's quite easy to use an api i have a consumer and a producer i just send messages like api calls and they work yeah that's good but when you have 101 it's okay what happens and you have multiple producers that are sending events to multiple consumers and it happens when those consumers want to send a call back and things are getting complicated like this when everybody is coupled to everybody and you change one and you need to go into the code of multiple and change it so that's why we need an event router a bent bus or whatever sits in the middle and routes the messages from the producers to the consumers and neither the producers or the consumers are aware of the other's existence so if we replace one then doesn't care the very important thing in this case is that both parties agree on what is the message types or what is inside the messages or the events so event reach is a managed service is totally serverless that means that it scales automatically it's highly available you pay as much as you use and you don't have to manage infrastructure and eventbridge is a fully managed service it's a serverless service and that means that scales automatically you know how to manage infrastructure is highly available and you pay as much as you use eventbridge does not only allows you to connect multiple aws services or microservices or applications you build within one aws account but it allows you to connect to other aws accounts other regions other providers um like sas partners so for example you can connect to zero data though salesforce they have native connections with eventbridge if you want to know the list of all the connections i leave you a link in the description box so you can check them out but there is many and also uh you can connect to third-party places or apis or whatever through the api destinations i will cover that in a future video so stay tuned for that it might come the week after this one is released so stay that tune for that so we talk a little bit on how event bridge works it sits in the middle between the consumers and the producers it allows the producers to send messages to the consumers everything is loosely coupled the magic of event bridge is that all the events that are passing uh in this bus they need to be packaged in a character in a way so basically we call that these messages has an envelope metadata and this envelope metadata is something that eventbridge understands and can use to wrote the messages so here we have an example of a message that is an event that is sent inside event bridge and you can see that the top part is the envelope metadata and that's something that either event rich will populate or you in your application can populate then we have the payload that is the data that you want to really send in your events so that something is totally up to you but there is also some parameters that you have control on so for example the source uh the detail type those are things that you can define you can say from which application if it's a custom application like we are going to build in the demo or if it's a nato via service it will say s3 or something like that and then the detail tab you can also put more information there so you can say one more level of description so if you have i don't know a paying system you can talk about the different applications within that system or something like that but you can define those things so now the next question that you might have is like how eventbridge knows how to send messages from those producers to those consumers with this package well bridge works with rules so within event bridge you will define the different rules and then when an event comes in then it will go and get it analyzed through a rule it can apply to one rule or multiple rules and then if it's uh matching that rule it will go to the different targets there can be multiple targets per individual rules up to five targets nowadays so you can send all the information um to the right place so that's pretty neat you uh how you can use that um rule system with eventbridge so you have multiple rules and they can say well some rules can be let's match the detail and the source others can be all the s3 events are going here others like whatever it's it's very customizable and you can have up to 300 rules per event bus so what event bus you just added a new new concept there well we have event bridge that is an event bus service and inside event bridge you can have multiple event buses by default there is the default event bus and that's the one that aws services will send their messages so most of aws services will send messages without you doing much or just by configuring something directly to event bridge so it will happen inside this bus then we have the partner uh event buses and for example if you're using aut00 or datadog or other such providers will be using their own independent [Music] bus and these buses are kind of independent on each other so we will talk a little bit about how many buses you should have in a moment and then finally we have the custom event buses and these are the buses that you define you can either define them from your applications for your needs or as i will say you can use the default event bus if you don't have a very complex application and you don't need your own particular bus so you can send your application uh events through the default bus so how many buses we need that's a great question you can have as many as you need i don't i imagine there is a limit but well but i think the most important thing here is that you avoid routing inside the event buses so if you have applications that need to talk to each other don't put them in different buses that will make a mess so keep everything in the same bus if you have applications that are totally independent then you can have them in different custom buses and that will be totally fine so these are good practices on working with the buses you can also work with uh buses that are in different accounts so you can have a custom bus that is handling the traffic that is coming from or going to another account or different regions so that's something you can also do with the band pass and finally the question might also come in how we define the rules who owns the rules i think it's a good kind of strategy if you are using one custom bus or the default bus that the rules be belong to the consumer so it's kind of easier to handle so you will see it in the example that we are going to do now we will have um one producer that will be sending the events and then the different consumer uh services will have the definition for the rules within that service so then it's kind of easier to find and it's more decoupled as well if not then you will have the definition of the consumer inside the uh producer code so you start adding couple and you get what i mean so now that we went through the basics of event reach what it is what it does and all that i want to show you how you can build this with awsome in the first video i did there in 2009 i did exactly the same with serverless frameworks so you can go and check it out and if you're wondering what is the difference we didn't even breach squeeze sns kinesis and all those services i already made a video about that some time ago so go and check it out so now we are ready to get started and you have been watching this content for a while and have not liked that button yet hit that like button i'm watching you i'm watching you i'm watching you and if you're not subscribed subscribe yeah let's continue so what we are going to build in this demo imagine you're a delivery company like world or if you're in nordics or eats or i don't know rugby if you're in latin america whatever and people will order food from your app and then you need to connect to all these different restaurants okay you need to take the order and then send that order to restaurants so they can prepare the food and then they can deliver it well you will deliver it so this needs to be some kind of callback system back and forth and messaging and this can get very complicated if you are using apis because things are asynchronous things are not like using the message and it will come the response right away you know so here we are going to use an event bus we are going to build an application using assam and for this i'm going to use three some projects simulating uh the three parts of our application we have the producer that will be the application that is taken in the orders from the customers and sending these messages to the different restaurants and then we will have two restaurants so two consumers in this case that are um kind of listening to the rules they have rules attached into uh event event breach and their uh trigger lambda functions when those rules get triggered what we are going to implement today it can be improved because if you have multiple restaurants it becomes a mess i will show you how you can improve it in the following video when we talk about api destinations but i think this is a great example that can work for many use cases that you might have in your organization so we'll have three projects three some projects one for the order manager one for the restaurant that i call pizza hut very original and another one for the restaurant called thailand so let's go to the code so before starting out with the code you need to have a database account you need to have some cli installed and i think that's it configuring your computer you can get the code from the link i leave you in the description box so as always i will be starting with an empty project creating a folder called some event bridge and there i will put my free projects and the first part of this video is be basically setting up everything so i will create three directories and inside each of the directories i will create a template yaml that is empty a handle yama a handheld js that is empty and i will initialize the node projects for each of them so i will fast forward until we have that setup because me creating files and running commands in the screen that are not very hard it's boring so now we are back and this is the result we have three folders for directories one for the other manager one for pizza hut and one for thailand two restaurants one producer two consumers and the three directories are the same we have each of them has a handler js that is empty a temple's general that is empty and a packet json that has the vermont for initializing each of the node projects so we are going to start working first we are going to start with the restaurants we are creating the bourbon infrastructure and that means i'm going to create a function for each of the restaurants with no trigger or anything so i will create a function in both restaurants and i will create a handler inside the restaurant that basically prints in console an order has been received and in here if you were doing this in real life you can either call the api of the restaurant or do something else invoke something i don't know so this is what we are doing here um so i will fast forward until we arrive to the order manager that is a little bit more interesting so now we jump into the order manager and there i will also define one function that is the put order function so that's the function i'm defining and there you will see that it has a trigger it's an api trigger by a post http event with the path order so that's good we can trigger this and then when we trigger this this will put an uh event in event bridge and this event bridge will get the rules analyzed and it will send it to the right restaurant so that's good uh we will go and also add a little bit code in our handler so we have something to start with and i will basically put a print off the body because we want the body and then we can deploy this thing i will deploy this right away so you see that everything is working if you're following this you don't need to deploy it right away you can do whatever you can deploy it later on once but it's easier for me to deploy it now and then it's ready to start working so uh the order details i want them to include the restaurant name the order uh basically what we are ordering pizza pad thai the customer name and then the amount of items so two pad ties or something like that this doesn't make much sense this is what it's going to come in the order retail so i will prepare my http request with these bodies so i know that everything is working so i will do some deploy dash that's guided go for the configuration deploy it and then i will be testing it out so when we are ready we just go to our favorite http client for testing i'm using this thunder client that is inside visual studio code and i put in the url i'm putting the message post i'm putting the body that is the restaurant name the order the custom name and the amount that i mentioned two seconds ago and i'm sending the request and that's basically not doing anything because it's not connected to anything but it's working good now like two minutes of demo for setting i need to show you those things sorry about that so now we can move on to uh sending an event through that order manager so the first part we want to do is to put an event inside event reach so we will first give permissions for these lambda functions to put an event in the custom bus of eventbridge and again i repeat this all the time all the different aws services are born without permissions we always need to give them permissions to do things and in this case is to put an event so every time you're using the aws sdk you need to give permissions for that particular operations you're doing now that we are out of the way with the permissions we are ready to start using that api and we can create a new method that will be the one putting the events in a event bridge and you will see as always it's very very simple to use the aws sdk it's very straightforward i'm going to create a new method where i'm passing this order details the restaurant name the order the customer name and the amount into event bridge so this put event in event bridge what it will have it will have we are going to create this detail that is the payload of the event that will have all these order details and then i will be sending an event and i can modify a little bit the envelope of the event you can see there the details that's the payload of the vent and then we have the detail type that's part of the envelope and i'm saying that this is the order this is an order and the source is a custom dot order manager so this is a custom application you don't need to boot custom you can put whatever you want i like to put custom so i know some are aws some are mine and then we send that to event bridge and that's kind of it now we can uh basically deploy this again and test it but if we deploy it now nobody is kind of listening to this event so nothing will happen so the next thing we need to do is to have somebody listening to this event and for that we are going to create a rule and we will create rules in two different ways because this is a sum video i want to show you the cloud formation way and i want to show you the native some way of doing things so the first way i want to show you is the more cloud formation way where you have to define more things so we will use the example in the pizza hut we are going to template jamal and we are going to add the rule for them and bridge so here we are going to define the rule for this consumer inside the consumer project so it's tied together and it's not tied to the producer so here i will create an event rich rule and you can see there that there is a lot of information there is the description the event pattern so this is what the rule needs to match them we have the source uh and here the source needs to match exactly the same as the custom order manager that is the source that we define in the handler.js in the producer so things need to match and then we have the detail type also we are checking against that so we need to have exactly the same and the restaurant name that will be inside the detail there will be an attribute called restaurant name so it needs to match exactly this event pattern is a quite complex one there is free but you can have it more complex there is a lot of things you can do with those rules but there is three matches that we need to do we can have even one it's like if the source is this do that like you don't need to make it too complex but this is a cool thing you can go inside the payload you can see the different parts of the envelope you can do quite a lot of things and even i i didn't show it here but it's in the demo code you will see that there is a lambda function when nothing matches the rule you can also capture that and that's pretty cool so go and check the github code that has that that demo and then we have the target definition and that is what is going to get triggered with the target in this case this lambda function the pizza hat function but again all things in aws are born without permissions so we need to give permissions to event bridge to invoke the love of the function so we are going to do that and we are going to give the permissions to invoke alarm the function we have to say who and what permissions so yeah so that's what we are doing here and now we can deploy and now we can test the producer sending an event to event bridge and then even bridge matching the rule and sending it to the right target so i will fast forward until we are deployed because we need to deploy the two projects the producer and the consumer that is this pizza hut so now we are back in our http client uh i have exactly the same request because we have created the body that has the restaurant name pizza hut so everything is good so now i can send this request and i can also open in the side the logs for the pizza hut lambda to see if it's getting trigger i will open that in the side using the serverless console that is my favorite plugin for serverless in visual studio code so i will just configure everything and we can go and see it in action so this is just finding that function and showing me the logs there is nothing in the log group so that's good because we have not executed that yet so now we have side by side the request we send it and then as soon as i get the response we can refresh the logs and we see that an order has arrived and we can see that everything matches whatever we send in the in the body that sounds really good now we will do exactly the same but using the sum notation for a band bridge this makes things simpler so this is my preferred way i will do it in the thailand example so you have it there and you can see that instead of adding the rule and the invocation permission we add just an event source to the lambda function and then sam knows how to create all those other things so we add an event trigger to this lambda function with the type of cloud watch event and then again we put the pattern on which this rule needs to match the pattern is a very similar pattern that the one we did in the pizza hut we need to match the source that is the custom order manager we send it in the handler the detail type that is the order and then the restaurant name that in this case is thailand so this is a way simpler kind of way of defining this if you have along the function you can attach this um some uh way of handling this so i really like it and basically now we can deploy this service and test it again so let's fast forward until everything is deployed the thailand needs to get deployed and we can go and test it again we will add another service using the serverless console and we will add the thailand restaurant and all those things and we can see the logs also we need to modify a little bit our existing request because it was uh sending it to the restaurant uh pizza hut before so now i modify it to send it to thailand and we are also ordering part-time if not they will say what the hell so now we can see the order went okay and pad thai got the event so if you want to see what is going on in event bridge now we can go to the aws console and see it so if you go to your management console and you look for event bridge you will find it and there you will see all the different pieces that i mentioned you can see your event buses i have one that is the default one and the custom event bus so we have uh that one the default you will have the custom event bus i created it for testing uh so that's good then we have the rules and the rules there are independent for each bus so you need to uh pick the bus and then you can see the rules and you can see that there's like 15 in here i was just a little confused because there was no moving forward at the end of the page fascination was on top so i was like oh i cannot find all my rules i don't come that often to the use the ui so i find the rules here that we define so you can filter and find the rules so there is the pizza hat and the thailand so you can open the rules and you can see the configuration in each of the rules how they are defined and all that you can see the event pattern that we were defining with the rules you can see the targets which lambda in this case get triggered but they can be any anything can be a target so and you're gonna need them on more targets and things like that i will not recommend you to go manually and edit this this is infrastructures code you want to keep it in the code so i show you how to do this with some i show you how to do this with confirmation do you want to see it with cdk let me know in the comments so that's everything for today today you learn about the bridge you learn about its integration with some and in the following video i want to talk about event bridge with api destinations that i think is a powerful match for all those things that are not within aws or in our shared sas providers and you don't want to just have a lambda function running things just to call an api so it will solve a lot of problems so tune in for that and i see you in the next episode [Music] you
Info
Channel: FooBar Serverless
Views: 2,288
Rating: undefined out of 5
Keywords: foobar, serverless architecture, serverless aws, serverless computing, serverless tutorial, serverless web application, amazon eventbridge, event bridge aws, eventbridge, aws event bridge, event bridge example, serverless event bridge, event driven architecture tutorial
Id: c493KFaWteg
Channel Id: undefined
Length: 27min 27sec (1647 seconds)
Published: Thu Aug 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.