AWS Certified Solutions Architect - Professional: API Gateway and Lambda Integration for Error Mapping

API Gateway and Lambda Integration for Error Mapping

Prev Question Next Question

Question

API gateway and Lambda proxy integrations have been chosen to implement an application by a software engineer.

The application is a data analysis tool that returns some statistical results when the HTTP endpoint is called.

The Lambda needs to communicate with some back-end data services such as Keen.io.

However, there are chances that error happens, such as wrong data requested, bad communications, etc.

The Lambda is written using Java.

Two exceptions may be returned which are BadRequestException and InternalErrorException.

What should the software engineer do to map these two exceptions in the API gateway with proper HTTP return codes? For example, BadRequestException and InternalErrorException are mapped to HTTP return codes 400 and 500 respectively.

Select 2.

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E.

Correct Answer - B, D.

When an API gateway is established, there are four parts.

Method Request/Method Response mainly deals with API gateways.

They are the API's interface with the API's frontend (a client), whereas Integration Request and Integration Response are the API's interface with the backend.

In this case, the backend is a Lambda.

For the mapping of exceptions that come from Lambda, Integration Response is the correct place to configure.

However, the corresponding error code (400) on the method response should be created first.

Otherwise, API Gateway throws an invalid configuration error response at runtime.

Below is an example of mapping BadRequestException to HTTP return code 400:

Option A is incorrect: Because HTTP error codes are defined as firstly in Method Response instead of Integration Response.

Option B is CORRECT: Because HTTP error codes are defined as firstly in Method Response instead of Integration Response.

(Same reason as A).

Option C is incorrect: Because Integration Response in API gateway should be used.

Refer to https://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html on “how to Handle Lambda Errors in API Gateway”.

Option D is CORRECT: Because BadRequest or InternalError should be mapped to 400 and 500 in Integration Response settings.

Option E is incorrect: Because Method Response is the interface with the frontend.

It does not deal with how to map the response from Lambda/backend.

/pets - POST - Method Execution

Method Request

TEST
5 Auth: NONE

ARN: arn:aws:execute-api:us-
_ west-
2: :j3v98efwza/*/F

Client

Method Response

HTTP Status: 200

Models: application/json =>
-_ NewPetResponse

Integration Request

Type: HTTP.
Input passthrough: Yes

Integration Response

HTTP status pattern: - +
Output passthrough: No

**"woo'ide-a}ndexe"juIodpus-owep-2a0}s}ed//:dyU 1SOd

To map exceptions in the API gateway with proper HTTP return codes, the software engineer should perform the following steps:

  1. Add Integration Responses: In the API gateway, the software engineer should add Integration Responses, which define how to handle responses from the backend service. These responses can be associated with regular expression patterns, such as BadRequest or InternalError, and with corresponding HTTP status codes.

  2. Add Method Responses: In the API gateway, the software engineer should add Method Responses, which define how to handle responses from the API endpoint. These responses can also be associated with regular expression patterns, such as BadRequest or InternalError, and with corresponding HTTP status codes, such as 400 and 500.

  3. Add error codes on Integration Response: The software engineer should add the corresponding error codes (400 and 500) on the Integration Response in the API gateway. This ensures that when the backend service returns an error, the API gateway will map it to the appropriate HTTP status code.

  4. Add error codes on Method Response: The software engineer should add the corresponding error codes (400 and 500) on the Method Response in the API gateway. This ensures that when the API endpoint returns an error, the API gateway will map it to the appropriate HTTP status code.

  5. Put the mapping logic into Lambda: The software engineer can put the mapping logic into the Lambda itself so that when an exception happens, error codes are returned at the same time in a JSON body. This approach provides more flexibility and control over the mapping logic.

Overall, options A and B are correct answers. The software engineer should add Integration Responses and Method Responses and associate them with regular expression patterns and corresponding HTTP status codes. Options C, D, and E are incorrect as they either suggest putting the mapping logic into the Lambda or adding Integration Responses or Method Responses with regular expression patterns but not associating them with corresponding HTTP status codes.