What is the role of the integration request in an API method in Amazon API gateway?

I want to integrate an Amazon API Gateway REST API with an Application Load Balancer. How can I do that using API Gateway integrations?

Short description

To integrate your API Gateway REST API with a public Application Load Balancer, use API Gateway HTTP integration.

For private Application Load Balancers, use API Gateway VPC link to first connect to a private Network Load Balancer. Then, use the Network Load Balancer to forward API Gateway requests to the private Application Load Balancer.

Note: There's currently no direct API Gateway integration for private Application Load Balancers.

Resolution

Note: The following procedure assumes two things:

  • You have access to a public Application Load Balancer and its DNS name.
  • You have an API Gateway REST API resource with an HTTP method.

1.    In the API Gateway console, choose the API you want to integrate with the Application Load Balancer.

2.    In the Resources pane, for Methods, choose the HTTP method that your API uses.

3.    Choose Integration Request.

4.    In the Integration Request pane, for Integration Type, choose HTTP.

Note: To pass the entire API request and its parameters to the backend Application Load Balancer, create one of the following instead:
An
-or-
An

For more information, see Set up HTTP integrations in API Gateway.

5.    In the Endpoint URL field, enter either the Application Load Balancer's default DNS name or custom DNS name. Then, add the configured protocol of its listener. For example, an Application Load Balancer that's configured with an HTTPS listener on port 8080 requires the following endpoint URL format: https://domain-name:8080/

Important: Make sure that you create an HTTP listener or HTTPS listener for the Application Load Balancer using the port and of your choice. For more information, see Listeners for your Application Load Balancers. For an Application Load Balancer configured with an HTTPS listener, the associated certificate must be issued by an API Gateway-supported certificate authority. If you have to use a certificate that's self-signed or issued by a private certificate authority, then set insecureSkipVerification to true in the integration's tlsConfig.

This section explains how to set up data mappings from an API's method request data, including other data stored in , , or variables, to the corresponding integration request parameters and from an integration response data, including the other data, to the method response parameters. The method request data includes request parameters (path, query string, headers) and the body. The integration response data includes response parameters (headers) and the body. For more information about using the stage variables, see Amazon API Gateway stage variables reference.

Map method request data to integration request parameters

Integration request parameters, in the form of path variables, query strings or headers, can be mapped from any defined method request parameters and the payload.

In the following table, PARAM_NAME is the name of a method request parameter of the given parameter type. It must match the regular expression '^[a-zA-Z0-9._$-]+$]'. It must have been defined before it can be referenced. JSONPath_EXPRESSION is a JSONPath expression for a JSON field of the body of a request or response.

The "$" prefix is omitted in this syntax.

Mapped data sourceMapping expressionMethod request pathmethod.request.path.PARAM_NAMEMethod request query stringmethod.request.querystring.PARAM_NAMEMulti-value method request query stringmethod.request.multivaluequerystring.PARAM_NAMEMethod request header


...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
0Multi-value method request header

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
1Method request body

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
2Method request body (JsonPath)

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
3.Stage variables

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
4Context variables

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
5 that must be one of the .Static value

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
6. The

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
7 is a string literal and must be enclosed within a pair of single quotes.

Example Mappings from method request parameter in OpenAPI

The following example shows an OpenAPI snippet that maps:

  • the method request's header, named

    
    ...
    "requestParameters" : {
                
        "integration.request.header.body-header" : "method.request.body",
        "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
                
    }
    ...
                    
                
    8, into the integration request path parameter, named
    
    ...
    "requestParameters" : {
                
        "integration.request.header.body-header" : "method.request.body",
        "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
                
    }
    ...
                    
                
    9

  • the multi-value method request query string, named

    
    ...
    "responseParameters" : {
                
        "method.response.header.location" : "integration.response.body.redirect.url",
        "method.response.header.id" : "integration.response.header.x-app-id",
        "method.response.header.items" : "integration.response.multivalueheader.item",
                
    }
    ...
                    
                
    0, into the integration request query string, named
    
    ...
    "responseParameters" : {
                
        "method.response.header.location" : "integration.response.body.redirect.url",
        "method.response.header.id" : "integration.response.header.x-app-id",
        "method.response.header.items" : "integration.response.multivalueheader.item",
                
    }
    ...
                    
                
    1


