Skip to content

dennj/aws-lambda-go-rest-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS Lambda Go REST Docker

A template for creating AWS Lambda REST APIs with Go, deployed using Docker. This setup supports local testing without requiring AWS API Gateway for routing.

Features

  • AWS Lambda: Run your Go applications in AWS Lambda.
  • Docker: Deploy using Docker containers.
  • Local Testing: Test your API locally without AWS API Gateway.
  • Routing: Handle routing within the Go application.
  • MIT License: Open-source and free to use.

Author

Dennj Osele - dennj.osele@gmail.com

Getting Started

Prerequisites

Clone the Repository

git clone https://github.com/dennj/aws-lambda-go-rest-docker.git
cd aws-lambda-go-rest-docker

Build and Run Locally

Build the Docker Image

docker build --platform linux/amd64 -t docker-image:test .

Run the Docker Container

docker run -d -p 9000:9000 --name lambda-go docker-image:test

Test the Endpoints

curl -X GET "http://localhost:9000/v1/getinfo/12345"
curl -X POST "http://localhost:9000/v1/set/12345/of/67890"

Stop the Docker Container

List Running Containers

docker ps

Stop the Container

docker stop lambda-go

Remove the Container

docker rm lambda-go

Deploy to AWS Lambda

Push Docker Image to Amazon ECR

aws ecr create-repository --repository-name lambda-go-rest
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com
docker tag lambda-go-rest <account-id>.dkr.ecr.<region>.amazonaws.com/lambda-go-rest:latest
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/lambda-go-rest:latest

Create Lambda Function

aws lambda create-function --function-name lambda-go \
--package-type Image \
--code ImageUri=<account-id>.dkr.ecr.<region>.amazonaws.com/lambda-go-rest:latest \
--role <your-execution-role-arn>

Set Up Amazon API Gateway HTTP API

aws apigatewayv2 create-api --name "LambdaGoAPI" --protocol-type HTTP

Integrate Lambda

aws apigatewayv2 create-integration \
--api-id <api-id> \
--integration-type AWS_PROXY \
--integration-uri arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<region>:<account-id>:function:lambda-go/invocations \
--payload-format-version 2.0

Catch all routes

aws apigatewayv2 create-route \
--api-id <api-id> \
--route-key "ANY /{proxy+}" \
--target "integrations/<integration-id>"

Deploy API

aws apigatewayv2 create-deployment --api-id <api-id> --stage-name prod

Add Permissions for API Gateway to Invoke Lambda:

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

About

A template for creating AWS Lambda REST APIs with Go, deployed using Docker. Supports local testing without AWS API Gateway for routing. Licensed under MIT.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors