1 00:00:00,480 --> 00:00:01,313 ‫So in this lecture 2 00:00:01,313 --> 00:00:03,500 ‫we're going to create a CloudFormation template 3 00:00:03,500 --> 00:00:06,640 ‫upload it that will create a Lambda function for us. 4 00:00:06,640 --> 00:00:08,470 ‫So let's open the lambda-CloudFormation 5 00:00:08,470 --> 00:00:09,850 ‫and then the lambda-xray.yaml 6 00:00:09,850 --> 00:00:11,900 ‫And let's have a look at this CloudFormation template. 7 00:00:11,900 --> 00:00:13,390 ‫So first of all, we see 8 00:00:13,390 --> 00:00:15,990 ‫that there are three parameters available to us. 9 00:00:15,990 --> 00:00:18,850 ‫The S3 Bucket parameter, The S3 Key parameter 10 00:00:18,850 --> 00:00:21,020 ‫and the S3 object version parameter. 11 00:00:21,020 --> 00:00:23,660 ‫And these will be helpful to tell CloudFormation 12 00:00:23,660 --> 00:00:27,233 ‫where to get the function zip from in Amazon S3. 13 00:00:28,250 --> 00:00:29,670 ‫Next we create resources. 14 00:00:29,670 --> 00:00:32,670 ‫And the first resource we create is a Lambda execution role. 15 00:00:32,670 --> 00:00:34,120 ‫So it's an IAM role. 16 00:00:34,120 --> 00:00:36,900 ‫There is a policy document which allows Lambda 17 00:00:36,900 --> 00:00:39,300 ‫functions to assume this IAM role 18 00:00:39,300 --> 00:00:41,220 ‫as well as the policy itself. 19 00:00:41,220 --> 00:00:43,180 ‫And the policy contains multiple statements. 20 00:00:43,180 --> 00:00:45,640 ‫The first one is actions on CloudWatch logs 21 00:00:45,640 --> 00:00:47,500 ‫The second one is action on x-ray 22 00:00:47,500 --> 00:00:49,980 ‫to be able to send traces to x-ray 23 00:00:49,980 --> 00:00:51,590 ‫and the last one and finally 24 00:00:51,590 --> 00:00:55,060 ‫some allow operations on S3, Get star, List star 25 00:00:55,060 --> 00:00:57,373 ‫to be able to read it from Amazon S3. 26 00:00:58,330 --> 00:01:01,020 ‫Next to function itself, Lambda with x-ray. 27 00:01:01,020 --> 00:01:03,200 ‫So it's a function in which we have to specify the handlers 28 00:01:03,200 --> 00:01:04,630 ‫so Index.handler 29 00:01:04,630 --> 00:01:06,660 ‫the role which is the Lambda execution role 30 00:01:06,660 --> 00:01:08,050 ‫Arn that we get 31 00:01:08,050 --> 00:01:10,370 ‫and retrieve thanks to this interesting function 32 00:01:10,370 --> 00:01:13,220 ‫get attributes that will be get gotten 33 00:01:13,220 --> 00:01:15,350 ‫from this Lambda role right here 34 00:01:16,310 --> 00:01:17,480 ‫and 35 00:01:17,480 --> 00:01:18,650 ‫the code itself 36 00:01:18,650 --> 00:01:22,470 ‫which is having three inputs the S3 buckets 37 00:01:22,470 --> 00:01:24,980 ‫the S3 key in the S3 object version. 38 00:01:24,980 --> 00:01:26,980 ‫And all of these are references 39 00:01:26,980 --> 00:01:29,220 ‫to the parameters we have specified 40 00:01:29,220 --> 00:01:31,540 ‫at the beginning of the templates. 41 00:01:31,540 --> 00:01:32,800 ‫Okay So we're good to go with this 42 00:01:32,800 --> 00:01:36,660 ‫The runtime is Nodejs14.X, the timer is 10 seconds. 43 00:01:36,660 --> 00:01:38,270 ‫And then we enable x-ray 44 00:01:38,270 --> 00:01:40,422 ‫by having tracing config mode active. 45 00:01:40,422 --> 00:01:43,070 ‫So as we can see, the entire configuration 46 00:01:43,070 --> 00:01:46,340 ‫of our Lambda function can be done through CloudFormation 47 00:01:46,340 --> 00:01:47,940 ‫which is really awesome. 48 00:01:47,940 --> 00:01:51,460 ‫So next we need to just set up things to run these 49 00:01:51,460 --> 00:01:52,730 ‫CloudFormation templates. 50 00:01:52,730 --> 00:01:55,480 ‫So the first thing is that we can go into Amazon is free 51 00:01:55,480 --> 00:01:57,030 ‫and I need to create a new bucket. 52 00:01:57,030 --> 00:01:59,480 ‫So let's create a bucket and I'll call this one 53 00:01:59,480 --> 00:02:01,330 ‫S3 CloudFormation 54 00:02:02,890 --> 00:02:05,090 ‫Lambda demos Stephane 55 00:02:06,220 --> 00:02:07,053 ‫and 56 00:02:07,053 --> 00:02:09,140 ‫in the same region, as I am with my Lambda functions 57 00:02:10,470 --> 00:02:13,390 ‫and I will enable buckets versioning so that we 58 00:02:13,390 --> 00:02:14,223 ‫have a bucket 59 00:02:14,223 --> 00:02:15,810 ‫So we have an object version necessary 60 00:02:15,810 --> 00:02:18,770 ‫for uploading this file through confirmation. 61 00:02:18,770 --> 00:02:19,603 ‫So, okay. 62 00:02:19,603 --> 00:02:20,436 ‫So we're good to go. 63 00:02:20,436 --> 00:02:21,269 ‫We create this bucket. 64 00:02:21,269 --> 00:02:22,980 ‫It has versioning enabled 65 00:02:22,980 --> 00:02:24,880 ‫and I need to just go in it. 66 00:02:24,880 --> 00:02:28,910 ‫So let's open this CloudFormation buckets right here 67 00:02:29,750 --> 00:02:32,650 ‫and I need to upload my function.zip. 68 00:02:32,650 --> 00:02:34,500 ‫So if you're going to go to CloudShell or wherever 69 00:02:34,500 --> 00:02:36,766 ‫you were having this, let's go into the Lambda folder 70 00:02:36,766 --> 00:02:40,140 ‫and have created a function.zip file. 71 00:02:40,140 --> 00:02:44,340 ‫So let's just get the full path to it and I'm going to 72 00:02:44,340 --> 00:02:45,173 ‫download it. 73 00:02:45,173 --> 00:02:48,810 ‫So I copy this, I do actions, download file 74 00:02:48,810 --> 00:02:52,000 ‫and I copy the entire path to my function.zip 75 00:02:52,000 --> 00:02:53,660 ‫within my CloudShell environment. 76 00:02:53,660 --> 00:02:55,020 ‫So I download this file 77 00:02:58,930 --> 00:03:00,140 ‫And the file has been downloaded 78 00:03:00,140 --> 00:03:02,690 ‫So now we can reuse this function.zip 79 00:03:02,690 --> 00:03:07,180 ‫and I can just directly upload it into my S3 buckets. 80 00:03:07,180 --> 00:03:09,000 ‫So it's going to be uploaded 81 00:03:09,000 --> 00:03:09,833 ‫upload 82 00:03:11,230 --> 00:03:12,700 ‫and we're good to go. 83 00:03:12,700 --> 00:03:16,470 ‫So now we are able to reference this function, zip directly 84 00:03:16,470 --> 00:03:17,720 ‫from our CloudFormation templates. 85 00:03:17,720 --> 00:03:20,410 ‫So very good, now lets go to Cloudformation. 86 00:03:20,410 --> 00:03:22,060 ‫So that's open the CloudFormation 87 00:03:23,450 --> 00:03:24,283 ‫Console. 88 00:03:29,150 --> 00:03:30,660 ‫And create a stack. 89 00:03:30,660 --> 00:03:31,890 ‫The template is ready 90 00:03:32,820 --> 00:03:34,720 ‫and I will upload the templates 91 00:03:34,720 --> 00:03:36,580 ‫and I will choose the templates from before. 92 00:03:36,580 --> 00:03:40,690 ‫So let's take the code, go into the lambda section 93 00:03:42,200 --> 00:03:44,833 ‫Lambda CloudFormation and Lambda x-ray.yaml. 94 00:03:45,930 --> 00:03:49,230 ‫Okay. So next I click on next. 95 00:03:49,230 --> 00:03:51,693 ‫This Stack name is demo LambdaCF. 96 00:03:54,200 --> 00:03:56,020 ‫And now we need to specify three parameters 97 00:03:56,020 --> 00:03:57,170 ‫the buckets parameter 98 00:03:57,170 --> 00:04:00,000 ‫the S3 key parameter and the object version. 99 00:04:00,000 --> 00:04:01,670 ‫So the bucket name is right here. 100 00:04:01,670 --> 00:04:04,050 ‫So let me copy this and paste it. 101 00:04:04,050 --> 00:04:06,410 ‫The key is the name of the zip file 102 00:04:06,410 --> 00:04:09,080 ‫So function.zip 103 00:04:09,080 --> 00:04:10,940 ‫and the object version I can get 104 00:04:10,940 --> 00:04:13,090 ‫by just clicking on the object. 105 00:04:13,090 --> 00:04:15,180 ‫And because we have enabled versioning before 106 00:04:15,180 --> 00:04:16,530 ‫on the buckets 107 00:04:16,530 --> 00:04:19,610 ‫the object version should be somewhere around here 108 00:04:19,610 --> 00:04:22,010 ‫versions in here, and here is the object version 109 00:04:23,141 --> 00:04:24,200 ‫of I'll just copy 110 00:04:24,200 --> 00:04:25,780 ‫like this 111 00:04:25,780 --> 00:04:27,870 ‫and then paste it here. 112 00:04:27,870 --> 00:04:30,170 ‫Okay So everything looks good here. 113 00:04:30,170 --> 00:04:34,650 ‫And then I'm going to click on next and I will scroll down 114 00:04:34,650 --> 00:04:37,080 ‫click on next and then 115 00:04:38,680 --> 00:04:39,660 ‫acknowledge the fact 116 00:04:39,660 --> 00:04:42,010 ‫that CloudFormation will create an IAM role for us 117 00:04:42,010 --> 00:04:44,330 ‫which is necessary for our Lambda function 118 00:04:44,330 --> 00:04:46,150 ‫and then create stack. 119 00:04:46,150 --> 00:04:48,760 ‫So my stack creation is in progress 120 00:04:48,760 --> 00:04:51,290 ‫and I'm going to wait for it to be done. 121 00:04:51,290 --> 00:04:54,330 ‫Okay So my CloudFormation stack is now completed. 122 00:04:54,330 --> 00:04:55,750 ‫So my Lambda function was created. 123 00:04:55,750 --> 00:04:59,050 ‫So if you go back into Lambda, refresh this 124 00:04:59,050 --> 00:05:00,960 ‫I have now nine functions 125 00:05:00,960 --> 00:05:03,750 ‫and this one was created by CloudFormation. 126 00:05:03,750 --> 00:05:04,870 ‫So really cool though. 127 00:05:04,870 --> 00:05:05,810 ‫As soon as we opened this function 128 00:05:05,810 --> 00:05:07,260 ‫we get an information message. 129 00:05:07,260 --> 00:05:09,130 ‫This function belongs to an application 130 00:05:09,130 --> 00:05:11,970 ‫because there was an integration between CloudFormation 131 00:05:11,970 --> 00:05:14,120 ‫and Lambda that was detected by the Lambda console. 132 00:05:14,120 --> 00:05:17,759 ‫So it knows that it is managed by a CloudFormation template. 133 00:05:17,759 --> 00:05:19,700 ‫And so, as we can see 134 00:05:19,700 --> 00:05:21,960 ‫the function zip was uploaded correctly 135 00:05:21,960 --> 00:05:23,660 ‫and then you could test their function 136 00:05:23,660 --> 00:05:27,600 ‫as with a simple events, click on create and then test. 137 00:05:27,600 --> 00:05:29,540 ‫And obviously the function would succeed 138 00:05:29,540 --> 00:05:30,960 ‫because it has the necessary permissions 139 00:05:30,960 --> 00:05:32,700 ‫and returns a list of the buckets. 140 00:05:32,700 --> 00:05:35,260 ‫And if you took your time and went into the x-ray console 141 00:05:35,260 --> 00:05:37,190 ‫you should see the extra traces as well. 142 00:05:37,190 --> 00:05:38,100 ‫Appearing. 143 00:05:38,100 --> 00:05:40,500 ‫Thanks to the integration we have with x-ray. 144 00:05:40,500 --> 00:05:42,450 ‫As we can see, if we go to configuration 145 00:05:42,450 --> 00:05:44,990 ‫we can see that active tracing is enabled 146 00:05:44,990 --> 00:05:46,430 ‫for Ireland function. 147 00:05:46,430 --> 00:05:47,263 ‫So that's it. 148 00:05:47,263 --> 00:05:48,850 ‫You can just leave things as is 149 00:05:48,850 --> 00:05:51,421 ‫or if you wanted to clean up, you could take this stack 150 00:05:51,421 --> 00:05:55,070 ‫and then delete it to clean up the Lambda role as well 151 00:05:55,070 --> 00:05:58,890 ‫as the Lambda function within your accounts. 152 00:05:58,890 --> 00:06:00,380 ‫But that's it for this lecture. 153 00:06:00,380 --> 00:06:02,318 ‫We've seen a lot of different 154 00:06:02,318 --> 00:06:03,400 ‫moving parts in here, but really cool. 155 00:06:03,400 --> 00:06:05,150 ‫I will see you in the next lecture.