Exposing Lambda via API Gateway

API Gateway turns a Lambda function into a real HTTP API with routes, auth, and throttling.

A Lambda function on its own has no URL. API Gateway puts an HTTP endpoint in front of it, mapping routes like GET /users to your function. It also handles authentication, rate limiting, and request validation.

HTTP API vs REST API

  • HTTP API β€” newer, cheaper, faster. The right default for most apps.
  • REST API β€” older, more features (request validation, API keys, WAF integration) when you need them.

Together, API Gateway + Lambda form a complete serverless backend that scales automatically and costs nothing when idle.

Example

Example Β· bash
# Create an HTTP API that proxies all routes to one Lambda
aws apigatewayv2 create-api \
  --name myapp-http-api \
  --protocol-type HTTP \
  --target arn:aws:lambda:us-east-1:123456789012:function:myapp-api

# API Gateway returns an https://... invoke URL you can call immediately

When to use it

  • A team exposes a Lambda function as a REST API with path parameters and rate limiting using API Gateway HTTP API.
  • An ops engineer adds an API key and usage plan to API Gateway to throttle third-party integrations to 1000 requests per day.
  • A developer maps POST /webhooks on API Gateway to a Lambda function that processes incoming Stripe payment events.

More examples

Create HTTP API and Lambda integration

Creates an HTTP API and wires it to a Lambda function using the AWS_PROXY integration type.

Example Β· bash
API_ID=$(aws apigatewayv2 create-api \
  --name my-api \
  --protocol-type HTTP \
  --query 'ApiId' --output text)

INT_ID=$(aws apigatewayv2 create-integration \
  --api-id $API_ID \
  --integration-type AWS_PROXY \
  --integration-uri arn:aws:lambda:us-east-1:123456789012:function:my-handler \
  --payload-format-version 2.0 \
  --query 'IntegrationId' --output text)

Add route and auto-deploy stage

Maps POST /events to the Lambda integration and creates a prod stage with automatic deployment on config changes.

Example Β· bash
aws apigatewayv2 create-route \
  --api-id $API_ID \
  --route-key 'POST /events' \
  --target integrations/$INT_ID

aws apigatewayv2 create-stage \
  --api-id $API_ID \
  --stage-name prod \
  --auto-deploy

Grant API Gateway permission to invoke Lambda

Adds a resource-based policy to the Lambda function allowing API Gateway to invoke it for the specific route.

Example Β· bash
aws lambda add-permission \
  --function-name my-handler \
  --statement-id apigateway-invoke \
  --action lambda:InvokeFunction \
  --principal apigateway.amazonaws.com \
  --source-arn "arn:aws:execute-api:us-east-1:123456789012:${API_ID}/*/*/events"

Discussion

  • Be the first to comment on this lesson.
Exposing Lambda via API Gateway β€” AWS Deployment | SoundsCode