1 00:00:00,000 --> 00:00:01,250 So let's get started 2 00:00:01,250 --> 00:00:03,310 with our first API Gateway. 3 00:00:03,310 --> 00:00:07,220 So in API Gateway, I'm going to go into the console 4 00:00:07,220 --> 00:00:09,050 and we are greeted with a new UI, 5 00:00:09,050 --> 00:00:12,030 so it says it is to create, maintain and secure APIs 6 00:00:12,030 --> 00:00:14,710 at any scale, so this is serverless and great. 7 00:00:14,710 --> 00:00:18,070 So we have different choices, we can build an HTTP API 8 00:00:18,070 --> 00:00:20,810 and that works with Lambda and HTP back-ends, 9 00:00:20,810 --> 00:00:22,700 or we can build a WebSocket API 10 00:00:22,700 --> 00:00:24,680 that works with Lambda, HTTP and AWS services. 11 00:00:24,680 --> 00:00:26,080 This is when we have, for example, 12 00:00:26,080 --> 00:00:27,770 something that is real time use cases 13 00:00:27,770 --> 00:00:29,970 such as a chat application or a dashboard, 14 00:00:29,970 --> 00:00:32,560 and then, finally, a REST API 15 00:00:32,560 --> 00:00:36,490 to have more control over the API management capabilities 16 00:00:36,490 --> 00:00:39,740 and REST API private to have this privately within your VPC. 17 00:00:39,740 --> 00:00:41,490 So this may be confusing, all these options 18 00:00:41,490 --> 00:00:44,830 and I will go over them one by one later on in this course. 19 00:00:44,830 --> 00:00:47,300 But, so right now I don't want the HTTP API, 20 00:00:47,300 --> 00:00:49,800 I wanna scroll down and use the REST API. 21 00:00:49,800 --> 00:00:52,730 Okay, so we're going to build one 22 00:00:52,730 --> 00:00:54,360 and I'm going to click on the build. 23 00:00:54,360 --> 00:00:57,990 And here I am greeted with a panel to create your first API. 24 00:00:57,990 --> 00:01:00,810 So I say, okay, I would like a REST API 25 00:01:00,810 --> 00:01:02,890 and instead of using the example API, 26 00:01:02,890 --> 00:01:05,570 we're going to create a new API 27 00:01:05,570 --> 00:01:06,980 and then we have to name our API, 28 00:01:06,980 --> 00:01:10,420 so I'll just call it MyFirstAPI. 29 00:01:10,420 --> 00:01:12,260 And then we choose an endpoint type. 30 00:01:12,260 --> 00:01:14,090 So regional is deployed within this region 31 00:01:14,090 --> 00:01:16,980 and we're not using the CloudFront Edge network, 32 00:01:16,980 --> 00:01:19,600 Edge Optimized, we're using the CloudFront Edge network 33 00:01:19,600 --> 00:01:21,770 and Private, this is within our VPC. 34 00:01:21,770 --> 00:01:24,400 To keep things simple, I'm going to use original endpoints, 35 00:01:24,400 --> 00:01:27,040 we get more information with the I right here. 36 00:01:27,040 --> 00:01:28,710 Okay, let's create our API. 37 00:01:28,710 --> 00:01:31,110 And now our first API is created. 38 00:01:31,110 --> 00:01:32,850 So there's lots of menus that have just appeared 39 00:01:32,850 --> 00:01:35,410 and, if needed, we'll go over them one by one. 40 00:01:35,410 --> 00:01:37,670 But so right now I'm going to create my first API. 41 00:01:37,670 --> 00:01:40,650 And so, as such, I'm going to click first on this hint 42 00:01:40,650 --> 00:01:41,530 and hide all the hints. 43 00:01:41,530 --> 00:01:43,370 This way this will not be distracting, 44 00:01:43,370 --> 00:01:44,860 and I'm going to create a method. 45 00:01:44,860 --> 00:01:49,090 So here I click on action and then create method 46 00:01:49,090 --> 00:01:52,060 and the method is going to be a GET. 47 00:01:52,060 --> 00:01:55,380 And then I press validate and we have our first method. 48 00:01:55,380 --> 00:01:58,650 So this is a GET at the root of our resources 49 00:01:58,650 --> 00:02:00,700 and we are having many options. 50 00:02:00,700 --> 00:02:02,760 So the first one is integration type. 51 00:02:02,760 --> 00:02:05,197 And so we can choose the type of integration we want. 52 00:02:05,197 --> 00:02:08,789 We want Lambda functions HTTP, Mock, a service, for example, 53 00:02:08,789 --> 00:02:11,750 it could be in any region and any service. 54 00:02:11,750 --> 00:02:15,940 So we can expose an AWS service as an API or a VPC link. 55 00:02:15,940 --> 00:02:18,640 And this is when we have a VPC link inside of our VBC, 56 00:02:18,640 --> 00:02:20,980 but this is advanced and out of scope for the exam. 57 00:02:20,980 --> 00:02:23,020 So right now the only thing we'll be doing 58 00:02:23,020 --> 00:02:24,810 is a Lambda function. 59 00:02:24,810 --> 00:02:25,920 And then you have the option to use 60 00:02:25,920 --> 00:02:27,510 a Lambda Proxy integration. 61 00:02:27,510 --> 00:02:29,620 And I'm going to say yes, I want the proxy integration 62 00:02:29,620 --> 00:02:31,470 cause I want to show you the full event 63 00:02:31,470 --> 00:02:33,600 coming out of the API gateway. 64 00:02:33,600 --> 00:02:36,240 Then the Lambda region is going to be eu-west-2 65 00:02:36,240 --> 00:02:38,430 and we have to create our Lambda function. 66 00:02:38,430 --> 00:02:41,490 So let's go and create our first Lambda function 67 00:02:41,490 --> 00:02:42,910 for this API Gateway. 68 00:02:42,910 --> 00:02:45,120 So I'm going to create a function 69 00:02:45,120 --> 00:02:50,120 and I'm going to call it Lambda API Gateway Proxy root GET. 70 00:02:50,870 --> 00:02:52,620 Pretty long name, but at least it is explicit 71 00:02:52,620 --> 00:02:54,160 around what it does. 72 00:02:54,160 --> 00:02:57,280 Then the runtime is going to be Python 3.8 73 00:02:57,280 --> 00:02:59,960 and then I will create a new execution role, 74 00:02:59,960 --> 00:03:01,460 I click on create function. 75 00:03:01,460 --> 00:03:02,340 And while this happens, 76 00:03:02,340 --> 00:03:05,230 I want us to use some code I already created for this. 77 00:03:05,230 --> 00:03:08,110 So in my API Gateway folder, I have my Lambda code 78 00:03:08,110 --> 00:03:09,820 and we've defined a Lambda handler 79 00:03:09,820 --> 00:03:12,000 which is very similar to what we've seen so far. 80 00:03:12,000 --> 00:03:15,170 So we return a body, which is used here, 81 00:03:15,170 --> 00:03:18,110 we return a status code 200 which means okay. 82 00:03:18,110 --> 00:03:19,840 And then some headers that I've added 83 00:03:19,840 --> 00:03:21,270 to say to our web browser 84 00:03:21,270 --> 00:03:24,560 that the content we're getting is of type json. 85 00:03:24,560 --> 00:03:25,510 So, very simple. 86 00:03:25,510 --> 00:03:28,680 I'm going to copy this entire code, go back here, 87 00:03:28,680 --> 00:03:32,650 scroll down and I'm going to paste this code, save it. 88 00:03:32,650 --> 00:03:34,640 And just for the sake of it I'm going to test it. 89 00:03:34,640 --> 00:03:38,070 So I'll create a test event, click on create 90 00:03:38,070 --> 00:03:41,040 and my Lambda function, if I click on test, works. 91 00:03:41,040 --> 00:03:44,890 And I just get the status code 200, the body and headers. 92 00:03:44,890 --> 00:03:48,620 So this is the function we want to use for our API Gateway. 93 00:03:48,620 --> 00:03:52,610 So back in our API Gateway, I'm going to go up and go down 94 00:03:52,610 --> 00:03:55,140 and I can select the Lambda function that I want 95 00:03:55,140 --> 00:03:57,370 and I can use the Lambda proxy integration, 96 00:03:57,370 --> 00:03:59,900 so again, make sure to tick that box. 97 00:03:59,900 --> 00:04:02,960 Then run the timeouts so the Lambda has a timeout 98 00:04:02,960 --> 00:04:05,460 of up to 15 minutes, but the API Gateway 99 00:04:05,460 --> 00:04:08,900 times out automatically after 29 seconds. 100 00:04:08,900 --> 00:04:10,880 So I can disable this and use a custom timeout, 101 00:04:10,880 --> 00:04:13,790 but the maximum timeout you can get is 29 seconds 102 00:04:13,790 --> 00:04:16,160 and I will use the default timeout for now. 103 00:04:16,160 --> 00:04:17,500 Let's click on save. 104 00:04:17,500 --> 00:04:19,459 And now we're about to give API Gateway 105 00:04:19,459 --> 00:04:21,959 permission to invoke our Lambda function right here. 106 00:04:21,959 --> 00:04:23,600 So yes, I'm happy with this 107 00:04:24,830 --> 00:04:26,853 and our first method is done. 108 00:04:28,150 --> 00:04:30,180 So how do we know that our API gateway 109 00:04:30,180 --> 00:04:31,830 can invoke our Lambda function? 110 00:04:31,830 --> 00:04:33,540 Well, we can test it, we're going to do this in a second. 111 00:04:33,540 --> 00:04:37,400 But before I'm going to go into Lambda, go to permissions 112 00:04:37,400 --> 00:04:40,260 and scroll down and in here have a resource-based policy 113 00:04:40,260 --> 00:04:43,810 on my Lambda function, which authorizes the API Gateway 114 00:04:43,810 --> 00:04:46,820 for my API right here to invoke my Lambda function. 115 00:04:46,820 --> 00:04:49,150 So this is how my API gateway is authorized 116 00:04:49,150 --> 00:04:51,130 to access my Lambda function. 117 00:04:51,130 --> 00:04:52,120 Back in the API Gateway, 118 00:04:52,120 --> 00:04:54,710 I can look at this method execution. 119 00:04:54,710 --> 00:04:57,040 So it will create a method request. 120 00:04:57,040 --> 00:05:00,030 Then there is an integration request, which is Lambda proxy, 121 00:05:00,030 --> 00:05:02,430 then it will be passed onto my Lambda function. 122 00:05:02,430 --> 00:05:04,260 There is no integration response because 123 00:05:04,260 --> 00:05:07,260 we cannot transform request and response in proxy mode, 124 00:05:07,260 --> 00:05:08,700 we'll keep it simple. 125 00:05:08,700 --> 00:05:10,240 And then, finally, the method response 126 00:05:10,240 --> 00:05:11,830 sends back to the client. 127 00:05:11,830 --> 00:05:12,980 So let's test it out. 128 00:05:12,980 --> 00:05:14,340 We're going to test 129 00:05:14,340 --> 00:05:16,860 and I'm going to click on the test button 130 00:05:16,860 --> 00:05:18,300 and we get the response from Lambda. 131 00:05:18,300 --> 00:05:20,630 It says hello from Lambda. 132 00:05:20,630 --> 00:05:23,300 So the really cool thing is that our Lambda function here, 133 00:05:23,300 --> 00:05:25,420 if we go back to the code, 134 00:05:25,420 --> 00:05:28,410 was defining a full json documents 135 00:05:28,410 --> 00:05:31,840 that had a status code, a body, and some headers. 136 00:05:31,840 --> 00:05:36,040 And in our API Gateway, the response body was directly taken 137 00:05:36,040 --> 00:05:40,880 from the body here and the headers are ending up 138 00:05:40,880 --> 00:05:42,520 in the response headers, 139 00:05:42,520 --> 00:05:45,860 so we can see the content type is application json. 140 00:05:45,860 --> 00:05:49,360 On top of things, we can see the logs on the API Gateway, 141 00:05:49,360 --> 00:05:50,850 which I think is pretty cool. 142 00:05:50,850 --> 00:05:52,490 So the last thing I want to show you is 143 00:05:52,490 --> 00:05:55,550 we're going to log this request from the API Gateway. 144 00:05:55,550 --> 00:05:58,810 So I'm going to change my code and print the event 145 00:05:58,810 --> 00:06:00,100 to show you what the event is. 146 00:06:00,100 --> 00:06:02,940 So I'm going to save this and then I'm going to go back 147 00:06:02,940 --> 00:06:05,200 to my API Gateway and do another test. 148 00:06:05,200 --> 00:06:07,780 So I'm going to click on tests, and this test has worked. 149 00:06:07,780 --> 00:06:09,890 So now I'm back in my Lambda function, 150 00:06:09,890 --> 00:06:11,400 I'm going to go to monitoring 151 00:06:11,400 --> 00:06:13,203 and view the logs in CloudWatch. 152 00:06:14,120 --> 00:06:16,450 So I'm in my CloudWatch logs 153 00:06:16,450 --> 00:06:18,520 and I have a second log stream that was just created. 154 00:06:18,520 --> 00:06:20,020 I'm going to click on this 155 00:06:20,020 --> 00:06:22,030 and here is the logging statements. 156 00:06:22,030 --> 00:06:24,840 So this is the entire request we receive 157 00:06:24,840 --> 00:06:27,670 from the API Gateway and so we get lot of information. 158 00:06:27,670 --> 00:06:30,610 The resource, the path, the HDP method, 159 00:06:30,610 --> 00:06:33,430 which is a GET, some headers and so on. 160 00:06:33,430 --> 00:06:35,730 So, lot of information that can be used 161 00:06:35,730 --> 00:06:38,790 by our Lambda function to generate the correct response. 162 00:06:38,790 --> 00:06:41,950 So, super simple, this is great and this has worked. 163 00:06:41,950 --> 00:06:44,480 But now let's go ahead and create a second method. 164 00:06:44,480 --> 00:06:48,070 So let me click on actions and then create resource, 165 00:06:48,070 --> 00:06:50,500 And I'm going to call this houses. 166 00:06:50,500 --> 00:06:52,610 And the resource path is slash houses. 167 00:06:52,610 --> 00:06:54,710 Then I will click on create resource. 168 00:06:54,710 --> 00:06:57,030 So this has created slash houses path and in it, 169 00:06:57,030 --> 00:06:59,740 I can create a method which is, again going to be GET, 170 00:06:59,740 --> 00:07:02,360 but we can use any HTTP verb we wanted 171 00:07:02,360 --> 00:07:04,460 for any other type of queries, for example, 172 00:07:04,460 --> 00:07:06,780 post, if you want to post data. 173 00:07:06,780 --> 00:07:09,340 So we'll use a GET one more time. 174 00:07:09,340 --> 00:07:11,450 And then in here I have an integration type 175 00:07:11,450 --> 00:07:12,924 with the Lambda function. 176 00:07:12,924 --> 00:07:14,710 So I have to create a new Lambda function. 177 00:07:14,710 --> 00:07:17,340 So let me go back to my functions lists 178 00:07:17,340 --> 00:07:20,050 and I'm going to copy this to type it faster. 179 00:07:20,050 --> 00:07:21,640 I'm going to create a new Lambda function 180 00:07:21,640 --> 00:07:23,170 to handle that request. 181 00:07:23,170 --> 00:07:28,170 So the function name is Lambda API gateway proxy houses GET. 182 00:07:28,980 --> 00:07:32,250 The runtime is Python 3.8 and then we select 183 00:07:32,250 --> 00:07:35,330 a new execution role, so we create this function. 184 00:07:35,330 --> 00:07:37,780 And again, we'll copy this function code. 185 00:07:37,780 --> 00:07:42,270 So here I copy this code, go back to my console, 186 00:07:42,270 --> 00:07:44,840 scroll down, paste the code, 187 00:07:44,840 --> 00:07:46,130 and instead of hello from Lambda, 188 00:07:46,130 --> 00:07:51,130 I'm going to say hello from my pretty houses. 189 00:07:51,860 --> 00:07:54,410 Okay, just something that relates to houses 190 00:07:54,410 --> 00:07:55,660 to change the message. 191 00:07:55,660 --> 00:07:56,970 We'll click on save and then 192 00:07:56,970 --> 00:07:59,740 we'll go ahead and refresh this page. 193 00:07:59,740 --> 00:08:00,950 So I have a Lambda function, 194 00:08:00,950 --> 00:08:05,400 it's a proxy integration and this is the houses function. 195 00:08:05,400 --> 00:08:06,750 So here we go, 196 00:08:06,750 --> 00:08:08,510 and I click on save. 197 00:08:08,510 --> 00:08:10,610 Okay, and now my API Gateway 198 00:08:10,610 --> 00:08:13,470 has the capability to invoke this function. 199 00:08:13,470 --> 00:08:16,230 We can test it and test it, 200 00:08:16,230 --> 00:08:18,400 and we get hello from my pretty houses. 201 00:08:18,400 --> 00:08:19,233 So, really cool. 202 00:08:19,233 --> 00:08:21,870 This has working, so we have two routes, 203 00:08:21,870 --> 00:08:24,630 the slash at the root and the slash houses 204 00:08:24,630 --> 00:08:27,800 and they're testable from this UI, 205 00:08:27,800 --> 00:08:31,220 but we want to have access to them using HTTP endpoints. 206 00:08:31,220 --> 00:08:33,830 So for this, very simple, action, 207 00:08:33,830 --> 00:08:36,110 and then we're going to deploy this API. 208 00:08:36,110 --> 00:08:39,289 So we'll deploy this API and I'm going to create a new stage 209 00:08:39,289 --> 00:08:42,250 and I'm going to develop a stage, dev for development. 210 00:08:42,250 --> 00:08:43,082 Very simple. 211 00:08:43,082 --> 00:08:44,780 We'll go into stages later on 212 00:08:44,780 --> 00:08:47,850 and we'll deploy this stage API. 213 00:08:47,850 --> 00:08:49,130 And so here we go. 214 00:08:49,130 --> 00:08:52,130 We have an invoke URL now we can use. 215 00:08:52,130 --> 00:08:54,810 And so if I click on this URL and open it, 216 00:08:54,810 --> 00:08:58,040 as we can see, it says hello from Lambda 217 00:08:58,040 --> 00:09:03,040 and if I go and add slash houses at the end, 218 00:09:03,200 --> 00:09:05,730 I get hello from my pretty houses. 219 00:09:05,730 --> 00:09:08,760 And if I just have something wrong such as wrong, 220 00:09:08,760 --> 00:09:11,590 then I'm gonna get an error message in some way. 221 00:09:11,590 --> 00:09:12,480 So it's really cool. 222 00:09:12,480 --> 00:09:15,300 We have the root, again, hello from Lambda, 223 00:09:15,300 --> 00:09:18,780 and we have created a sub resource underneath 224 00:09:18,780 --> 00:09:20,880 called houses, hello from my pretty houses. 225 00:09:20,880 --> 00:09:23,500 And this is our first overview into the API Gateway, 226 00:09:23,500 --> 00:09:25,580 but very simply, we've deployed an API 227 00:09:25,580 --> 00:09:28,810 backed by two Lambda function on an HTTPS endpoint. 228 00:09:28,810 --> 00:09:30,270 Very cool, I hope you liked it 229 00:09:30,270 --> 00:09:32,220 and I will see you in the next lecture.