‫So now let's talk about the different ways
‫we can integrate our API Gateway with our backend.
‫And the first type is Integration Type MOCK
‫which is just to return a response
‫without even sending a request to the backend.
‫So this is useful when you are programming your API Gateway
‫and configuring it
‫and you don't want to have any work on the backend just yet.
‫So as you can expect, this is not something for production.
‫This is something for just development and testing.
‫Then we have the HTTP,
‫or AWS Lambda and other services
‫in which the API Gateway will forward our request,
‫but we can modify it.
‫So we must both configure
‫what's called an integration request
‫and an integration response.
‫And when you set that up,
‫we can set up data mappings using mapping templates
‫for the request and the response.
‫Well, that means that we will be able to change
‫the request made to the backend
‫and to change the response back from the backend
‫before sending it to our clients.
‫So this is really helpful.
‫For example, if we create a REST API and our API Gateway
‫and we add mapping templates
‫and then we're going to map this REST API call
‫to an API call on the SQS Queue
‫by changing, renaming, reordering, all these kind of things
‫such as the SQS Queue can understand the API call
‫made by the API Gateway.
‫So here, the API Gateway has the power to change the request
‫and the response.
‫Okay, this is not the things we've seen.
‫We've seen so far the one integration called AWS Proxy,
‫which means Lambda Proxy,
‫in which the request from the client
‫is going to be the input to the Lambda.
‫And we haven't modified any request or response
‫because we cannot, it's a proxy.
‫And so the function is solely responsible for
‫the logic of the request and the response.
‫That means that we cannot use any mapping templates,
‫we cannot change the headers, the query string parameters.
‫All these things will be passed as arguments
‫to our function directly.
‫And this is what we've seen.
‫So when we logged in the Lambda,
‫our request coming from the API Gateway,
‫so we logged the event,
‫we could see adjacent document that looked just like this.
‫So we have the information about the resource,
‫the path, the HTTP method, the headers,
‫the query string parameters and so on,
‫as well as stage variables,
‫the body and all these other things.
‫So this is very helpful.
‫And then our Lambda function is responsible
‫to take this request, understand what to do with it
‫and then the Lambda function can return a response.
‫And we've framed a response like this as well.
‫So we specify the status code,
‫we specify the headers, as well as the body.
‫So here in this Proxy Integration,
‫all the work is on the backend
‫and the API Gateway is just here to proxy requests through.
‫So there's a last integration type called HTTP Proxy.
‫In which case, again because this is a proxy,
‫we don't have any mapping templates
‫and the request itself is passed directly,
‫so proxy to the backend.
‫And then when the backend replies,
‫then the response is again proxied by the API
‫to going back into the clients.
‫And if you wanted to, you can add HTTP headers
‫if you need be.
‫So for example, you can add an API key
‫between your API Gateway and your backhand,
‫but your users don't see it.
‫So let's take an example,
‫a client makes an HTTP request into your API Gateway.
‫You're going to proxy that request to your backend,
‫for example an Application Load Balancer.
‫But again, as I said,
‫you can add optional HTTP headers, such as an API key,
‫that really guarantees that your API call is going to work.
‫So the client does not need to know
‫about the secret API key,
‫but yet your backend will see it coming
‫from the API Gateway.
‫So mapping templates to modify the request and the response,
‫they are only applicable if we integrate
‫within AWS or HTTP,
‫but without using the proxy methods.
‫So they can be used to modify request and response.
‫And then with it,
‫we are able to rename or modify query string parameters,
‫to modify the body content, to add or modify headers.
‫And to do this, it uses something called the VTL.
‫So the Velocity Template Language
‫which allows us to get for loops, if's, etc.
‫It's a scripting language used to modify some requests.
‫And finally, when we receive response
‫it is possible for us to remove some results
‫out of the response
‫by again using the mapping templates.
‫Finally, to set the mapping templates,
‫you must set the Content-Type to be application/json
‫or application/xml.
‫So a concrete application
‫of a mapping template that can come up in the exam
‫is how to integrate a client with a SOAP API
‫through an API Gateway.
‫So what is a SOAP API?
‫SOAP APIs are XML based
‫and we know REST APIs they are JSON based.
‫And so we need to have something in between
‫to convert the JSON into XML and back.
‫And so the idea is that you would create your API Gateway
‫with a mapping template
‫and the client would interact with JSON in your API Gateway.
‫But your API Gateway, thanks to the mapping template,
‫can transform this payload into an XML payload,
‫into the SOAP API and back.
‫And so in this case,
‫the API Gateway should extract the data from the request,
‫either the path, the payload, or the header,
‫then build the SOAP message
‫necessary for the backend to understand,
‫so this is where we use the mapping template,
‫then call the SOAP service and receive the XML response
‫and finally transform the XML response
‫back into a desired format to respond to your users.
‫Here is another example where we use the mapping template
‫to use query string parameters.
‫So our client is integrated with API Gateway
‫which is using Lambda directly, not as a proxy.
‫And so we want to be able to map
‫the query string parameters,
‫so if we send a request like this
‫with a question mark, name equals foo and other equal bar,
‫we want to be able to rename these query string parameters
‫before passing into the Lambda
‫so we can create a mapping template
‫and then the JSON that gets passed
‫into the Lambda function could be my variable foo
‫and other variable bar.
‫And so in this example
‫what I've done is that I've renamed variables
‫so you can map them to anything you want.
‫And this is again yet another use cases
‫for your mapping templates.
‫So that's it for the theory.
‫Now let's go into the practice to see how they work.