A company publishes a REST API in Amazon API Gateway for its partners' internal tools.
Only requests from the partners' two office ranges, 203.0.113.0/24 and 198.51.100.0/24, may reach the API. All other callers must be rejected by API Gateway before the Lambda backend runs. The backend code must not change, and the company does not want to add more AWS services.
What should the developer do?
Choose one answer.
AA. Add a Lambda authorizer that compares the X-Forwarded-For header with the two ranges and returns a Deny policy otherwise.
BB. Associate a security group that allows only the two ranges with the API's regional endpoint.
CC. Attach a resource policy to the API that denies execute-api:Invoke unless aws:SourceIp is within the two ranges.
DD. Issue an API key for each office, add each to a usage plan and require an API key on every method.
Show the answer and explanation
Answer: C
A. Wrong: This adds a Lambda function to build and run on each request, and the X-Forwarded-For header can be set by the caller, so it is not a trustworthy source address. A resource policy checks the real source address natively. Learn more: Lambda authorizers for REST APIs (opens in a new tab)
B. Wrong: Security groups attach to network interfaces such as EC2 instances and load balancers, not to API Gateway APIs. API Gateway offers resource policies for restricting source IP addresses.
An API Gateway resource policy can allow or deny invocation by source IP using the aws:SourceIp condition, and it is enforced by API Gateway before any backend runs. A Lambda authorizer would add code and rely on a spoofable header, security groups do not apply to API Gateway, and API keys are for usage tracking.
Want more on API authorization and tokens? The 5 full-length practice sets have 15 more API authorization and tokens questions, 390 in all, none of them in the free exam. One payment of $14.99, no subscription. See the practice sets →