Home

Lambda Go Servers

This following example uses Gin.

TODO: Add in the learnings from the local repo.

Resources

  1. Gin + Lambda Go
  2. Apex Gateway
  3. Gin + Lambda AWSLabs
  4. TS Lambda
  5. AWS CDK - API Gateway
  6. AWS CDK - First Go Lambda Function

Gin + Lambda with AWSLabs proxy

package main import ( "log" "context" "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-lambda-go/lambda" "github.com/awslabs/aws-lambda-go-api-proxy/gin" "github.com/gin-gonic/gin" ) var ginLambda *ginadapter.GinLambda func init() { // stdout and stderr are sent to AWS CloudWatch Logs log.Printf("Gin cold start") r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong", }) }) ginLambda = ginadapter.New(r) } // Handler will deal with Gin working with Lambda func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { // If no name is provided in the HTTP request body, throw an error return ginLambda.ProxyWithContext(ctx, req) } func main() { lambda.Start(Handler) }

Gin + Lambda with Apex Gateway

package main import ( "log" "net/http" "os" "github.com/apex/gateway" "github.com/gin-gonic/gin" ) func helloHandler(c *gin.Context) { name := c.Param("name") c.String(http.StatusOK, "Hello %s", name) } func welcomeHandler(c *gin.Context) { c.String(http.StatusOK, "Hello World from Go") } func rootHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "text": "Welcome to gin lambda server.", }) } func routerEngine() *gin.Engine { // set server mode gin.SetMode(gin.DebugMode) r := gin.New() // Global middleware r.Use(gin.Logger()) r.Use(gin.Recovery()) r.GET("/welcome", welcomeHandler) r.GET("/user/:name", helloHandler) r.GET("/", rootHandler) return r } func main() { addr := ":" + os.Getenv("PORT") log.Fatal(gateway.ListenAndServe(addr, routerEngine())) }

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/go/lambda-go-servers

Sections


Related