For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/terraform-aws-public-lambda-via-api-gateway.md.
A documentation index is available at /llms.txt.
Allowing a Lambda function to be invoked through a public API Gateway endpoint can introduce security vulnerabilities by exposing the function to the internet. This configuration grants invocation permissions on the Lambda function from any HTTP method and any resource within the associated API Gateway, as illustrated by the use of the wildcard "/*/*" in the ARN. If left unaddressed, attackers could exploit the public endpoint to trigger the Lambda function, potentially resulting in unauthorized data access, denial-of-service attacks, or increased costs due to unwanted invocations. To mitigate this risk, permissions should be restricted to only necessary resources and specific methods, thereby limiting the surface area exposed to the public.
Compliant Code Examples
resource"aws_lambda_permission""apigw"{statement_id="AllowAPIGatewayInvoke"action="lambda:InvokeFunction"function_name=aws_lambda_function.example.function_nameprincipal="apigateway.amazonaws.com" # The "/*/*" portion grants access from any method on any resource
# within the API Gateway REST API.
source_arn="${aws_api_gateway_rest_api.example.execution_arn}/test/test"}resource"aws_lambda_function""example"{function_name="ServerlessPerson"handler="MyHandler::handleRequest"runtime="java11"role=aws_iam_role.lambda_exec.arn}
Non-Compliant Code Examples
resource"aws_lambda_permission""apigw"{statement_id="AllowAPIGatewayInvoke"action="lambda:InvokeFunction"function_name=aws_lambda_function.example.function_nameprincipal="apigateway.amazonaws.com" # The "/*/*" portion grants access from any method on any resource
# within the API Gateway REST API.
source_arn="${aws_api_gateway_rest_api.example.execution_arn}/*/*"}resource"aws_lambda_function""example"{function_name="ServerlessPerson"handler="MyHandler::handleRequest"runtime="java11"role=aws_iam_role.lambda_exec.arn}
1
2
rulesets:- Terraform / AWS # Rules to enforce / AWS.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.