https://github.com/awslabs/aws-lambda-go-api-proxy

lambda-go-api-proxy makes it easy to port APIs written with Go frameworks such as Gin (https://gin-gonic.github.io/gin/ ) to AWS Lambda and Amazon API Gateway.

https://github.com/awslabs/aws-lambda-go-api-proxy

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (9.4%) to scientific vocabulary

Keywords from Contributors

resilience archival interactive projection sequences generic encoding hacking shellcodes modular
Last synced: 11 months ago · JSON representation

Repository

lambda-go-api-proxy makes it easy to port APIs written with Go frameworks such as Gin (https://gin-gonic.github.io/gin/ ) to AWS Lambda and Amazon API Gateway.

Basic Info
  • Host: GitHub
  • Owner: awslabs
  • License: apache-2.0
  • Language: Go
  • Default Branch: master
  • Size: 293 KB
Statistics
  • Stars: 1,157
  • Watchers: 20
  • Forks: 209
  • Open Issues: 84
  • Releases: 0
Archived
Created over 8 years ago · Last pushed over 1 year ago
Metadata Files
Readme Contributing License Code of conduct

README.md

AWS Lambda Go API Proxy Build Status

aws-lambda-go-api-proxy makes it easy to run Go APIs written with frameworks such as Gin with AWS Lambda and Amazon API Gateway.

Getting started

Install required dependencies.

```bash

First, install the Lambda go libraries.

$ go get github.com/aws/aws-lambda-go/events $ go get github.com/aws/aws-lambda-go/lambda

Next, install the core library.

$ go get github.com/awslabs/aws-lambda-go-api-proxy/... ```

Standard library

To use with the standard library, the httpadaptor.New function takes in a http.Handler. The ProxyWithContent method on the httpadapter.HandlerAdapter can then be used as a Lambda handler.

```go package main

import ( "io" "net/http"

"github.com/aws/aws-lambda-go/lambda"
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"

)

func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Hello") })

lambda.Start(httpadapter.New(http.DefaultServeMux).ProxyWithContext)

} ```

Gin

To use with the Gin framework, following the instructions from the Lambda documentation, declare a Handler method for the main package.

Declare a ginadapter.GinLambda object in the global scope, and initialize it in the init function, adding all API methods.

The ProxyWithContext method is then used to translate requests and responses.

```go 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)

}

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) } ```

Fiber

To use with the Fiber framework, following the instructions from the Lambda documentation, declare a Handler method for the main package.

Declare a fiberadapter.FiberLambda object in the global scope, and initialize it in the init function, adding all API methods.

The ProxyWithContext method is then used to translate requests and responses.

```go // main.go package main

import ( "context" "log"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
fiberadapter "github.com/awslabs/aws-lambda-go-api-proxy/fiber"
"github.com/gofiber/fiber/v2"

)

var fiberLambda *fiberadapter.FiberLambda

// init the Fiber Server func init() { log.Printf("Fiber cold start") var app *fiber.App app = fiber.New()

app.Get("/", func(c *fiber.Ctx) error {
    return c.SendString("Hello, World!")
})

fiberLambda = fiberadapter.New(app)

}

// Handler will deal with Fiber 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 fiberLambda.ProxyWithContext(ctx, req) }

func main() { // Make the handler available for Remote Procedure Call by AWS Lambda lambda.Start(Handler) } ```

Other frameworks

This package also supports Negroni, GorillaMux, and plain old HandlerFunc - take a look at the code in their respective sub-directories. All packages implement the Proxy method exactly like our Gin sample above.

Deploying the sample

We have included a SAM template with our sample application. You can use the AWS CLI to quickly deploy the application in your AWS account.

First, build the sample application by running make from the aws-lambda-go-api-proxy directory.

bash $ cd aws-lambda-go-api-proxy $ make

The make process should generate a main.zip file in the sample folder. You can now use the AWS CLI to prepare the deployment for AWS Lambda and Amazon API Gateway.

bash $ cd sample $ aws cloudformation package --template-file sam.yaml --output-template-file output-sam.yaml --s3-bucket YOUR_DEPLOYMENT_BUCKET $ aws cloudformation deploy --template-file output-sam.yaml --stack-name YOUR_STACK_NAME --capabilities CAPABILITY_IAM

Using the CloudFormation console, you can find the URL for the newly created API endpoint in the Outputs tab of the sample stack - it looks sample like this: https://xxxxxxxxx.execute-api.xx-xxxx-x.amazonaws.com/Prod/pets. Open a browser window and try to call the URL.

API Gateway context and stage variables

~~The RequestAccessor object, and therefore GinLambda, automatically marshals the API Gateway request context and stage variables objects and stores them in custom headers in the request: X-GinLambda-ApiGw-Context and X-GinLambda-ApiGw-StageVars. While you could manually unmarshal the json content into the events.APIGatewayProxyRequestContext and map[string]string objects, the library exports two utility methods to give you easy access to the data.~~

The gateway context, stage variables and lambda runtime variables are automatically populate to the context.

```go // the methods are available in your instance of the GinLambda // object and receive the context apiGwContext := ginLambda.GetAPIGatewayContextFromContext(ctx) apiGwStageVars := ginLambda.GetStageVarsFromContext(ctx) runtimeContext := ginLambda.GetRuntimeContextFromContext(ctx)

// you can access the properties of the context directly log.Println(apiGwContext.RequestID) log.Println(apiGwContext.Stage) log.Println(runtimeContext.InvokedFunctionArn)

// stage variables are stored in a map[string]string stageVarValue := apiGwStageVars["MyStageVar"] ```

Supporting other frameworks

The aws-lambda-go-api-proxy, alongside the various adapters, declares a core package. The core package, contains utility methods and interfaces to translate API Gateway proxy events into Go's default http.Request and http.ResponseWriter objects.

You can see that the ginlambda.go file extends the RequestAccessor struct defined in the request.go file. RequestAccessor gives you access to the ProxyEventToHTTPRequest() method.

The GinLambda object is initialized with an instance of gin.Engine. gin.Engine implements methods defined in the http.Handler interface.

The Proxy method of the GinLambda object simply receives the events.APIGatewayProxyRequest object and uses the ProxyEventToHTTPRequest() method to convert it into an http.Request object. Next, it creates a new ProxyResponseWriter object (defined in the response.go) file and passes both request and response writer to the ServeHTTP method of the gin.Engine.

The ProxyResponseWriter exports a method called GetProxyResponse() to generate an events.APIGatewayProxyResponse object from the data written to the response writer.

Support for frameworks other than Gin can rely on the same methods from the core package and swap the gin.Engine object for the relevant framework's object.

License

This library is licensed under the Apache 2.0 License.

Owner

  • Name: Amazon Web Services - Labs
  • Login: awslabs
  • Kind: organization
  • Location: Seattle, WA

AWS Labs

GitHub Events

Total
  • Issues event: 4
  • Watch event: 96
  • Delete event: 1
  • Issue comment event: 12
  • Push event: 1
  • Pull request event: 4
  • Fork event: 11
  • Create event: 1
Last Year
  • Issues event: 4
  • Watch event: 96
  • Delete event: 1
  • Issue comment event: 12
  • Push event: 1
  • Pull request event: 4
  • Fork event: 11
  • Create event: 1

Committers

Last synced: over 1 year ago

All Time
  • Total Commits: 124
  • Total Committers: 51
  • Avg Commits per committer: 2.431
  • Development Distribution Score (DDS): 0.79
Past Year
  • Commits: 1
  • Committers: 1
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
sapessi s****i@g****m 26
dependabot[bot] 4****] 8
James Siri j****i@a****m 7
Bhavik Shah b****t 6
David Malone a****e@g****m 5
Arran Ubels a****s@b****m 4
Emil Pirfält e****t@j****m 4
Zoltan Arvai z****i@l****m 4
Łukasz Rżanek l****k@r****l 3
drakejin d****0@g****m 3
Jim Ancona j****m@a****m 3
Marcus Rosén m****n@i****m 3
Buliani b****s@3****m 2
Alex Last a****t@g****m 2
Daniel White d****l@f****u 2
Marco Borromeo m****o@m****m 2
Nikolay Sarychev n****v 2
Peter Mescalchin p****r@m****m 2
Tara Arent a****a@a****m 2
Kyle Bailey k****y@g****m 2
Tharun t****i@g****m 2
EC2 Default User e****r@i****l 1
3unch4n 3****n@g****m 1
Adrian Hesketh a****h 1
Aldrin Leal a****h@l****r 1
watermelon 8****9@q****m 1
vishal24tuniki v****i@g****m 1
uji 4****g 1
minchao m****0@g****m 1
jslang 1****g 1
and 21 more...

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 63
  • Total pull requests: 73
  • Average time to close issues: 2 months
  • Average time to close pull requests: 5 months
  • Total issue authors: 58
  • Total pull request authors: 47
  • Average comments per issue: 3.08
  • Average comments per pull request: 1.92
  • Merged pull requests: 31
  • Bot issues: 0
  • Bot pull requests: 16
Past Year
  • Issues: 5
  • Pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 5
  • Pull request authors: 1
  • Average comments per issue: 0.2
  • Average comments per pull request: 0.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 1
Top Authors
Issue Authors
  • frankyhun (2)
  • sapessi (2)
  • caevv (2)
  • ss49919201 (1)
  • iamsalnikov (1)
  • hohmannr (1)
  • alexanderjshall (1)
  • HirokoMatsumoto-fp (1)
  • tschirmer (1)
  • estebane-frb (1)
  • hiteshshahjee (1)
  • mcblair (1)
  • pohika (1)
  • kdeloach (1)
  • reecerussell (1)
Pull Request Authors
  • dependabot[bot] (21)
  • NikSchaefer (4)
  • iamsalnikov (3)
  • AlexLast (3)
  • drakejin (2)
  • ikegam1 (2)
  • sportlane (2)
  • k-bailey (2)
  • hohmannr (1)
  • ckcks12 (1)
  • snabb (1)
  • vishal24tuniki (1)
  • reecerussell (1)
  • tharun-d (1)
  • arentta (1)
Top Labels
Issue Labels
enhancement (2) help wanted (1)
Pull Request Labels
dependencies (20)

Packages

  • Total packages: 3
  • Total downloads: unknown
  • Total docker downloads: 1,913
  • Total dependent packages: 247
    (may contain duplicates)
  • Total dependent repositories: 255
    (may contain duplicates)
  • Total versions: 46
proxy.golang.org: github.com/awslabs/aws-lambda-go-api-proxy
  • Versions: 22
  • Dependent Packages: 247
  • Dependent Repositories: 255
  • Docker Downloads: 1,913
Rankings
Dependent packages count: 0.3%
Dependent repos count: 0.4%
Docker downloads count: 1.0%
Average: 1.2%
Forks count: 2.1%
Stargazers count: 2.2%
Last synced: 11 months ago
proxy.golang.org: github.com/awslabs/aws-lambda-go-API-Proxy
  • Versions: 22
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.7%
Average: 5.9%
Dependent repos count: 6.1%
Last synced: 12 months ago
proxy.golang.org: github.com/awslabs/aws-lambda-go-api-proxy/sample
  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 7.0%
Average: 8.2%
Dependent repos count: 9.3%
Last synced: 12 months ago

Dependencies

go.mod go
  • github.com/BurntSushi/toml v1.1.0
  • github.com/aws/aws-lambda-go v1.19.1
  • github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible
  • github.com/gin-gonic/gin v1.7.7
  • github.com/go-chi/chi/v5 v5.0.2
  • github.com/goccy/go-json v0.9.7
  • github.com/gofiber/fiber/v2 v2.1.0
  • github.com/gorilla/mux v1.7.4
  • github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88
  • github.com/kataras/iris/v12 v12.2.0-alpha9
  • github.com/kataras/tunnel v0.0.4
  • github.com/klauspost/compress v1.15.6
  • github.com/labstack/echo/v4 v4.1.17
  • github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
  • github.com/onsi/ginkgo v1.16.5
  • github.com/onsi/gomega v1.18.1
  • github.com/tdewolff/minify/v2 v2.11.10
  • github.com/urfave/negroni v1.0.0
  • github.com/valyala/fasthttp v1.34.0
  • golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
  • golang.org/x/net v0.0.0-20220617184016-355a448f1bc9
  • golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c
  • golang.org/x/time v0.0.0-20220609170525-579cf78fd858
  • google.golang.org/protobuf v1.28.0
  • gopkg.in/ini.v1 v1.66.6
  • gopkg.in/yaml.v2v2.2.2=>gopkg.in/yaml.v2 v2.2.8
  • gopkg.in/yaml.v2v2.2.3=>gopkg.in/yaml.v2 v2.2.8
  • gopkg.in/yaml.v2v2.2.4=>gopkg.in/yaml.v2 v2.2.8
  • gopkg.in/yaml.v3 v3.0.1
go.sum go
  • 811 dependencies
sample/go.mod go
  • github.com/aws/aws-lambda-go v1.10.0
  • github.com/awslabs/aws-lambda-go-api-proxy v0.3.0
  • github.com/gin-gonic/gin v1.7.0
  • github.com/google/uuid v1.1.1
  • github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
  • github.com/modern-go/reflect2 v1.0.1
  • github.com/onsi/ginkgo v1.16.5
  • github.com/onsi/gomega v1.19.0
  • gopkg.in/yaml.v2v2.2.2=>gopkg.in/yaml.v2 v2.2.8
  • gopkg.in/yaml.v2v2.2.4=>gopkg.in/yaml.v2 v2.2.8
sample/go.sum go
  • 118 dependencies