akrylysov/algnhsa
소개
Go net/http 서버용 AWS Lambda 어댑터
시스템 및 인프라측정된 스타 증가
- in 24h
- 0
- in 7 days
- -1
- in 15 days
- -1
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
Originalalgnhsa

algnhsa is an AWS Lambda Go net/http server adapter.
algnhsa enables running Go web applications on AWS Lambda and API Gateway or ALB without changing the existing HTTP handlers:
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/akrylysov/algnhsa"
)
func addHandler(w http.ResponseWriter, r *http.Request) {
f, _ := strconv.Atoi(r.FormValue("first"))
s, _ := strconv.Atoi(r.FormValue("second"))
w.Header().Set("X-Hi", "foo")
fmt.Fprintf(w, "%d", f+s)
}
func contextHandler(w http.ResponseWriter, r *http.Request) {
lambdaEvent, ok := algnhsa.APIGatewayV2RequestFromContext(r.Context())
if ok {
fmt.Fprint(w, lambdaEvent.RequestContext.AccountID)
}
}
func main() {
http.HandleFunc("/add", addHandler)
http.HandleFunc("/context", contextHandler)
algnhsa.ListenAndServe(http.DefaultServeMux, nil)
}
Plug in a third-party web framework
Gin
package main
import (
"net/http"
"github.com/akrylysov/algnhsa"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "hi",
})
})
algnhsa.ListenAndServe(r, nil)
}
echo
package main
import (
"net/http"
"github.com/akrylysov/algnhsa"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "hi")
})
algnhsa.ListenAndServe(e, nil)
}
chi
package main
import (
"net/http"
"github.com/akrylysov/algnhsa"
"github.com/go-chi/chi"
)
func main() {
r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hi"))
})
algnhsa.ListenAndServe(r, nil)
}
Fiber
package main
import (
"github.com/akrylysov/algnhsa"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
)
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
algnhsa.ListenAndServe(adaptor.FiberApp(app), nil)
}
Deployment
First, build your Go application for Linux and zip it:
GOOS=linux GOARCH=amd64 go build -tags lambda.norpc -o bootstrap
zip function.zip bootstrap
When creating a new function, choose the "Provide your own bootstrap on Amazon Linux 2" runtime or "Custom runtime on Amazon Linux 2" when modifying an existing function. Make sure to use bootstrap as the executable name and as the handler name in AWS.
AWS provides plenty of ways to expose a Lambda function to the internet.
Lambda Function URL
This is the easier way to deploy your Lambda function as an HTTP endpoint. It only requires going to the "Function URL" section of the Lambda function configuration and clicking "Configure Function URL".
API Gateway
HTTP API
Create a new HTTP API.
Configure a catch-all
$defaultroute.
REST API
Create a new REST API.
In the "Resources" section create a new
ANYmethod to handle requests to/(check "Use Lambda Proxy Integration").Add a catch-all
{proxy+}resource to handle requests to every other path (check "Configure as proxy resource").
ALB
Create a new ALB and point it to your Lambda function.
In the target group settings in the "Attributes" section enable "Multi value headers".
관련 프로젝트 살펴보기
불러오는 중…