Skip to content

Instantly share code, notes, and snippets.

@decagondev
Created December 26, 2024 22:11
Show Gist options
  • Save decagondev/6f38c78d03342ab88298dc2deaa5e297 to your computer and use it in GitHub Desktop.
Save decagondev/6f38c78d03342ab88298dc2deaa5e297 to your computer and use it in GitHub Desktop.

AWS Lambda API Setup Guide

This guide explains how to set up an AWS Lambda function with API Gateway integration using the AWS CLI.

Prerequisites

  • AWS CLI installed and configured
  • Java 11
  • Gradle
  • AWS Account with appropriate permissions
  • IAM Role for Lambda execution

Quick Start

  1. Build and package your Lambda function
  2. Create the Lambda function in AWS
  3. Set up API Gateway
  4. Configure integrations and permissions
  5. Deploy and test

Detailed Setup Steps

1. Package Your Lambda Function

Build your Java project:

gradle clean build

Create deployment package:

zip function.zip build/libs/<your-jar-file-name>.jar

2. Create Lambda Function

Create the function using AWS CLI:

aws lambda create-function \
    --function-name PostRequestLambda \
    --runtime java11 \
    --role arn:aws:iam::<account-id>:role/<role-name> \
    --handler com.example.lambda.PostRequestHandler::handleRequest \
    --timeout 15 \
    --memory-size 512 \
    --zip-file fileb://function.zip

3. API Gateway Setup

Create HTTP API:

aws apigatewayv2 create-api \
    --name "PostRequestAPI" \
    --protocol-type "HTTP"

Create Lambda integration:

aws apigatewayv2 create-integration \
    --api-id <ApiId> \
    --integration-type AWS_PROXY \
    --integration-uri arn:aws:lambda:<region>:<account-id>:function:PostRequestLambda

Create route:

aws apigatewayv2 create-route \
    --api-id <ApiId> \
    --route-key "POST /post"

4. Deployment and Permissions

Deploy the API:

aws apigatewayv2 create-deployment \
    --api-id <ApiId>

Grant API Gateway permissions:

aws lambda add-permission \
    --function-name PostRequestLambda \
    --statement-id ApiGatewayInvokePermission \
    --action lambda:InvokeFunction \
    --principal apigateway.amazonaws.com \
    --source-arn arn:aws:execute-api:<region>:<account-id>:<ApiId>/*/POST/post

Testing

Test your API endpoint using curl:

curl -X POST \
    -H "Content-Type: application/json" \
    -d '{"name":"AWS"}' \
    https://<api-id>.execute-api.<region>.amazonaws.com/post

Configuration Variables

Replace the following placeholders with your actual values:

  • <account-id>: Your AWS account ID
  • <region>: Your AWS region
  • <ApiId>: API Gateway ID from creation step
  • <role-name>: IAM role name for Lambda execution
  • <api-id>: API Gateway endpoint ID

Troubleshooting

Common issues and solutions:

  1. Permission errors: Verify IAM role permissions
  2. Deployment failures: Check Lambda function handler path
  3. Integration errors: Confirm API Gateway configuration
  4. Timeout issues: Adjust Lambda timeout setting

Additional Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment