AWS Lambda Tutorial | Use AWS CloudWatch Events to schedule Lambda function invocation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's going on in this tutorial we're going to look at how to set up a basic hello world lambda function that's triggered by a cloud watch scheduled event roll that way we can have our lambda function execute at any time interval of our choice so if that sounds good please stick around and let's do this [Music] alright guys so by the end of this video you'll know not only how to set up a basic AWS lambda function but also how to configure it to fire at any regular time interval of your choosing so our first step is going to be to create an execution role for our lambda function so that it has the basic permissions it needs to write logs to cloud watch then we'll go into lambda and set up a basic hello world function and just verify that it works next we'll go over to cloud watch where we'll set up a rule to invoke our lambda function every few minutes finally after some time we'll check our cloud watch logs and verify that our lambda function is being invoked at regular intervals by our cloud watch event and with that let's get right into it alright so head on over to aws.amazon.com and go ahead and login to your management console now the first step again is going to be to create an execution role for our lambda function so our first stop is going to be I am alright and once you're there go ahead and select roles from the left-hand menu and click on create role alright so once you're here just ensure that AWS service is selected and go ahead and select lambda since this is for our lambda function and go ahead and click on Next permissions all right now in the search box go ahead and start to type in AWS lambda basic execution roll if you just enter the first few characters of that it should show up and what this role does if we expand it right here we can see that it provides write permissions to cloud watch logs and that's really all we need this role to do so that looks good go ahead and click on next we can skip tags we don't need any tags for this exercise so go ahead and click on next review alright let's give this guy a name I'm going to call it HelloWorld lambda role but you can give it any name you want obviously so everything looks good here let's go ahead and click on create role all right and there is our new role so we are done here let's go ahead and navigate over to lambda all right so here's where we'll set up our lambda function so go ahead and click on create function and we're gonna create a function from a blueprint this time so go ahead and select blueprints all right and let's search for hello world and there should be a couple of different options we can choose from make sure that Dash is in here or all-suite you won't get the correct options all right so we're gonna choose the second one here just this very basic vanilla hello world starter function this is the most basic lambda function you could you could set up so that's good for the purposes of this tutorial so go ahead and click on configure alright and let's give it a name I'm just gonna say hello world lambda all right so for role I'll leave it as choose an existing role and for existing role we'll select the role that we created in the previous step hello world lambda role alright and scroll to the bottom and go ahead and click on create function alright great so our hello world lambda has been created and we just need to edit a few things here to get this working now I'm gonna blow up my screen size here let's see just so you can see it a little bit better there we go that should be good okay so if we scroll down a little bit here we can see the body of the function and let's go ahead and what do we want to do here alright let's go ahead and get rid of everything here and we're gonna do a consult log well I guess since this is a hello world function we should at least say hello world alright so we'll do that for the first line all right then let's do another console log and this time we'll output the event well we'll put some text here let's just say logging scheduled event and let's output the event as well which will just be an object okay that looks good now the next thing we want to do is set up a test event so go ahead and click on test at the top all right and we'll just say hello world event and we can assign arbitrary values to our keys 1 2 & 3 here so let's just say orange apple banana just so these values are recognizable when we look at our logs later all right so scroll down a bit and go ahead and click on create and so let's change event to event key 1 so that we see one of the values that we just set up in our test event so go ahead and click Save and then let's click on test all right and this will just ensure that our lambda is working at a basic level so if we expand this and here we go hello world logging scheduled event orange all right so everything looks good here with our basic little lambda function great so next step is going to be to go ahead and go over to cloud watch and setup an event so that this lambda is triggered or invoked at a regular interval all right now I'm gonna we're not gonna have any key values in our once this is being invoked by cloud watch so I'm just gonna go ahead and get that key go ahead and remove that key 1 and leave it as event and save our function okay all right so let's navigate over to cloud watch from the top menu and just type it in the search box if you don't already have it in your history over to the left alright and once you get here let's go ahead and look for events on the left hand menu and select rules right underneath events so events rules and we're gonna create a rule great now for our event source let's go ahead and select schedule because we want to have this rule invoked every so often every few minutes so let's see I'm gonna do a fixed rate of and the most granular unit of time that you can do here is by the minute you know one minute or you can select hours or days if you want to have a job run once a day or once every few hours you've got those options here as well so for the purpose of this tutorial I'm going to have this thing run every minute so I'm just going to change this to one all right now over to the right hand side I'm going to select add a target and it should be defaulted to lambda function so just leave that as is and we're gonna select our hello world lambda function from the drop-down alright now go ahead and click on configure details so let's give her a rule a name just gonna say hello world lamda rule I'm sure I could think of a better name if I really put some thought into it but this will do for now and I'm just gonna add a short description here there we go and let's go ahead and click on create rule all right now you can see here's our new rule here and the status is already green so this rule is already in effect as of now all right so what I'm gonna do we're gonna wait a few minutes and let let our lamb to be invoked a couple of times and then we're gonna go over to logs well we can navigate over there right now and then wait so if I choose logs from the left-hand menu I should see a list of log groups all right so I'm gonna look for the one corresponding to our lambda function hello world lambda and just click on that and here we can already see a couple of logs streams now if we check the time stamp well this one looks like it's from just now but the the previous entries are from probably before when I was testing my lambda over at AWS lambda via the test event so we can actually we can open this one and see if it if it's run our function yet yeah and there we go so it runs apparently it runs the first one right away at didn't actually know that so it appears like it doesn't wait a minute it just goes ahead and runs or invokes the lamda right away alright so I'm gonna go back one level up and what I'm gonna do now is wait 10 or 15 minutes I'm gonna pause the video and then we'll come back here and we should see a bunch of new entries for you know every minute that our lamda was invoked alright so I'll pause the video and I'll be right back alright guys so it's been about 15 minutes so let's check our logs again and see what the activity looks like alright so I'm going to again I'm under let me back up to the top level here so if you're coming here for the first time I'm gonna go under logs and then I'm gonna choose the hello world lamda that we've targeted alright now I'm going to drill into this latest log here alright now once we look at the detail now if we scroll down here we can see some log output for just about every minute 451 452 453 4 4 and so on and so forth so looks like everything is working fine and we see our hello world being logged every minute as well as our event object so everything is working as intended and our event role is calling our lambda function once every minute so that is it we are done here now I am gonna do a little bit of cleanup because I don't want this obviously to go on and on every minute calling my function so what I can do to stop this basically is go back to events and rules and I just have to select our hello world lamda role and delete it from the actions drop-down alright are you sure you want to delete yes I do alright so our lambda function is no longer being invoked and finally I will go back to lambda and just delete the function as well action is delete and just to be complete I can go back to I am and delete that execution role as well alright just good practice to clean up especially when we're working with tests code here hello world really I'm to roll all right and delete all right and that concludes this tutorial on triggering lambda functions via cloud watch events so hope you guys enjoyed it if you find this content useful please consider subscribing to my channel let me know in the comments how this works for you or if you ran into any issues and I'll do my best to help you out and I hope to see you in the next video alright guys take care bye [Music] [Music]
Info
Channel: Cloud Path
Views: 20,434
Rating: 4.9156628 out of 5
Keywords: aws lambda, aws lambda tutorial, cloudwatch, aws cloudwatch, aws cloudwatch events, cloudwatch events, cloud watch events, aws cloud watch events, schedule lambda cloudwatch, aws lambda cloudwatch, aws cloudwatch scheduled event, cloudwatch event trigger lambda, aws cloudwatch event, lambda and cloudwatch tutorial, trigger lambda cloudwatch, lambda execution role, cloudwatch logs, lambda cloudwatch event, lambda cloudwatch, hello world lambda function, aws serverless
Id: -v4LMV5DAD4
Channel Id: undefined
Length: 12min 36sec (756 seconds)
Published: Tue Feb 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.