‫Okay. So in this hands-on, we're going to have a play
‫and create a Lambda function with dependencies.
‫To do so let's go into Lambda with dependencies,
‫and there are two files interesting to you;
‫index.js and steps.js.
‫Now for you to be able to reproduce all the steps,
‫we're going to be able to use CloudShell,
‫and CloudShell is going to give us a terminal
‫that is connected directly into our accounts
‫that has NPM installed.
‫So NPM is a way to manage packages on node JS.
‫And so this is why we want to use CloudShell,
‫because the setup is already done on CloudShell for you.
‫So the other thing is that you need to create
‫a Lambda folder.
‫So I'll remove one because I just created one,
‫but create a Lambda folder.
‫So you do make directory Lambda
‫and then you go into the Lambda folder.
‫So now you're in the Lambda folder,
‫and there is nothing in it.
‫And the first thing is that you need to add
‫this index.js file.
‫So I will just do sudo yum install -y nano
‫to have a text editor very quickly.
‫So nano is installed, and I do now nano index.js,
‫which is going to allow me to copy all this code into here.
‫And I will remove this prompt,
‫and I will just copy all this code,
‫and paste it into nano control + X, Y, enter.
‫And now if we clear this and do cat index.js,
‫we can see that the file has been uploaded correctly
‫into our CloudShell environment.
‫So next, let's go into the steps
‫to install some dependencies,
‫and make sure the function will be packaged correctly
‫to be uploaded into the Lambda service.
‫So first, if we look at this index.js
‫we can see that it requires the xray-sdk-core,
‫and this is something that we need to bundle
‫with our Lambda function.
‫Then we are opening the AWS SDK
‫to talk to Amazon S3,
‫and we return a ListBuckets operation into Amazon S3.
‫So what we need to do is to make sure
‫that our Lambda function does have access
‫to the xray-sdk for AWS.
‫So this is what this command does right here:
‫npm install aws-xray-sdk,
‫which is going to install locally, all the necessary files
‫and folders to have access to this SDK.
‫So now, if I clear my screen
‫and list my directories, we have index.js.
‫We have node_modules.
‫And if you look within node_modules,
‫you're going to get a lot
‫of different packages that do install the xray-sdk.
‫And finally package-lock.json,
‫which is going to lock the version
‫of all these packages right here.
‫So we're good to go.
‫Now in here, we need to edit the permissions of these files.
‫So we'll do a chmod command right here,
‫and we're good to go.
‫Next, we need to zip all the, all the files
‫in here into a zip file called functions.zip.
‫So now if you look at it,
‫we have a functions.zip file available
‫to us within our CloudShell environment.
‫And finally, we need to upload this .zip file
‫into the Lambda console.
‫So we could do it manually
‫by downloading it and then uploading it.
‫But, something else we can do is, use the CLI.
‫So we'll use the CLI, but what we need to do first
‫is to create a role for a Lambda function.
‫So let's go into services
‫and then we'll go into IAM,
‫and we're going to manually create a role
‫for our Lambda functions.
‫So we've got a roles, create role.
‫This is for a service, is going to be for Lambda.
‫I need to scroll down,
‫and then just unzoom and click on next permission.
‫So this is just temporary.
‫Okay. Then I need to add permissions policy.
‫So the basic execution role of Lambda
‫is what we'd like to have added to our role,
‫and then click on next tags, next review.
‫And we'll even call it demo lambda with dependencies.
‫Okay. Create this role.
‫And we're good to go.
‫So now this role has been created and I can just
‫copy the role ARN in here and paste it into my command line.
‫So please make sure to update this ARN
‫for you within your code.
‫Okay, great.
‫So let's just copy this entire code right here.
‫Go into my CloudShell.
‫Paste this command, press enter,
‫and now it says successful.
‫So indeed my Lambda function was created.
‫So let's go into my functions, refresh this.
‫Now we have eight functions
‫and we can find my lambda-xray-with-dependencies function
‫right here, so has been created.
‫And if you look at the code source,
‫we can find the nodes_module, the index.js from before
‫as well as the package-lock.JSON file.
‫Very nice.
‫So all the commands I just did, you could run them directly
‫from your terminal as well, but you need to install NPM
‫in the first place for it to work.
‫So it's, as you want.
‫You could also, if you want, for code source,
‫instead of doing this in the CLI, you could upload from
‫and you could show like a .zip file that you've created.
‫Or, an Amazon S3 location as well.
‫If you upload that .zip file
‫into Amazon S3 in the first place.
‫Okay. But the idea here is
‫that we have uploaded our Lambda function
‫into our Lambda console using dependencies.
‫And so right now, what we could do is
‫that we could test our Lambda function.
‫Now, if we test it, let's have a test.
‫So we create, a simple event.
‫We test it.
‫Now we get a failure and we get an "Access Denied",
‫because our Lambda function is trying to access Amazon S3.
‫Okay. So if we go right here,
‫we can see that it's doing a S3 ListBuckets operation,
‫but it turns out that our Lambda function
‫does not have access to Amazon S3.
‫And another thing is that we've enabled x-ray
‫and we need to go into configuration and also for the x-ray.
‫So monitoring and active tracing;
‫we're going to enable x-ray, which is also
‫going to add the extra permissions into our role.
‫So our role is missing some permissions.
‫So let's go here and refresh this role.
‫Right now we have Lambda basic execution role
‫and the Lambda active tracer role,
‫but we need to attach one more policy.
‫And this policy is going to be
‫around a read only access to S3.
‫So let's do Amazon S3 read only access,
‫which is going to allow us to do the ListBuckets API call.
‫So this is great.
‫Let's go into here,
‫and then we're going to test our function.
‫Let's test it again.
‫And we get another failure,
‫which we'll get an "Access Denied",
‫so it could take a bit of time
‫for the permissions right here to be reflected
‫within your Lambda function.
‫But that's okay.
‫Let's try it one more time.
‫Let's test it.
‫And now we get an execution result of success,
‫and we can see all the buckets
‫within our accounts are listed right here as an outcome.
‫But, because we've enabled the x-ray integration as well,
‫it should be possible for us to go into the x-ray console
‫and look at the service map of all the API calls
‫and invocations done by our Lambda function.
‫So let me wait a few minutes
‫for the service map to be drawn.
‫And here we go, we see our service map right here.
‫And so the cool thing is that we can see
‫that the clients, so us, we invoked the Lambda function.
‫And then the function itself invoked Amazon S3
‫in terms of API calls, right?
‫And we can see that half of them were successful.
‫So the green is successful and half of them was a failure.
‫So this is when we had permission issues.
‫And so we can really have a look
‫at the details of our execution.
‫So I think that's a good example that had an error.
‫50% was okay.
‫And 50% was an error.
‫Now we can have a look at the traces in detail as well.
‫So we can have a look at these two traces.
‫This is a trace that produced an error.
‫So we can have a look and say, okay,
‫the function was invoked.
‫The initialization was okay, the invocation was not okay,
‫because we got a 403 on Lambda
‫using the ListBuckets operation.
‫So we can really see from x-ray what was going on.
‫It was S3.
‫We got a 403,
‫and then we can look at the operation;
‫it was a ListBuckets.
‫And we can have a look at the exception;
‫it was an Access Denied.
‫So S3 x-ray is really helpful,
‫in this case, to have a look at what went wrong.
‫And again, if I look at the x-rays trace
‫for the successful one
‫we can see that the invocation of S3 was good.
‫We can see how long it listed
‫to run the ListBuckets operation,
‫and then to get the response.
‫So again, these traces are amazing
‫because it really gives you access and overviews
‫into the performance of everything you're doing with Lambda.
‫So this is a cool little hands-on because in this hands-on
‫we've seen a lot of different things.
‫We've seen how to package a function
‫and upload it to Lambda using the CLI.
‫We've seen how to package dependencies.
‫We've seen yet again, how to use x-ray.
‫We've seen how to modify the permissions in IAM
‫to get access to S3.
‫So it's a well-rounded example.
‫I hope you liked it.
‫And I will see you in the next lecture.