1 00:00:00,540 --> 00:00:02,340 ‫So now let's talk about the different ways 2 00:00:02,340 --> 00:00:05,040 ‫we can integrate our API Gateway with our backend. 3 00:00:05,040 --> 00:00:08,130 ‫And the first type is Integration Type MOCK 4 00:00:08,130 --> 00:00:09,600 ‫which is just to return a response 5 00:00:09,600 --> 00:00:12,060 ‫without even sending a request to the backend. 6 00:00:12,060 --> 00:00:15,210 ‫So this is useful when you are programming your API Gateway 7 00:00:15,210 --> 00:00:16,320 ‫and configuring it 8 00:00:16,320 --> 00:00:19,560 ‫and you don't want to have any work on the backend just yet. 9 00:00:19,560 --> 00:00:21,630 ‫So as you can expect, this is not something for production. 10 00:00:21,630 --> 00:00:24,480 ‫This is something for just development and testing. 11 00:00:24,480 --> 00:00:26,280 ‫Then we have the HTTP, 12 00:00:26,280 --> 00:00:28,890 ‫or AWS Lambda and other services 13 00:00:28,890 --> 00:00:32,670 ‫in which the API Gateway will forward our request, 14 00:00:32,670 --> 00:00:34,260 ‫but we can modify it. 15 00:00:34,260 --> 00:00:35,640 ‫So we must both configure 16 00:00:35,640 --> 00:00:37,350 ‫what's called an integration request 17 00:00:37,350 --> 00:00:39,090 ‫and an integration response. 18 00:00:39,090 --> 00:00:40,350 ‫And when you set that up, 19 00:00:40,350 --> 00:00:42,930 ‫we can set up data mappings using mapping templates 20 00:00:42,930 --> 00:00:44,490 ‫for the request and the response. 21 00:00:44,490 --> 00:00:47,220 ‫Well, that means that we will be able to change 22 00:00:47,220 --> 00:00:49,530 ‫the request made to the backend 23 00:00:49,530 --> 00:00:52,080 ‫and to change the response back from the backend 24 00:00:52,080 --> 00:00:53,880 ‫before sending it to our clients. 25 00:00:53,880 --> 00:00:54,780 ‫So this is really helpful. 26 00:00:54,780 --> 00:00:58,200 ‫For example, if we create a REST API and our API Gateway 27 00:00:58,200 --> 00:00:59,580 ‫and we add mapping templates 28 00:00:59,580 --> 00:01:02,310 ‫and then we're going to map this REST API call 29 00:01:02,310 --> 00:01:04,440 ‫to an API call on the SQS Queue 30 00:01:04,440 --> 00:01:07,980 ‫by changing, renaming, reordering, all these kind of things 31 00:01:07,980 --> 00:01:10,350 ‫such as the SQS Queue can understand the API call 32 00:01:10,350 --> 00:01:12,270 ‫made by the API Gateway. 33 00:01:12,270 --> 00:01:15,150 ‫So here, the API Gateway has the power to change the request 34 00:01:15,150 --> 00:01:16,290 ‫and the response. 35 00:01:16,290 --> 00:01:17,910 ‫Okay, this is not the things we've seen. 36 00:01:17,910 --> 00:01:21,390 ‫We've seen so far the one integration called AWS Proxy, 37 00:01:21,390 --> 00:01:23,100 ‫which means Lambda Proxy, 38 00:01:23,100 --> 00:01:24,690 ‫in which the request from the client 39 00:01:24,690 --> 00:01:26,940 ‫is going to be the input to the Lambda. 40 00:01:26,940 --> 00:01:29,220 ‫And we haven't modified any request or response 41 00:01:29,220 --> 00:01:31,080 ‫because we cannot, it's a proxy. 42 00:01:31,080 --> 00:01:33,510 ‫And so the function is solely responsible for 43 00:01:33,510 --> 00:01:35,820 ‫the logic of the request and the response. 44 00:01:35,820 --> 00:01:38,400 ‫That means that we cannot use any mapping templates, 45 00:01:38,400 --> 00:01:40,740 ‫we cannot change the headers, the query string parameters. 46 00:01:40,740 --> 00:01:42,720 ‫All these things will be passed as arguments 47 00:01:42,720 --> 00:01:44,100 ‫to our function directly. 48 00:01:44,100 --> 00:01:45,210 ‫And this is what we've seen. 49 00:01:45,210 --> 00:01:48,120 ‫So when we logged in the Lambda, 50 00:01:48,120 --> 00:01:52,140 ‫our request coming from the API Gateway, 51 00:01:52,140 --> 00:01:53,550 ‫so we logged the event, 52 00:01:53,550 --> 00:01:56,160 ‫we could see adjacent document that looked just like this. 53 00:01:56,160 --> 00:01:57,720 ‫So we have the information about the resource, 54 00:01:57,720 --> 00:02:00,000 ‫the path, the HTTP method, the headers, 55 00:02:00,000 --> 00:02:01,800 ‫the query string parameters and so on, 56 00:02:01,800 --> 00:02:02,670 ‫as well as stage variables, 57 00:02:02,670 --> 00:02:04,740 ‫the body and all these other things. 58 00:02:04,740 --> 00:02:06,120 ‫So this is very helpful. 59 00:02:06,120 --> 00:02:08,250 ‫And then our Lambda function is responsible 60 00:02:08,250 --> 00:02:10,890 ‫to take this request, understand what to do with it 61 00:02:10,890 --> 00:02:14,250 ‫and then the Lambda function can return a response. 62 00:02:14,250 --> 00:02:16,590 ‫And we've framed a response like this as well. 63 00:02:16,590 --> 00:02:18,450 ‫So we specify the status code, 64 00:02:18,450 --> 00:02:21,390 ‫we specify the headers, as well as the body. 65 00:02:21,390 --> 00:02:23,613 ‫So here in this Proxy Integration, 66 00:02:23,613 --> 00:02:25,830 ‫all the work is on the backend 67 00:02:25,830 --> 00:02:29,250 ‫and the API Gateway is just here to proxy requests through. 68 00:02:29,250 --> 00:02:32,640 ‫So there's a last integration type called HTTP Proxy. 69 00:02:32,640 --> 00:02:34,560 ‫In which case, again because this is a proxy, 70 00:02:34,560 --> 00:02:36,690 ‫we don't have any mapping templates 71 00:02:36,690 --> 00:02:39,240 ‫and the request itself is passed directly, 72 00:02:39,240 --> 00:02:40,860 ‫so proxy to the backend. 73 00:02:40,860 --> 00:02:42,750 ‫And then when the backend replies, 74 00:02:42,750 --> 00:02:45,712 ‫then the response is again proxied by the API 75 00:02:45,712 --> 00:02:47,310 ‫to going back into the clients. 76 00:02:47,310 --> 00:02:50,010 ‫And if you wanted to, you can add HTTP headers 77 00:02:50,010 --> 00:02:50,843 ‫if you need be. 78 00:02:50,843 --> 00:02:53,070 ‫So for example, you can add an API key 79 00:02:53,070 --> 00:02:55,170 ‫between your API Gateway and your backhand, 80 00:02:55,170 --> 00:02:56,940 ‫but your users don't see it. 81 00:02:56,940 --> 00:02:57,990 ‫So let's take an example, 82 00:02:57,990 --> 00:03:01,440 ‫a client makes an HTTP request into your API Gateway. 83 00:03:01,440 --> 00:03:04,560 ‫You're going to proxy that request to your backend, 84 00:03:04,560 --> 00:03:06,870 ‫for example an Application Load Balancer. 85 00:03:06,870 --> 00:03:07,710 ‫But again, as I said, 86 00:03:07,710 --> 00:03:11,102 ‫you can add optional HTTP headers, such as an API key, 87 00:03:11,102 --> 00:03:15,240 ‫that really guarantees that your API call is going to work. 88 00:03:15,240 --> 00:03:16,650 ‫So the client does not need to know 89 00:03:16,650 --> 00:03:18,480 ‫about the secret API key, 90 00:03:18,480 --> 00:03:20,910 ‫but yet your backend will see it coming 91 00:03:20,910 --> 00:03:22,473 ‫from the API Gateway. 92 00:03:23,610 --> 00:03:27,540 ‫So mapping templates to modify the request and the response, 93 00:03:27,540 --> 00:03:29,760 ‫they are only applicable if we integrate 94 00:03:29,760 --> 00:03:32,280 ‫within AWS or HTTP, 95 00:03:32,280 --> 00:03:34,500 ‫but without using the proxy methods. 96 00:03:34,500 --> 00:03:37,080 ‫So they can be used to modify request and response. 97 00:03:37,080 --> 00:03:37,913 ‫And then with it, 98 00:03:37,913 --> 00:03:41,280 ‫we are able to rename or modify query string parameters, 99 00:03:41,280 --> 00:03:44,400 ‫to modify the body content, to add or modify headers. 100 00:03:44,400 --> 00:03:46,590 ‫And to do this, it uses something called the VTL. 101 00:03:46,590 --> 00:03:48,750 ‫So the Velocity Template Language 102 00:03:48,750 --> 00:03:51,240 ‫which allows us to get for loops, if's, etc. 103 00:03:51,240 --> 00:03:54,810 ‫It's a scripting language used to modify some requests. 104 00:03:54,810 --> 00:03:56,760 ‫And finally, when we receive response 105 00:03:56,760 --> 00:03:58,740 ‫it is possible for us to remove some results 106 00:03:58,740 --> 00:03:59,790 ‫out of the response 107 00:03:59,790 --> 00:04:02,160 ‫by again using the mapping templates. 108 00:04:02,160 --> 00:04:04,830 ‫Finally, to set the mapping templates, 109 00:04:04,830 --> 00:04:08,850 ‫you must set the Content-Type to be application/json 110 00:04:08,850 --> 00:04:11,670 ‫or application/xml. 111 00:04:11,670 --> 00:04:13,162 ‫So a concrete application 112 00:04:13,162 --> 00:04:15,960 ‫of a mapping template that can come up in the exam 113 00:04:15,960 --> 00:04:19,564 ‫is how to integrate a client with a SOAP API 114 00:04:19,564 --> 00:04:21,630 ‫through an API Gateway. 115 00:04:21,630 --> 00:04:23,160 ‫So what is a SOAP API? 116 00:04:23,160 --> 00:04:25,200 ‫SOAP APIs are XML based 117 00:04:25,200 --> 00:04:27,540 ‫and we know REST APIs they are JSON based. 118 00:04:27,540 --> 00:04:29,340 ‫And so we need to have something in between 119 00:04:29,340 --> 00:04:32,040 ‫to convert the JSON into XML and back. 120 00:04:32,040 --> 00:04:34,590 ‫And so the idea is that you would create your API Gateway 121 00:04:34,590 --> 00:04:36,120 ‫with a mapping template 122 00:04:36,120 --> 00:04:39,240 ‫and the client would interact with JSON in your API Gateway. 123 00:04:39,240 --> 00:04:41,700 ‫But your API Gateway, thanks to the mapping template, 124 00:04:41,700 --> 00:04:44,400 ‫can transform this payload into an XML payload, 125 00:04:44,400 --> 00:04:46,560 ‫into the SOAP API and back. 126 00:04:46,560 --> 00:04:47,393 ‫And so in this case, 127 00:04:47,393 --> 00:04:49,830 ‫the API Gateway should extract the data from the request, 128 00:04:49,830 --> 00:04:51,960 ‫either the path, the payload, or the header, 129 00:04:51,960 --> 00:04:53,880 ‫then build the SOAP message 130 00:04:53,880 --> 00:04:55,920 ‫necessary for the backend to understand, 131 00:04:55,920 --> 00:04:58,350 ‫so this is where we use the mapping template, 132 00:04:58,350 --> 00:05:01,830 ‫then call the SOAP service and receive the XML response 133 00:05:01,830 --> 00:05:03,870 ‫and finally transform the XML response 134 00:05:03,870 --> 00:05:07,443 ‫back into a desired format to respond to your users. 135 00:05:08,700 --> 00:05:11,670 ‫Here is another example where we use the mapping template 136 00:05:11,670 --> 00:05:13,680 ‫to use query string parameters. 137 00:05:13,680 --> 00:05:16,320 ‫So our client is integrated with API Gateway 138 00:05:16,320 --> 00:05:19,710 ‫which is using Lambda directly, not as a proxy. 139 00:05:19,710 --> 00:05:21,570 ‫And so we want to be able to map 140 00:05:21,570 --> 00:05:22,650 ‫the query string parameters, 141 00:05:22,650 --> 00:05:24,600 ‫so if we send a request like this 142 00:05:24,600 --> 00:05:27,900 ‫with a question mark, name equals foo and other equal bar, 143 00:05:27,900 --> 00:05:30,450 ‫we want to be able to rename these query string parameters 144 00:05:30,450 --> 00:05:32,100 ‫before passing into the Lambda 145 00:05:32,100 --> 00:05:33,720 ‫so we can create a mapping template 146 00:05:33,720 --> 00:05:35,340 ‫and then the JSON that gets passed 147 00:05:35,340 --> 00:05:38,640 ‫into the Lambda function could be my variable foo 148 00:05:38,640 --> 00:05:40,650 ‫and other variable bar. 149 00:05:40,650 --> 00:05:41,580 ‫And so in this example 150 00:05:41,580 --> 00:05:43,470 ‫what I've done is that I've renamed variables 151 00:05:43,470 --> 00:05:45,480 ‫so you can map them to anything you want. 152 00:05:45,480 --> 00:05:48,030 ‫And this is again yet another use cases 153 00:05:48,030 --> 00:05:49,560 ‫for your mapping templates. 154 00:05:49,560 --> 00:05:51,150 ‫So that's it for the theory. 155 00:05:51,150 --> 00:05:53,573 ‫Now let's go into the practice to see how they work.