...
"requestParameters" : {
            
    "integration.request.path.integrationPathParam" : "method.request.header.methodRequestHeaderParam",        
    "integration.request.querystring.integrationQueryParam" : "method.request.multivaluequerystring.methodRequestQueryParam"
            
}
...
                
            

Integration request parameters can also be mapped from fields in the JSON request body using a . The following table shows the mapping expressions for a method request body and its JSON fields.

Example Mapping from method request body in OpenAPI

The following example shows an OpenAPI snippet that maps 1) the method request body to the integration request header, named


...
"responseParameters" : {
            
    "method.response.header.location" : "integration.response.body.redirect.url",
    "method.response.header.id" : "integration.response.header.x-app-id",
    "method.response.header.items" : "integration.response.multivalueheader.item",
            
}
...
                
            
2, and 2) a JSON field of the body, as expressed by a JSON expression (

...
"responseParameters" : {
            
    "method.response.header.location" : "integration.response.body.redirect.url",
    "method.response.header.id" : "integration.response.header.x-app-id",
    "method.response.header.items" : "integration.response.multivalueheader.item",
            
}
...
                
            
3, without the

...
"responseParameters" : {
            
    "method.response.header.location" : "integration.response.body.redirect.url",
    "method.response.header.id" : "integration.response.header.x-app-id",
    "method.response.header.items" : "integration.response.multivalueheader.item",
            
}
...
                
            
4 prefix).


...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            

Map integration response data to method response headers

Method response header parameters can be mapped from any integration response header or integration response body,


...
"responseParameters" : {
            
    "method.response.header.location" : "integration.response.body.redirect.url",
    "method.response.header.id" : "integration.response.header.x-app-id",
    "method.response.header.items" : "integration.response.multivalueheader.item",
            
}
...
                
            
5 variables, or static values.

Mapped data sourceMapping expressionIntegration response header


...
"responseParameters" : {
            
    "method.response.header.location" : "integration.response.body.redirect.url",
    "method.response.header.id" : "integration.response.header.x-app-id",
    "method.response.header.items" : "integration.response.multivalueheader.item",
            
}
...
                
            
6Integration response header

...
"responseParameters" : {
            
    "method.response.header.location" : "integration.response.body.redirect.url",
    "method.response.header.id" : "integration.response.header.x-app-id",
    "method.response.header.items" : "integration.response.multivalueheader.item",
            
}
...
                
            
7Integration response body

...
"responseParameters" : {
            
    "method.response.header.location" : "integration.response.body.redirect.url",
    "method.response.header.id" : "integration.response.header.x-app-id",
    "method.response.header.items" : "integration.response.multivalueheader.item",
            
}
...
                
            
8Integration response body (JsonPath)

...
"responseParameters" : {
            
    "method.response.header.location" : "integration.response.body.redirect.url",
    "method.response.header.id" : "integration.response.header.x-app-id",
    "method.response.header.items" : "integration.response.multivalueheader.item",
            
}
...
                
            
9Stage variable

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
4Context variable

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
5 that must be one of the .Static value

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
6. The

...
"requestParameters" : {
            
    "integration.request.header.body-header" : "method.request.body",
    "integration.request.path.pet-name" : "method.request.body.petstore.pets[0].name",
            
}
...
                
            
7 is a string literal and must be enclosed within a pair of single quotes.

Example Data mapping from integration response in OpenAPI

The following example shows an OpenAPI snippet that maps 1) the integration response's PARAM_NAME4, JSONPath field into the request response's PARAM_NAME5 header; and 2) the integration response's PARAM_NAME6 header to the method response's PARAM_NAME7 header.


