Question

Difficulty: MediumAPI Development and Integration with Amazon API Gateway

A developer is building a serverless photo-sharing application that allows users to upload JPEG images. The application uses an Amazon API Gateway REST API with an AWS Lambda proxy integration to process the uploads. During testing, the developer discovers that the image files processed by the Lambda function are corrupted because the binary payload is being treated as UTF-8 string data instead of binary data. Which configuration steps must the developer take to resolve this issue?

  1. A
    Configure an Integration Request mapping template for image/jpeg in API Gateway to convert the binary stream to a JSON string before invoking the Lambda function.
  2. B
    Set the Integration Request's Content Handling setting to CONVERT_TO_TEXT in the API Gateway console, and read the raw binary stream from the Lambda function's context object.
  3. Add image/jpeg to the Binary Media Types of the API Gateway API, and update the Lambda function to base64-decode the body field from the input event when isBase64Encoded is true.Answer
  4. D
    Configure a custom Lambda Authorizer to parse the incoming image payload and inject the decoded binary buffer into the request context.

Answer

Add the image/jpeg content type to the API Gateway API's Binary Media Types, and update the Lambda function code to base64-decode the event body when the isBase64Encoded flag is true.
For API Gateway to properly pass binary payloads (such as JPEG images) to a Lambda proxy integration, the API must be configured with the appropriate Binary Media Types. When the request Content-Type matches a binary media type, API Gateway base64-encodes the request body and sets the isBase64Encoded boolean flag to true in the event object. The Lambda function must check this flag and decode the body to get the raw binary content.

Step-by-Step Solution

1
Configure API Gateway to recognize image/jpeg as a binary format by adding it to the Binary Media Types list in the API settings.
API Gateway will treat incoming requests with the image/jpeg Content-Type as binary payloads rather than text.
This prevents API Gateway from attempting to parse the binary image data as a UTF-8 string, which causes data corruption.
2
Pass the incoming request via Lambda proxy integration, allowing API Gateway to automatically base64-encode the binary body.
The event object passed to the Lambda function will contain a base64-encoded string in the body field, and the isBase64Encoded flag will be set to true.
Proxy integrations require binary payloads to be base64-encoded to safely transport the data inside the JSON event structure.
3
Modify the Lambda function code to check if isBase64Encoded is true, and if so, base64-decode the body payload.
The Lambda function successfully retrieves the original raw binary JPEG bytes for processing or storage.
The function must decode the base64 string to restore the original binary format of the image.

Key Concept

Handling binary media payloads with API Gateway Lambda Proxy integration
Estimated Time:1m 30s
Rate this question