1 00:00:00,300 --> 00:00:02,260 ‫Okay. So in this hands-on, we're going to have a play 2 00:00:02,260 --> 00:00:05,250 ‫and create a Lambda function with dependencies. 3 00:00:05,250 --> 00:00:08,130 ‫To do so let's go into Lambda with dependencies, 4 00:00:08,130 --> 00:00:09,810 ‫and there are two files interesting to you; 5 00:00:09,810 --> 00:00:12,450 ‫index.js and steps.js. 6 00:00:12,450 --> 00:00:14,620 ‫Now for you to be able to reproduce all the steps, 7 00:00:14,620 --> 00:00:17,210 ‫we're going to be able to use CloudShell, 8 00:00:17,210 --> 00:00:20,000 ‫and CloudShell is going to give us a terminal 9 00:00:20,000 --> 00:00:22,020 ‫that is connected directly into our accounts 10 00:00:22,020 --> 00:00:23,520 ‫that has NPM installed. 11 00:00:23,520 --> 00:00:28,340 ‫So NPM is a way to manage packages on node JS. 12 00:00:28,340 --> 00:00:30,050 ‫And so this is why we want to use CloudShell, 13 00:00:30,050 --> 00:00:33,190 ‫because the setup is already done on CloudShell for you. 14 00:00:33,190 --> 00:00:35,310 ‫So the other thing is that you need to create 15 00:00:35,310 --> 00:00:36,360 ‫a Lambda folder. 16 00:00:36,360 --> 00:00:37,990 ‫So I'll remove one because I just created one, 17 00:00:37,990 --> 00:00:39,600 ‫but create a Lambda folder. 18 00:00:39,600 --> 00:00:43,080 ‫So you do make directory Lambda 19 00:00:43,080 --> 00:00:44,630 ‫and then you go into the Lambda folder. 20 00:00:44,630 --> 00:00:45,810 ‫So now you're in the Lambda folder, 21 00:00:45,810 --> 00:00:47,510 ‫and there is nothing in it. 22 00:00:47,510 --> 00:00:49,370 ‫And the first thing is that you need to add 23 00:00:49,370 --> 00:00:51,130 ‫this index.js file. 24 00:00:51,130 --> 00:00:55,590 ‫So I will just do sudo yum install -y nano 25 00:00:55,590 --> 00:00:57,570 ‫to have a text editor very quickly. 26 00:00:57,570 --> 00:01:02,570 ‫So nano is installed, and I do now nano index.js, 27 00:01:02,920 --> 00:01:06,810 ‫which is going to allow me to copy all this code into here. 28 00:01:06,810 --> 00:01:08,550 ‫And I will remove this prompt, 29 00:01:08,550 --> 00:01:09,920 ‫and I will just copy all this code, 30 00:01:09,920 --> 00:01:14,740 ‫and paste it into nano control + X, Y, enter. 31 00:01:14,740 --> 00:01:18,590 ‫And now if we clear this and do cat index.js, 32 00:01:18,590 --> 00:01:21,180 ‫we can see that the file has been uploaded correctly 33 00:01:21,180 --> 00:01:24,260 ‫into our CloudShell environment. 34 00:01:24,260 --> 00:01:25,840 ‫So next, let's go into the steps 35 00:01:25,840 --> 00:01:28,130 ‫to install some dependencies, 36 00:01:28,130 --> 00:01:30,380 ‫and make sure the function will be packaged correctly 37 00:01:30,380 --> 00:01:33,010 ‫to be uploaded into the Lambda service. 38 00:01:33,010 --> 00:01:35,020 ‫So first, if we look at this index.js 39 00:01:35,020 --> 00:01:38,660 ‫we can see that it requires the xray-sdk-core, 40 00:01:38,660 --> 00:01:40,940 ‫and this is something that we need to bundle 41 00:01:40,940 --> 00:01:42,650 ‫with our Lambda function. 42 00:01:42,650 --> 00:01:45,200 ‫Then we are opening the AWS SDK 43 00:01:45,200 --> 00:01:47,070 ‫to talk to Amazon S3, 44 00:01:47,070 --> 00:01:50,880 ‫and we return a ListBuckets operation into Amazon S3. 45 00:01:50,880 --> 00:01:52,490 ‫So what we need to do is to make sure 46 00:01:52,490 --> 00:01:54,670 ‫that our Lambda function does have access 47 00:01:54,670 --> 00:01:58,420 ‫to the xray-sdk for AWS. 48 00:01:58,420 --> 00:02:00,300 ‫So this is what this command does right here: 49 00:02:00,300 --> 00:02:02,453 ‫npm install aws-xray-sdk, 50 00:02:03,650 --> 00:02:07,890 ‫which is going to install locally, all the necessary files 51 00:02:07,890 --> 00:02:10,690 ‫and folders to have access to this SDK. 52 00:02:10,690 --> 00:02:12,610 ‫So now, if I clear my screen 53 00:02:12,610 --> 00:02:15,980 ‫and list my directories, we have index.js. 54 00:02:15,980 --> 00:02:17,140 ‫We have node_modules. 55 00:02:17,140 --> 00:02:19,150 ‫And if you look within node_modules, 56 00:02:19,150 --> 00:02:20,170 ‫you're going to get a lot 57 00:02:20,170 --> 00:02:24,221 ‫of different packages that do install the xray-sdk. 58 00:02:24,221 --> 00:02:25,820 ‫And finally package-lock.json, 59 00:02:25,820 --> 00:02:27,700 ‫which is going to lock the version 60 00:02:27,700 --> 00:02:30,370 ‫of all these packages right here. 61 00:02:30,370 --> 00:02:31,580 ‫So we're good to go. 62 00:02:31,580 --> 00:02:34,370 ‫Now in here, we need to edit the permissions of these files. 63 00:02:34,370 --> 00:02:37,220 ‫So we'll do a chmod command right here, 64 00:02:37,220 --> 00:02:38,440 ‫and we're good to go. 65 00:02:38,440 --> 00:02:41,950 ‫Next, we need to zip all the, all the files 66 00:02:41,950 --> 00:02:46,310 ‫in here into a zip file called functions.zip. 67 00:02:46,310 --> 00:02:47,830 ‫So now if you look at it, 68 00:02:47,830 --> 00:02:51,040 ‫we have a functions.zip file available 69 00:02:51,040 --> 00:02:53,170 ‫to us within our CloudShell environment. 70 00:02:53,170 --> 00:02:56,600 ‫And finally, we need to upload this .zip file 71 00:02:56,600 --> 00:02:58,800 ‫into the Lambda console. 72 00:02:58,800 --> 00:02:59,770 ‫So we could do it manually 73 00:02:59,770 --> 00:03:01,470 ‫by downloading it and then uploading it. 74 00:03:01,470 --> 00:03:04,400 ‫But, something else we can do is, use the CLI. 75 00:03:04,400 --> 00:03:06,820 ‫So we'll use the CLI, but what we need to do first 76 00:03:06,820 --> 00:03:09,560 ‫is to create a role for a Lambda function. 77 00:03:09,560 --> 00:03:11,460 ‫So let's go into services 78 00:03:11,460 --> 00:03:13,403 ‫and then we'll go into IAM, 79 00:03:15,260 --> 00:03:17,310 ‫and we're going to manually create a role 80 00:03:17,310 --> 00:03:18,190 ‫for our Lambda functions. 81 00:03:18,190 --> 00:03:20,620 ‫So we've got a roles, create role. 82 00:03:20,620 --> 00:03:23,287 ‫This is for a service, is going to be for Lambda. 83 00:03:23,287 --> 00:03:25,840 ‫I need to scroll down, 84 00:03:25,840 --> 00:03:28,810 ‫and then just unzoom and click on next permission. 85 00:03:28,810 --> 00:03:30,690 ‫So this is just temporary. 86 00:03:30,690 --> 00:03:34,720 ‫Okay. Then I need to add permissions policy. 87 00:03:34,720 --> 00:03:37,460 ‫So the basic execution role of Lambda 88 00:03:37,460 --> 00:03:40,570 ‫is what we'd like to have added to our role, 89 00:03:40,570 --> 00:03:42,890 ‫and then click on next tags, next review. 90 00:03:42,890 --> 00:03:47,840 ‫And we'll even call it demo lambda with dependencies. 91 00:03:47,840 --> 00:03:49,913 ‫Okay. Create this role. 92 00:03:50,750 --> 00:03:51,583 ‫And we're good to go. 93 00:03:51,583 --> 00:03:54,670 ‫So now this role has been created and I can just 94 00:03:54,670 --> 00:03:59,670 ‫copy the role ARN in here and paste it into my command line. 95 00:04:00,020 --> 00:04:02,610 ‫So please make sure to update this ARN 96 00:04:02,610 --> 00:04:04,630 ‫for you within your code. 97 00:04:04,630 --> 00:04:05,463 ‫Okay, great. 98 00:04:05,463 --> 00:04:09,160 ‫So let's just copy this entire code right here. 99 00:04:09,160 --> 00:04:10,930 ‫Go into my CloudShell. 100 00:04:10,930 --> 00:04:13,223 ‫Paste this command, press enter, 101 00:04:14,370 --> 00:04:16,180 ‫and now it says successful. 102 00:04:16,180 --> 00:04:19,160 ‫So indeed my Lambda function was created. 103 00:04:19,160 --> 00:04:21,720 ‫So let's go into my functions, refresh this. 104 00:04:21,720 --> 00:04:23,720 ‫Now we have eight functions 105 00:04:23,720 --> 00:04:27,200 ‫and we can find my lambda-xray-with-dependencies function 106 00:04:27,200 --> 00:04:29,070 ‫right here, so has been created. 107 00:04:29,070 --> 00:04:30,580 ‫And if you look at the code source, 108 00:04:30,580 --> 00:04:34,240 ‫we can find the nodes_module, the index.js from before 109 00:04:34,240 --> 00:04:36,620 ‫as well as the package-lock.JSON file. 110 00:04:36,620 --> 00:04:37,453 ‫Very nice. 111 00:04:37,453 --> 00:04:39,910 ‫So all the commands I just did, you could run them directly 112 00:04:39,910 --> 00:04:42,530 ‫from your terminal as well, but you need to install NPM 113 00:04:42,530 --> 00:04:43,860 ‫in the first place for it to work. 114 00:04:43,860 --> 00:04:45,600 ‫So it's, as you want. 115 00:04:45,600 --> 00:04:47,660 ‫You could also, if you want, for code source, 116 00:04:47,660 --> 00:04:50,600 ‫instead of doing this in the CLI, you could upload from 117 00:04:50,600 --> 00:04:52,720 ‫and you could show like a .zip file that you've created. 118 00:04:52,720 --> 00:04:54,158 ‫Or, an Amazon S3 location as well. 119 00:04:54,158 --> 00:04:56,040 ‫If you upload that .zip file 120 00:04:56,040 --> 00:04:58,170 ‫into Amazon S3 in the first place. 121 00:04:58,170 --> 00:04:59,780 ‫Okay. But the idea here is 122 00:04:59,780 --> 00:05:02,610 ‫that we have uploaded our Lambda function 123 00:05:02,610 --> 00:05:07,010 ‫into our Lambda console using dependencies. 124 00:05:07,010 --> 00:05:08,400 ‫And so right now, what we could do is 125 00:05:08,400 --> 00:05:10,750 ‫that we could test our Lambda function. 126 00:05:10,750 --> 00:05:12,450 ‫Now, if we test it, let's have a test. 127 00:05:12,450 --> 00:05:14,393 ‫So we create, a simple event. 128 00:05:16,310 --> 00:05:18,163 ‫We test it. 129 00:05:20,250 --> 00:05:22,820 ‫Now we get a failure and we get an "Access Denied", 130 00:05:22,820 --> 00:05:26,360 ‫because our Lambda function is trying to access Amazon S3. 131 00:05:26,360 --> 00:05:27,830 ‫Okay. So if we go right here, 132 00:05:27,830 --> 00:05:32,830 ‫we can see that it's doing a S3 ListBuckets operation, 133 00:05:33,090 --> 00:05:35,110 ‫but it turns out that our Lambda function 134 00:05:35,110 --> 00:05:37,290 ‫does not have access to Amazon S3. 135 00:05:37,290 --> 00:05:39,250 ‫And another thing is that we've enabled x-ray 136 00:05:39,250 --> 00:05:43,620 ‫and we need to go into configuration and also for the x-ray. 137 00:05:43,620 --> 00:05:45,980 ‫So monitoring and active tracing; 138 00:05:45,980 --> 00:05:48,750 ‫we're going to enable x-ray, which is also 139 00:05:48,750 --> 00:05:51,300 ‫going to add the extra permissions into our role. 140 00:05:51,300 --> 00:05:53,070 ‫So our role is missing some permissions. 141 00:05:53,070 --> 00:05:55,793 ‫So let's go here and refresh this role. 142 00:05:57,400 --> 00:05:59,720 ‫Right now we have Lambda basic execution role 143 00:05:59,720 --> 00:06:02,730 ‫and the Lambda active tracer role, 144 00:06:02,730 --> 00:06:04,713 ‫but we need to attach one more policy. 145 00:06:05,720 --> 00:06:06,940 ‫And this policy is going to be 146 00:06:06,940 --> 00:06:09,410 ‫around a read only access to S3. 147 00:06:09,410 --> 00:06:12,240 ‫So let's do Amazon S3 read only access, 148 00:06:12,240 --> 00:06:16,250 ‫which is going to allow us to do the ListBuckets API call. 149 00:06:16,250 --> 00:06:17,410 ‫So this is great. 150 00:06:17,410 --> 00:06:18,880 ‫Let's go into here, 151 00:06:18,880 --> 00:06:20,620 ‫and then we're going to test our function. 152 00:06:20,620 --> 00:06:21,763 ‫Let's test it again. 153 00:06:24,030 --> 00:06:24,890 ‫And we get another failure, 154 00:06:24,890 --> 00:06:26,140 ‫which we'll get an "Access Denied", 155 00:06:26,140 --> 00:06:27,760 ‫so it could take a bit of time 156 00:06:27,760 --> 00:06:29,720 ‫for the permissions right here to be reflected 157 00:06:29,720 --> 00:06:30,970 ‫within your Lambda function. 158 00:06:30,970 --> 00:06:32,160 ‫But that's okay. 159 00:06:32,160 --> 00:06:34,360 ‫Let's try it one more time. 160 00:06:34,360 --> 00:06:35,290 ‫Let's test it. 161 00:06:35,290 --> 00:06:37,960 ‫And now we get an execution result of success, 162 00:06:37,960 --> 00:06:40,070 ‫and we can see all the buckets 163 00:06:40,070 --> 00:06:43,760 ‫within our accounts are listed right here as an outcome. 164 00:06:43,760 --> 00:06:46,660 ‫But, because we've enabled the x-ray integration as well, 165 00:06:46,660 --> 00:06:49,660 ‫it should be possible for us to go into the x-ray console 166 00:06:49,660 --> 00:06:52,480 ‫and look at the service map of all the API calls 167 00:06:52,480 --> 00:06:55,290 ‫and invocations done by our Lambda function. 168 00:06:55,290 --> 00:06:56,520 ‫So let me wait a few minutes 169 00:06:56,520 --> 00:06:58,870 ‫for the service map to be drawn. 170 00:06:58,870 --> 00:07:01,690 ‫And here we go, we see our service map right here. 171 00:07:01,690 --> 00:07:03,550 ‫And so the cool thing is that we can see 172 00:07:03,550 --> 00:07:07,750 ‫that the clients, so us, we invoked the Lambda function. 173 00:07:07,750 --> 00:07:10,960 ‫And then the function itself invoked Amazon S3 174 00:07:10,960 --> 00:07:12,480 ‫in terms of API calls, right? 175 00:07:12,480 --> 00:07:14,560 ‫And we can see that half of them were successful. 176 00:07:14,560 --> 00:07:16,600 ‫So the green is successful and half of them was a failure. 177 00:07:16,600 --> 00:07:18,420 ‫So this is when we had permission issues. 178 00:07:18,420 --> 00:07:20,480 ‫And so we can really have a look 179 00:07:20,480 --> 00:07:22,090 ‫at the details of our execution. 180 00:07:22,090 --> 00:07:23,920 ‫So I think that's a good example that had an error. 181 00:07:23,920 --> 00:07:25,020 ‫50% was okay. 182 00:07:25,020 --> 00:07:26,750 ‫And 50% was an error. 183 00:07:26,750 --> 00:07:29,490 ‫Now we can have a look at the traces in detail as well. 184 00:07:29,490 --> 00:07:32,840 ‫So we can have a look at these two traces. 185 00:07:32,840 --> 00:07:34,690 ‫This is a trace that produced an error. 186 00:07:34,690 --> 00:07:36,600 ‫So we can have a look and say, okay, 187 00:07:36,600 --> 00:07:38,140 ‫the function was invoked. 188 00:07:38,140 --> 00:07:40,860 ‫The initialization was okay, the invocation was not okay, 189 00:07:40,860 --> 00:07:43,940 ‫because we got a 403 on Lambda 190 00:07:43,940 --> 00:07:45,560 ‫using the ListBuckets operation. 191 00:07:45,560 --> 00:07:48,560 ‫So we can really see from x-ray what was going on. 192 00:07:48,560 --> 00:07:49,460 ‫It was S3. 193 00:07:49,460 --> 00:07:51,200 ‫We got a 403, 194 00:07:51,200 --> 00:07:52,970 ‫and then we can look at the operation; 195 00:07:52,970 --> 00:07:54,600 ‫it was a ListBuckets. 196 00:07:54,600 --> 00:07:56,110 ‫And we can have a look at the exception; 197 00:07:56,110 --> 00:07:57,230 ‫it was an Access Denied. 198 00:07:57,230 --> 00:08:00,190 ‫So S3 x-ray is really helpful, 199 00:08:00,190 --> 00:08:03,150 ‫in this case, to have a look at what went wrong. 200 00:08:03,150 --> 00:08:05,870 ‫And again, if I look at the x-rays trace 201 00:08:05,870 --> 00:08:06,830 ‫for the successful one 202 00:08:06,830 --> 00:08:10,230 ‫we can see that the invocation of S3 was good. 203 00:08:10,230 --> 00:08:11,770 ‫We can see how long it listed 204 00:08:11,770 --> 00:08:13,740 ‫to run the ListBuckets operation, 205 00:08:13,740 --> 00:08:15,260 ‫and then to get the response. 206 00:08:15,260 --> 00:08:17,450 ‫So again, these traces are amazing 207 00:08:17,450 --> 00:08:20,170 ‫because it really gives you access and overviews 208 00:08:20,170 --> 00:08:23,590 ‫into the performance of everything you're doing with Lambda. 209 00:08:23,590 --> 00:08:25,580 ‫So this is a cool little hands-on because in this hands-on 210 00:08:25,580 --> 00:08:27,130 ‫we've seen a lot of different things. 211 00:08:27,130 --> 00:08:29,170 ‫We've seen how to package a function 212 00:08:29,170 --> 00:08:32,390 ‫and upload it to Lambda using the CLI. 213 00:08:32,390 --> 00:08:34,270 ‫We've seen how to package dependencies. 214 00:08:34,270 --> 00:08:36,560 ‫We've seen yet again, how to use x-ray. 215 00:08:36,560 --> 00:08:38,760 ‫We've seen how to modify the permissions in IAM 216 00:08:38,760 --> 00:08:39,960 ‫to get access to S3. 217 00:08:39,960 --> 00:08:41,800 ‫So it's a well-rounded example. 218 00:08:41,800 --> 00:08:42,660 ‫I hope you liked it. 219 00:08:42,660 --> 00:08:44,610 ‫And I will see you in the next lecture.