Question

Difficulty: MediumAPI Development and Integration with Amazon API Gateway

A developer is configuring an Amazon API Gateway REST API that integrates with an AWS Lambda function. To support multiple environments, the developer wants to use API Gateway stage variables to dynamically route requests to the correct Lambda function alias (such as dev or prod) corresponding to the deployed stage. Which two steps must the developer perform to configure this routing and ensure successful invocations?

  1. Define the stage variable in each API Gateway stage to specify the alias name, and reference the stage variable in the Lambda function integration as MyFunction:${stageVariables.aliasName}.Answer
  2. Add the lambda:InvokeFunction permission to the resource-based policy of each Lambda function alias to authorize API Gateway to invoke it.Answer
  3. C
    Configure a Lambda proxy integration mapping template (VTL) in the integration request to inject the stage variable name directly into the function payload.
  4. D
    Create a custom Lambda authorizer to parse the stage variable from the client request and redirect the request to the appropriate Lambda function alias.
  5. E
    Update the client application to append the stage variable name as a query string parameter to the URL so that API Gateway automatically routes to the matching Lambda alias.

Answer

Define the stage variable in each API Gateway stage and reference it in the Lambda function integration as MyFunction:${stageVariables.aliasName}, and manually add the lambda:InvokeFunction permission to the resource-based policy of each Lambda function alias.
Defining the stage variable in each stage and referencing it in the Lambda function integration allows API Gateway to dynamically route requests based on the stage. However, because the console cannot determine the backend function ARN beforehand, the console cannot automatically assign the necessary invoke permissions. Therefore, the developer must manually add lambda:InvokeFunction permission to the resource-based policy of each Lambda function alias.

Step-by-Step Solution

1
Define stage variables in the API Gateway Stage configuration.
Variables like lambdaAlias are assigned values such as 'dev' or 'prod' for each stage.
This establishes the environment-specific values that API Gateway will resolve at runtime.
2
Reference the stage variable in the API Gateway Lambda integration.
The integration points to MyFunction:${stageVariables.lambdaAlias}.
This tells API Gateway to dynamically construct the target ARN using the stage variable.
3
Manually add the lambda:InvokeFunction permission to each Lambda function alias using the AWS CLI.
Resource-based policies are updated to trust apigateway.amazonaws.com.
Since the console cannot determine the target ARN dynamically during setup, it cannot automatically grant invoke permissions, requiring manual configuration.

Key Concept

API Gateway Stage Variables and Dynamic Lambda Routing Permissions
Rate this question