- Confirm Endpoints: After
serverless deploy, runserverless infoto list endpoints and stack outputs. Ensure the URLs match exactly, including the stage (e.g.,/dev/). - Test with Tools: Use
curl -v https://your-endpoint.com/pathor Postman to inspect response headers for clues (e.g., CORS or "Internal Server Error"). - AWS Console Check: Navigate to API Gateway > Your API > Stages and verify no deployment errors or invalid mappings.
This is often the fastest way to identify errors, as 500s typically log stack traces.
- Enable Logging: In
serverless.yml, ensureprovider: logs: rest: truefor API Gateway. - AWS Console: Go to CloudWatch > Log groups >
/aws/lambda/your-function-name. Filter recent invocations for errors like unhandled exceptions, timeouts, or missing modules. - CLI: Run
serverless logs -f yourFunctionName --tailto stream logs while hitting the endpoint. - Common Issues: Runtime errors (e.g., undefined variables), missing environment variables, or dependency issues (verify
package.json/requirements are bundled).
Bypass API Gateway to isolate the issue.
- AWS Console: Lambda > Functions > Your function > Test tab. Create a test event mimicking the API request (e.g., JSON with method, path, headers).
- Outcome: If it fails, suspect code/config; if it succeeds, check API Gateway (e.g., integration mappings or authorization).
- CLI: Use
serverless invoke -f yourFunctionName --data '{"key": "value"}'to simulate.
500s can result from Lambda lacking resource access (e.g., DynamoDB, S3).
- Serverless Config: Ensure
provider.iamRoleStatementsinserverless.ymlgrants necessary permissions. - AWS Console: Lambda > Your function > Configuration > Permissions. Verify the execution role and test policies with IAM Policy Simulator if needed.
- Log Check: Look for "AccessDeniedException" in CloudWatch.
- Validate Config: Check
serverless.ymlfor correct function handlers, environment variables, and plugins (e.g.,serverless-offline). Runserverless printto expand config. - Dependencies: For Node.js/Python, ensure all dependencies are included (use
serverless packageto inspect the zip). Watch for native modules not built for Lambda runtime. - Environment Variables: Missing secrets/configs cause runtime failures—use AWS SSM or Secrets Manager and reference correctly.
- X-Ray: Add
provider: tracing: lambda: trueinserverless.yml. Use AWS X-Ray to trace invocations and analyze init vs. invocation time. - API Gateway Logging: Enable execution logs in API Gateway stages for request/response details.
- Timeouts: If suspected, check/increase Lambda timeout/memory in
serverless.yml.
provider:
name: aws
runtime: nodejs18.x # Adjust to your runtime
stage: ${opt:stage, 'dev'}
region: us-east-1
# Logging
logs:
lambda:
logFormat: json
logRetentionInDays: 30
restApi:
executionLogging: true
level: INFO
fullExecutionData: true
accessLogging: true
# Tracing
tracing:
lambda: true
apiGateway: true
# IAM for X-Ray (if needed)
iamRoleStatements:
- Effect: Allow
Action:
- xray:PutTraceSegments
- xray:PutTelemetryRecords
Resource: "*"
functions:
yourFunction:
handler: handler.main
events:
- http:
path: /your-path
method: get