0
0
AwsHow-ToBeginner · 4 min read

How to Trigger AWS Lambda from API Gateway Easily

To trigger a AWS Lambda function from API Gateway, create an API Gateway REST API or HTTP API and configure an integration that points to your Lambda function. When the API endpoint is called, API Gateway invokes the Lambda function automatically.
📐

Syntax

The main parts to connect API Gateway to Lambda are:

  • API Gateway API: Defines the HTTP endpoint.
  • Integration: Connects the API method to the Lambda function.
  • Lambda Function: The code that runs when the API is called.

In AWS CLI or CloudFormation, you specify the API method and set the integration type to AWS_PROXY with the Lambda ARN.

bash
aws apigateway create-rest-api --name "MyAPI"

aws lambda create-function --function-name MyFunction --runtime nodejs18.x --handler index.handler --role arn:aws:iam::123456789012:role/lambda-role --zip-file fileb://function.zip

aws apigateway put-integration --rest-api-id <api-id> --resource-id <resource-id> --http-method POST --type AWS_PROXY --integration-http-method POST --uri arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<region>:123456789012:function:MyFunction/invocations
💻

Example

This example shows how to create a simple HTTP API in AWS Console that triggers a Lambda function when you send a GET request.

Steps:

  • Create a Lambda function that returns a message.
  • Create an API Gateway HTTP API.
  • Add an integration to the Lambda function.
  • Deploy and call the API endpoint.
python
import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps({'message': 'Hello from Lambda!'})
    }
Output
{"message": "Hello from Lambda!"}
⚠️

Common Pitfalls

Common mistakes when triggering Lambda from API Gateway include:

  • Not granting API Gateway permission to invoke the Lambda function.
  • Using the wrong integration type (use AWS_PROXY for Lambda proxy integration).
  • Incorrect Lambda function ARN or region in the integration URI.
  • Not deploying the API after changes.

Always check the Lambda execution role and API Gateway permissions.

bash
Wrong permission example:

aws lambda add-permission --function-name MyFunction --statement-id apigateway-test --action lambda:InvokeFunction --principal apigateway.amazonaws.com

Right permission example:

aws lambda add-permission --function-name MyFunction --statement-id apigateway-test --action lambda:InvokeFunction --principal apigateway.amazonaws.com --source-arn arn:aws:execute-api:<region>:123456789012:<api-id>/*/GET/<resource-path>
📊

Quick Reference

Tips for triggering Lambda from API Gateway:

  • Use AWS_PROXY integration for simple request/response handling.
  • Grant API Gateway permission to invoke Lambda with lambda:AddPermission.
  • Deploy API changes before testing.
  • Test Lambda function independently before connecting.

Key Takeaways

Create an API Gateway API and set integration to your Lambda function ARN.
Use AWS_PROXY integration type for automatic request and response mapping.
Grant API Gateway permission to invoke your Lambda function explicitly.
Deploy your API after configuration changes to activate them.
Test Lambda and API separately to isolate issues.