...
"responseParameters" : {
            
    "method.response.header.location" : "integration.response.body.redirect.url",
    "method.response.header.id" : "integration.response.header.x-app-id",
    "method.response.header.items" : "integration.response.multivalueheader.item",
            
}
...
                
            

Map request and response payloads between method and integration

API Gateway uses Velocity Template Language (VTL)engine to process body for the integration request and integration response. The mapping templates translate method request payloads to the corresponding integration request payloads and translate integration response bodies to the method response bodies.

The VTL templates use JSONPath expressions, other parameters such as calling contexts and stage variables, and utility functions to process the JSON data.

If a model is defined to describe the data structure of a payload, API Gateway can use the model to generate a skeletal mapping template for an integration request or integration response. You can use the skeletal template as an aid to customize and expand the mapping VTL script. However, you can create a mapping template from scratch without defining a model for the payload's data structure.

Select a VTL mapping template

API Gateway uses the following logic to select a mapping template, in Velocity Template Language (VTL), to map the payload from a method request to the corresponding integration request or to map the payload from an integration response to the corresponding method response.

For a request payload, API Gateway uses the request’s PARAM_NAME8 header value as the key to select the mapping template for the request payload. For a response payload, API Gateway uses the incoming request’s PARAM_NAME9 header value as the key to select the mapping template.

When the PARAM_NAME8 header is absent in the request, API Gateway assumes that its default value is '^[a-zA-Z0-9._$-]+$]'1. For such a request, API Gateway uses '^[a-zA-Z0-9._$-]+$]'1 as the default key to select the mapping template, if one is defined. When no template matches this key, API Gateway passes the payload through unmapped if the property is set to '^[a-zA-Z0-9._$-]+$]'3 or '^[a-zA-Z0-9._$-]+$]'4.

When the PARAM_NAME9 header is not specified in the request, API Gateway assumes that its default value is '^[a-zA-Z0-9._$-]+$]'1. In this case, API Gateway selects an existing mapping template for '^[a-zA-Z0-9._$-]+$]'1 to map the response payload. If no template is defined for '^[a-zA-Z0-9._$-]+$]'1, API Gateway selects the first existing template and uses it as the default to map the response payload. Similarly, API Gateway uses the first existing template when the specified PARAM_NAME9 header value does not match any existing template key. If no template is defined, API Gateway simply passes the response payload through unmapped.

For example, suppose that an API has a '^[a-zA-Z0-9._$-]+$]'1 template defined for a request payload and has a JSONPath_EXPRESSION1 template defined for the response payload. If the client sets the JSONPath_EXPRESSION2, and JSONPath_EXPRESSION3 headers in the request, both the request and response payloads will be processed with the corresponding mapping templates. If the JSONPath_EXPRESSION4 header is absent, the JSONPath_EXPRESSION1 mapping template will be used to map the response payload. To return the response payload unmapped instead, you must set up an empty template for '^[a-zA-Z0-9._$-]+$]'1.

Only the MIME type is used from the PARAM_NAME9 and PARAM_NAME8 headers when selecting a mapping template. For example, a header of JSONPath_EXPRESSION9 will have a request template with the '^[a-zA-Z0-9._$-]+$]'1 key selected.

What is integration response in API gateway?

An integration response is an HTTP response encapsulating the backend response. For an HTTP endpoint, the backend response is an HTTP response. The integration response status code can take the backend-returned status code, and the integration response body is the backend-returned payload.

What is API integration in Amazon?

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. APIs act as the "front door" for applications to access data, business logic, or functionality from your backend services.

What is an integration request Lambda?

The request parameters include headers, URL path variables, query string parameters, and the applicable payload. The integrated Lambda function verifies all of the input sources before processing the request and responding to the client with meaningful error messages if any of the required input is missing.

Which integration type would you use to get a canned response from an API gateway resource without using any other service?

MOCK : This type of integration lets API Gateway return a response without sending the request further to the backend.