Question

Difficulty: EasyAPI Development and Integration with Amazon API Gateway

A developer is configuring an Amazon API Gateway REST API with a Lambda proxy integration. The backend AWS Lambda function needs to return a custom HTTP status code of 201 (Created), a custom header, and a JSON payload to the client. How must the developer structure the response returned by the Lambda function?

  1. A
    Configure integration response mapping templates in API Gateway to extract the custom header and HTTP status code from the Lambda function's default output properties.
  2. B
    Use a custom Lambda authorizer to modify the response headers and HTTP status code before the API Gateway forwards the payload to the client.
  3. Return a JSON object from the Lambda function containing statusCode, headers, and body fields, with the body field stringified.Answer
  4. D
    Return the raw application JSON payload directly, because API Gateway automatically maps the payload and defaults to a 201 status code for successful Lambda executions.

Answer

Return a JSON object from the Lambda function containing statusCode, headers, and body fields, with the body field stringified.
In a Lambda proxy integration, the backend Lambda function is fully responsible for determining the structure of the HTTP response. To return custom status codes, headers, and payloads, the developer must return a JSON object with the keys 'statusCode', 'headers', and 'body' (where the body payload must be serialized to a string). API Gateway parses this object to construct the final HTTP response sent back to the client.

Step-by-Step Solution

1
Determine the integration type configuration.
Identify that the REST API is configured with a Lambda proxy integration.
The integration type determines whether API Gateway or the backend Lambda function is responsible for formatting the HTTP response.
2
Identify the response requirements for Lambda proxy integration.
Recognize that API Gateway expects the Lambda function to return a specific JSON schema consisting of 'statusCode', 'headers', and 'body'.
Under proxy integration, API Gateway passes the raw output directly from the Lambda function to the client after parsing these top-level fields.
3
Format the payload and metadata in the Lambda code.
Build the response object where 'statusCode' is set to 201, the custom header is included inside 'headers', and the JSON payload is serialized into a string inside 'body'.
The body must be stringified because API Gateway expects the value of the 'body' property to be a string.

Key Concept

Lambda Proxy Integration Response Format
Estimated Time:1m 0s
Rate this question