Hands-On: Put a URL on Your Function
Create an HTTP API in front of a Lambda function with two CLI commands.
This uses quick create, which makes an HTTP API with a default route and an auto-deploying stage, all pointing at one function. Replace the account ID, Region, and function name with yours.
Try it
REGION=us-east-1
ACCOUNT=123456789012
FN_ARN=arn:aws:lambda:$REGION:$ACCOUNT:function:hello-notesnap
# 1. Create the API (quick create wires every route to the function)
aws apigatewayv2 create-api \
--name notesnap-api \
--protocol-type HTTP \
--target $FN_ARN
# 2. Allow API Gateway to invoke the function (use the ApiId from step 1)
aws lambda add-permission \
--function-name hello-notesnap \
--statement-id apigateway-invoke \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:$REGION:$ACCOUNT:<ApiId>/*"
# 3. Call it
curl https://<ApiId>.execute-api.$REGION.amazonaws.com/Why step 2?
Lambda has its own resource-based policy listing who may invoke it. Without add-permission, API Gateway
is blocked and callers get a 500 error. The console adds this permission for you automatically, which is why
people often miss it when using the CLI.
Just need one quick URL for a single function, with no routing, auth options, or throttling? Lambda Function URLs are even simpler. API Gateway is the better choice once you need multiple routes, JWT auth, or throttling.
Key takeaways
- Quick create builds an HTTP API, a default route, and a stage in one command.
- Lambda must explicitly allow apigateway.amazonaws.com to invoke it.
- Lambda Function URLs are a simpler option for single-function endpoints.
Check your understanding
Pick an answer to see the explanation.
1. Your new API returns 500 and Lambda logs show nothing at all. What's the likely cause?