1 00:00:00,140 --> 00:00:04,190 ‫So let's go ahead to practice stages and stage variables. 2 00:00:04,190 --> 00:00:05,600 ‫So the first thing I want to do 3 00:00:05,600 --> 00:00:07,640 ‫is to just change my Lambda functions, 4 00:00:07,640 --> 00:00:10,270 ‫so I'm just going to change my first Lambda function, 5 00:00:10,270 --> 00:00:13,450 ‫so the "root get", to make this very simple. 6 00:00:13,450 --> 00:00:15,130 ‫And I'm going to say right here 7 00:00:15,130 --> 00:00:19,050 ‫that this is "Hello from Lamda v1!" 8 00:00:19,050 --> 00:00:21,250 ‫And so I'm going to save this 9 00:00:21,250 --> 00:00:23,150 ‫and I'm going to publish this version. 10 00:00:23,150 --> 00:00:24,380 ‫So, I've published this new version, 11 00:00:24,380 --> 00:00:26,200 ‫this is my version one. 12 00:00:26,200 --> 00:00:27,720 ‫And then I'm going to go back 13 00:00:27,720 --> 00:00:30,220 ‫to my latest, I'm going to create 14 00:00:30,220 --> 00:00:32,440 ‫a new version so I go to my Lambda function, 15 00:00:32,440 --> 00:00:34,137 ‫I say, "Hello From Lambda v2!" 16 00:00:35,100 --> 00:00:38,280 ‫I will save this and I will also publish 17 00:00:38,280 --> 00:00:41,710 ‫this new version, so now this is my version two. 18 00:00:41,710 --> 00:00:42,900 ‫And now that we have two versions, 19 00:00:42,900 --> 00:00:45,110 ‫we can create multiple aliases. 20 00:00:45,110 --> 00:00:49,030 ‫So I can create an alias, and Action, Create alias. 21 00:00:49,030 --> 00:00:51,780 ‫And this one is going to be called "DEV." 22 00:00:51,780 --> 00:00:53,500 ‫And it's going to point to the latest version 23 00:00:53,500 --> 00:00:54,960 ‫of our Lambda function. 24 00:00:54,960 --> 00:00:57,812 ‫Then there's going to be an alias named "TEST." 25 00:00:57,812 --> 00:01:00,390 ‫so I'll create an alias, I'll name is "TEST". 26 00:01:00,390 --> 00:01:02,320 ‫And it's going to point to version two 27 00:01:02,320 --> 00:01:04,130 ‫of our Lambda function. 28 00:01:04,130 --> 00:01:07,180 ‫And then finally, there will be an alias "PROD." 29 00:01:07,180 --> 00:01:09,810 ‫So I'll call this one "PROD" and it's going 30 00:01:09,810 --> 00:01:12,320 ‫to point to version one of our Lambda function. 31 00:01:12,320 --> 00:01:13,940 ‫So this a set-up we've done from before, 32 00:01:13,940 --> 00:01:15,610 ‫so this is why I'm going a bit faster. 33 00:01:15,610 --> 00:01:17,570 ‫So we have three Lambda versions, 34 00:01:17,570 --> 00:01:19,440 ‫actually one latest and two Lambda versions. 35 00:01:19,440 --> 00:01:22,440 ‫And then three aliases, okay? 36 00:01:22,440 --> 00:01:23,880 ‫So, extremely simple, if we test 37 00:01:23,880 --> 00:01:25,930 ‫the PROD alias, and we test it. 38 00:01:25,930 --> 00:01:28,320 ‫It says, "Hello from Lambda v1!" 39 00:01:28,320 --> 00:01:30,407 ‫And if we test version two, it should say. 40 00:01:30,407 --> 00:01:32,290 ‫"Hello from Lambda v2!" 41 00:01:32,290 --> 00:01:33,230 ‫Excellent. 42 00:01:33,230 --> 00:01:35,590 ‫So, back into our API Gateway, 43 00:01:35,590 --> 00:01:38,690 ‫now we want to be able to point to the right alias. 44 00:01:38,690 --> 00:01:42,300 ‫So, for Resources, under my GET in here, 45 00:01:42,300 --> 00:01:47,070 ‫I'm going to change the API that gets invoked. 46 00:01:47,070 --> 00:01:49,100 ‫So I'll try to make this even more simple. 47 00:01:49,100 --> 00:01:51,340 ‫I'll create a resource and I'll call 48 00:01:51,340 --> 00:01:54,890 ‫this resource "dynamic" or something like this. 49 00:01:54,890 --> 00:01:56,880 ‫Or "stagevariables," even better. 50 00:01:56,880 --> 00:01:59,310 ‫Stagevariables, and this is going 51 00:01:59,310 --> 00:02:01,720 ‫to be demo-ing the stage variables feature. 52 00:02:01,720 --> 00:02:04,150 ‫So, I'll create this resource. 53 00:02:04,150 --> 00:02:05,800 ‫And underneath, I'm going to create 54 00:02:05,800 --> 00:02:08,020 ‫a GET so I will create a Method. 55 00:02:08,020 --> 00:02:10,330 ‫And this is a GET and it's going 56 00:02:10,330 --> 00:02:11,880 ‫to be invoking our Lambda function. 57 00:02:11,880 --> 00:02:15,440 ‫So, a Lambda function in proxy mode, 58 00:02:15,440 --> 00:02:17,880 ‫and the Lambda function, you get the name, 59 00:02:17,880 --> 00:02:20,700 ‫so get this entire name right here. 60 00:02:20,700 --> 00:02:22,340 ‫So I copy and paste it. 61 00:02:22,340 --> 00:02:23,670 ‫But I'm going to add something, 62 00:02:23,670 --> 00:02:25,490 ‫so I'd add a column. 63 00:02:25,490 --> 00:02:28,430 ‫Then dollar sign, I open brackets 64 00:02:28,430 --> 00:02:33,273 ‫and I write "stageVariables" with an S, dot, "lambdaAlias." 65 00:02:34,940 --> 00:02:36,600 ‫And I close this. 66 00:02:36,600 --> 00:02:38,600 ‫So here we are defining the fact 67 00:02:38,600 --> 00:02:40,480 ‫that the Lambda function we want to invoke, 68 00:02:40,480 --> 00:02:42,220 ‫right now, we don't really know it. 69 00:02:42,220 --> 00:02:44,270 ‫But this will be defined by the value 70 00:02:44,270 --> 00:02:47,380 ‫of the stage variable called "lambdaAlias." 71 00:02:47,380 --> 00:02:48,430 ‫So hopefully this works. 72 00:02:48,430 --> 00:02:51,267 ‫I'm going to save this and now it says, 73 00:02:51,267 --> 00:02:53,900 ‫"you defined your Lambda function as a stage variable. 74 00:02:53,900 --> 00:02:55,040 ‫Please ensure that you have the appropriate 75 00:02:55,040 --> 00:02:57,340 ‫Function Policy on all functions you will use." 76 00:02:57,340 --> 00:02:58,680 ‫And so for this, you can run 77 00:02:58,680 --> 00:03:02,910 ‫the AWS CLI command to do it correctly. 78 00:03:02,910 --> 00:03:05,230 ‫So I'm going to execute this, so, 79 00:03:05,230 --> 00:03:08,470 ‫if we look at the current permissions 80 00:03:08,470 --> 00:03:09,430 ‫of our Lambda functions. 81 00:03:09,430 --> 00:03:11,100 ‫So if we look at these permissions, 82 00:03:11,100 --> 00:03:12,600 ‫I'm going to refresh this page. 83 00:03:12,600 --> 00:03:15,350 ‫So if we look at the current Resource-based policy, 84 00:03:15,350 --> 00:03:17,690 ‫as we can see, it's empty. 85 00:03:17,690 --> 00:03:20,650 ‫And so we are going to add a resource-based policy 86 00:03:20,650 --> 00:03:22,390 ‫for this Lambda function. 87 00:03:22,390 --> 00:03:24,160 ‫So, let's go ahead and actually 88 00:03:24,160 --> 00:03:25,450 ‫it's going to be at the alias levels, 89 00:03:25,450 --> 00:03:28,110 ‫so I will go for example to the DEV alias, 90 00:03:28,110 --> 00:03:29,900 ‫and there is no Resource-based policy. 91 00:03:29,900 --> 00:03:30,990 ‫So let's go ahead and add one. 92 00:03:30,990 --> 00:03:34,490 ‫So I'm going go in my CLI, and copy this command 93 00:03:34,490 --> 00:03:36,130 ‫and I have to change something. 94 00:03:36,130 --> 00:03:39,930 ‫So I have to change and instead of the entire 95 00:03:39,930 --> 00:03:42,320 ‫stage variable in here, I'm going to remove 96 00:03:42,320 --> 00:03:46,620 ‫everything and I'm just going to replace this with DEV. 97 00:03:46,620 --> 00:03:49,370 ‫So once I'm done, I'll just write DEV. 98 00:03:49,370 --> 00:03:52,730 ‫Press Enter, and this has not functioned 99 00:03:52,730 --> 00:03:54,130 ‫because I'm in the wrong region. 100 00:03:54,130 --> 00:03:55,311 ‫So, region, region. 101 00:03:55,311 --> 00:03:59,980 ‫So, "--region eu-west-2" 102 00:03:59,980 --> 00:04:01,900 ‫And this should work now. 103 00:04:01,900 --> 00:04:03,480 ‫Okay, so this works. 104 00:04:03,480 --> 00:04:05,610 ‫Then, I'll do the exact same thing, 105 00:04:05,610 --> 00:04:08,400 ‫but this time I'll do it not with DEV, but with TEST. 106 00:04:08,400 --> 00:04:11,050 ‫So I replace the text on the right hand side by TEST. 107 00:04:12,220 --> 00:04:14,020 ‫And this has worked. 108 00:04:14,020 --> 00:04:16,290 ‫And then finally, I'll do one last time 109 00:04:16,290 --> 00:04:19,490 ‫this, but I will replace the text 110 00:04:19,490 --> 00:04:23,223 ‫with PROD so, colon PROD. 111 00:04:24,180 --> 00:04:25,440 ‫And we're good to go. 112 00:04:25,440 --> 00:04:27,030 ‫So this is something that the council 113 00:04:27,030 --> 00:04:28,080 ‫currently doesn't do for us, 114 00:04:28,080 --> 00:04:29,460 ‫we have to do it ourselves. 115 00:04:29,460 --> 00:04:31,853 ‫So the reason is now if I refresh this page, 116 00:04:32,910 --> 00:04:36,040 ‫we can see, and we'll see it very very soon, 117 00:04:36,040 --> 00:04:38,310 ‫so if I go to versions, alias and then 118 00:04:38,310 --> 00:04:41,520 ‫I'll go to PROD, we can see the Resource-based policy 119 00:04:41,520 --> 00:04:44,730 ‫for PROD, does allow my API gateway to invoke 120 00:04:44,730 --> 00:04:46,160 ‫my PROD function. 121 00:04:46,160 --> 00:04:47,540 ‫My PROD alias, and this is going 122 00:04:47,540 --> 00:04:50,120 ‫to be the same if we go into DEV, 123 00:04:50,120 --> 00:04:53,550 ‫and this is going to be the same if we go into TEST. 124 00:04:53,550 --> 00:04:54,650 ‫So this is something that's necessary 125 00:04:54,650 --> 00:04:57,290 ‫to do when you're using stage variables, 126 00:04:57,290 --> 00:04:59,210 ‫which is what we're trying to demonstrate right here. 127 00:04:59,210 --> 00:05:00,370 ‫So this is what the error message 128 00:05:00,370 --> 00:05:02,640 ‫was saying, now I click on "OK." 129 00:05:02,640 --> 00:05:04,970 ‫So now I can test my stage variables, 130 00:05:04,970 --> 00:05:07,620 ‫so I'm going to click on Test, I scroll down 131 00:05:07,620 --> 00:05:09,800 ‫and here there's a stage variable I have to fill. 132 00:05:09,800 --> 00:05:12,600 ‫So if I write "DEV" for example, 133 00:05:12,600 --> 00:05:15,730 ‫and test it, it says, "Hello from Lambda v2!" 134 00:05:15,730 --> 00:05:17,900 ‫Which is the exact output we would get 135 00:05:17,900 --> 00:05:21,530 ‫if we go to the alias DEV and run our functions. 136 00:05:21,530 --> 00:05:24,350 ‫So we would get, "Hello from Lambda v2!" 137 00:05:24,350 --> 00:05:25,183 ‫Now, let's test it out. 138 00:05:25,183 --> 00:05:27,480 ‫So we'll do TEST and see what the outcome 139 00:05:27,480 --> 00:05:30,730 ‫is so it again will say, "Hello from Lamda v2!" 140 00:05:30,730 --> 00:05:32,380 ‫which is the same we would have 141 00:05:32,380 --> 00:05:34,350 ‫if we had the TEST in here. 142 00:05:34,350 --> 00:05:35,417 ‫So we'll just test it and it says, 143 00:05:35,417 --> 00:05:36,760 ‫"Hello from Lamda v2!" 144 00:05:36,760 --> 00:05:39,020 ‫But then if we actually use the PROD, 145 00:05:39,020 --> 00:05:41,280 ‫so, I'll write PROD in here, 146 00:05:41,280 --> 00:05:44,090 ‫and click on Test, I get, "Hello from Lamda v1!" 147 00:05:44,090 --> 00:05:46,530 ‫So that means that my stage variables are indeed working. 148 00:05:46,530 --> 00:05:48,510 ‫And this is the same output we would get 149 00:05:48,510 --> 00:05:51,130 ‫if we went into the alias PROD, 150 00:05:51,130 --> 00:05:52,367 ‫and then test our function, which has, 151 00:05:52,367 --> 00:05:54,400 ‫"Hello from Lamda v1!" 152 00:05:54,400 --> 00:05:55,233 ‫So this is excellent. 153 00:05:55,233 --> 00:05:57,400 ‫That means our stage variables are working. 154 00:05:57,400 --> 00:05:59,630 ‫But now we need to deploy this API 155 00:05:59,630 --> 00:06:01,540 ‫and use it accordingly so I'm going 156 00:06:01,540 --> 00:06:04,760 ‫to deploy my API into my existing 157 00:06:04,760 --> 00:06:08,140 ‫deployment stage called DEV and click on Deploy. 158 00:06:08,140 --> 00:06:11,290 ‫And in DEV, I go into stage variables, 159 00:06:11,290 --> 00:06:12,640 ‫I will add a stage variable 160 00:06:12,640 --> 00:06:14,410 ‫and I'll call it "lambdaAlias." 161 00:06:15,590 --> 00:06:18,080 ‫And the value is going to be DEV. 162 00:06:18,080 --> 00:06:19,510 ‫And this is something I have to do once 163 00:06:19,510 --> 00:06:22,570 ‫for each stage I have my API into. 164 00:06:22,570 --> 00:06:24,140 ‫So this is my first API, then I go 165 00:06:24,140 --> 00:06:26,910 ‫to my second API that I'm going to deploy, 166 00:06:26,910 --> 00:06:31,720 ‫so I'll deploy a new API, and I'll call this one TEST. 167 00:06:31,720 --> 00:06:34,890 ‫Deploy it, and in here, in my stage variables, 168 00:06:34,890 --> 00:06:36,157 ‫I have to create a new one, so, 169 00:06:36,157 --> 00:06:39,883 ‫"lambdaAlias" and I'll name it TEST. 170 00:06:40,770 --> 00:06:43,310 ‫And then finally, I will create a third 171 00:06:43,310 --> 00:06:46,930 ‫stage, so, I'll create my PROD stage, 172 00:06:46,930 --> 00:06:49,610 ‫and I can use a deployment that we used from before, 173 00:06:49,610 --> 00:06:51,110 ‫another way of doing things. 174 00:06:51,110 --> 00:06:54,340 ‫I'll create it and I'll define a stage variable, 175 00:06:54,340 --> 00:06:59,000 ‫and this one I will say, "lambdaAlias", PROD. 176 00:06:59,000 --> 00:07:00,060 ‫Here we go. 177 00:07:00,060 --> 00:07:02,490 ‫So we have deployed three different stages, 178 00:07:02,490 --> 00:07:03,760 ‫so now let's go and test it out 179 00:07:03,760 --> 00:07:08,380 ‫from the URL, so, I will click on slash PROD, 180 00:07:08,380 --> 00:07:10,040 ‫and then what was the name of my resource? 181 00:07:10,040 --> 00:07:11,660 ‫I remember, Stage variable, 182 00:07:11,660 --> 00:07:16,400 ‫so I go to slash "stagevariables," press on Enter, 183 00:07:16,400 --> 00:07:18,340 ‫it says, "Hello from Lamda v1!" 184 00:07:18,340 --> 00:07:19,700 ‫But if I switch my stage and go 185 00:07:19,700 --> 00:07:22,150 ‫to DEV, it says, "Hello from Lamda v2!" 186 00:07:22,150 --> 00:07:24,370 ‫And likewise, for TEST, it should go 187 00:07:24,370 --> 00:07:26,440 ‫to "Hello from Lamda v2!" 188 00:07:26,440 --> 00:07:27,430 ‫So this is excellent. 189 00:07:27,430 --> 00:07:30,250 ‫This was a demonstration of how stages 190 00:07:30,250 --> 00:07:32,910 ‫and stage variables work, which I think is really 191 00:07:32,910 --> 00:07:34,430 ‫really full, a bit complicated, 192 00:07:34,430 --> 00:07:37,480 ‫I would agree, with the resource permissions, 193 00:07:37,480 --> 00:07:39,360 ‫but something that's necessary to see 194 00:07:39,360 --> 00:07:40,940 ‫before going to the exam. 195 00:07:40,940 --> 00:07:43,120 ‫And so now, we have our different stages 196 00:07:43,120 --> 00:07:44,890 ‫and we'll be able to look at the stage 197 00:07:44,890 --> 00:07:47,310 ‫configuration later on in more details. 198 00:07:47,310 --> 00:07:48,530 ‫So, that's it for this lecture. 199 00:07:48,530 --> 00:07:49,363 ‫I hope you liked it. 200 00:07:49,363 --> 00:07:51,250 ‫And I will see you in the next lecture.