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?
- 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
- Add the lambda:InvokeFunction permission to the resource-based policy of each Lambda function alias to authorize API Gateway to invoke it.Answer
- CConfigure a Lambda proxy integration mapping template (VTL) in the integration request to inject the stage variable name directly into the function payload.
- DCreate a custom Lambda authorizer to parse the stage variable from the client request and redirect the request to the appropriate Lambda function alias.
- EUpdate 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
Key Concept
API Gateway Stage Variables and Dynamic Lambda Routing Permissions