Skill 115 · Connecting Lambda To API Gateway
Subchapter 115.1
references/lambda-gateway-api.mdMarkdown11 KBView on GitHub
This SOP creates a REST API using Amazon API Gateway and connects it to an existing Lambda function, enabling HTTP-based invocation of the Lambda function through API endpoints.
Constraints for parameter acquisition:
Check for required tools and warn the user if any are missing.
Constraints:
Verify that the specified Lambda function exists and is accessible.
Constraints:
aws lambda get-functionCreate a new REST API Gateway with the specified name.
Constraints:
aws apigateway create-rest-apiaws apigateway get-resourcesCreate a new resource under the root resource with the specified path.
Constraints:
aws apigateway create-resourceCreate the specified HTTP method for the resource.
Constraints:
aws apigateway put-method--api-key-required flag if enable_api_key is trueSet up the integration between the API method and Lambda function.
Constraints:
aws apigateway put-integrationConfigure additional security measures for production deployments.
Constraints:
You MUST inform user about production security requirements:
You MUST provide commands for enabling throttling if requested:
aws apigateway create-usage-plan --name {api_name}-usage-plan --throttle burstLimit=100,rateLimit=50 --region {region}
aws apigateway create-api-key --name {api_name}-key --enabled --region {region}You MUST provide CloudWatch logging configuration if requested:
aws logs create-log-group --log-group-name API-Gateway-Execution-Logs_{api_id}/{stage_name} --region {region}
aws apigateway update-stage --rest-api-id {api_id} --stage-name {stage_name} --patch-ops op=replace,path=/accessLogSettings/destinationArn,value=arn:aws:logs:{region}:{account_id}:log-group:API-Gateway-Execution-Logs_{api_id}/{stage_name} --region {region}If CORS is enabled, configure Cross-Origin Resource Sharing settings for the API.
Constraints:
enable_cors parameter is true before proceeding with this stepaws apigateway put-methodAccess-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-MethodsAdd the necessary permissions for API Gateway to invoke the Lambda function.
Constraints:
aws lambda add-permissionDeploy the API to the specified stage to make it accessible.
Constraints:
aws apigateway create-deploymentGet the invoke URL for the deployed API.
Constraints:
https://{api-id}.execute-api.{region}.amazonaws.com/{stage}/{resource-path}Inform the user about the required response format for Lambda proxy integration.
Constraints:
You MUST explain that the Lambda function must return a response in the following format for API Gateway proxy integration:
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"message\": \"response data\"}"
}You MUST inform the user that the body field must be a string (JSON stringified if returning JSON)
You SHOULD provide a link or reference to AWS Lambda proxy integration documentation
You MUST warn that incorrect response format will result in API Gateway errors
Verify that the API Gateway can successfully invoke the Lambda function.
Constraints:
aws apigateway test-invoke-methodlambda_function_name: my-lambda-function
api_name: my-rest-api
region: us-east-1
stage_name: dev
resource_path: execute
http_method: POST
authorization_type: AWS_IAM
enable_api_key: true
enable_cors: trueAPI Gateway URL: https://abc123def4.execute-api.us-east-1.amazonaws.com/dev/execute
Test with curl (requires AWS IAM authentication):
aws apigateway test-invoke-method --rest-api-id abc123def4 --resource-id xyz789 --http-method POST --body '{"key": "value"}'If you receive an error that the Lambda function doesn’t exist, verify the function name is correct and that you have permission to access it.
If API Gateway cannot invoke the Lambda function, ensure the lambda:InvokeFunction permission was added correctly with the proper source ARN.
This typically indicates an issue with the Lambda integration. Check that:
statusCode, headers, and body fieldsbody field must be a string (use JSON.stringify() for JSON responses)If you receive errors about malformed responses, ensure your Lambda function returns:
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"key\": \"value\"}"
}Note that body must be a string, not an object.
If you’re getting CORS errors when calling the API from a web browser:
enable_cors: true when creating the APIIf the API deployment fails, ensure all method and integration configurations are complete before attempting to deploy.