项目作者: appleboy

项目描述 :
running golang using gin framework in AWS Lambda & API Gateway
高级语言: Go
项目地址: git://github.com/appleboy/gin-lambda.git
创建时间: 2018-01-20T02:22:12Z
项目社区:https://github.com/appleboy/gin-lambda

开源协议:MIT License

下载


gin-lambda

running golang using gin framework in AWS Lambda & API Gateway

Sample code

see the main.go in master branch.

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "github.com/apex/gateway"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func helloHandler(c *gin.Context) {
  10. name := c.Param("name")
  11. c.String(http.StatusOK, "Hello %s", name)
  12. }
  13. func welcomeHandler(c *gin.Context) {
  14. c.String(http.StatusOK, "Hello World from Go")
  15. }
  16. func rootHandler(c *gin.Context) {
  17. c.JSON(http.StatusOK, gin.H{
  18. "text": "Welcome to gin lambda server.",
  19. })
  20. }
  21. func routerEngine() *gin.Engine {
  22. // set server mode
  23. gin.SetMode(gin.DebugMode)
  24. r := gin.New()
  25. // Global middleware
  26. r.Use(gin.Logger())
  27. r.Use(gin.Recovery())
  28. r.GET("/welcome", welcomeHandler)
  29. r.GET("/user/:name", helloHandler)
  30. r.GET("/", rootHandler)
  31. return r
  32. }
  33. func main() {
  34. addr := ":" + os.Getenv("PORT")
  35. log.Fatal(gateway.ListenAndServe(addr, routerEngine()))
  36. }

Build and Upload

Build binary

  1. $ GOOS=linux go build -o main .
  2. $ zip deployment.zip main

Upload the deployment.zip to AWS Lambda using drone-lambda command.

  1. $ AWS_ACCESS_KEY_ID=xxxx \
  2. AWS_SECRET_ACCESS_KEY=xxx \
  3. drone-lambda --region ap-southeast-1 \
  4. --function-name function_name \
  5. --zip-file deployment.zip

Output log:

  1. {
  2. CodeSha256: "r/I7yg9tX9MWPsPH337Xk5MIF1dVgkDCFhOrmAYe7hc=",
  3. CodeSize: 4334079,
  4. Description: "",
  5. Environment: {
  6. Variables: {
  7. PORT: "8080"
  8. }
  9. },
  10. FunctionArn: "arn:aws:lambda:ap-southeast-1:xxxxxxx:function:gin:7",
  11. FunctionName: "gin",
  12. Handler: "main",
  13. LastModified: "2018-01-21T06:21:28.395+0000",
  14. MemorySize: 128,
  15. Role: "arn:aws:iam::xxxxxxx:role/service-role/test",
  16. Runtime: "go1.x",
  17. Timeout: 3,
  18. TracingConfig: {
  19. Mode: "PassThrough"
  20. },
  21. Version: "7",
  22. VpcConfig: {
  23. SecurityGroupIds: [],
  24. SubnetIds: []
  25. }
  26. }

AWS Policy

Add the following AWS policy if you want to integrate with CI/CD tools like Jenkins, GitLab Ci or Drone.

  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Effect": "Allow",
  6. "Action": [
  7. "s3:PutObject",
  8. "iam:ListRoles",
  9. "lambda:UpdateFunctionCode",
  10. "lambda:CreateFunction"
  11. ],
  12. "Resource": "*"
  13. }
  14. ]
  15. }

Benchmark

Memory: 128 MB, Latencies: 6.091282008s

  1. $ vegeta attack -rate=1024 -duration=10s -targets=target2.txt | tee results.bin | vegeta report
  2. Requests [total, rate] 10240, 1024.10
  3. Duration [total, attack, wait] 20.335101947s, 9.999018014s, 10.336083933s
  4. Latencies [mean, 50, 95, 99, max] 6.091282008s, 4.893951645s, 14.508009942s, 17.11847442s, 20.128384389s
  5. Bytes In [total, mean] 143360, 14.00
  6. Bytes Out [total, mean] 0, 0.00
  7. Success [ratio] 100.00%
  8. Status Codes [code:count] 200:10240

Memory: 512 MB, Latencies: 1.491340336s

  1. $ vegeta attack -rate=1024 -duration=10s -targets=target2.txt | tee results.bin | vegeta report
  2. Requests [total, rate] 10240, 1024.10
  3. Duration [total, attack, wait] 11.989730554s, 9.999012371s, 1.990718183s
  4. Latencies [mean, 50, 95, 99, max] 1.491340336s, 1.114643849s, 4.112241113s, 6.087949237s, 10.107294516s
  5. Bytes In [total, mean] 143360, 14.00
  6. Bytes Out [total, mean] 0, 0.00
  7. Success [ratio] 100.00%
  8. Status Codes [code:count] 200:10240