{"id":644,"date":"2020-06-07T10:00:00","date_gmt":"2020-06-07T01:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/06\/07\/aws-lambda-with-golang-gin"},"modified":"2020-06-07T10:00:00","modified_gmt":"2020-06-07T01:00:00","slug":"aws-lambda-with-golang-gin","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/06\/07\/aws-lambda-with-golang-gin\/","title":{"rendered":"AWS Lambda\u3067Golang\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30afGin\u3092\u5229\u7528\u3057\u3066\u307f\u305f"},"content":{"rendered":"
\u6700\u8fd1\u306f\u4f53\u8abf\u7ba1\u7406\u306b\u4e00\u756a\u56f0\u3063\u3066\u3044\u308b\u3084\u3063\u3055\u3093\u3067\u3054\u3056\u3044\u307e\u3059\u3002
\n\u982d\u75db\u3084\u80a9\u3053\u308a\u3001\u305d\u3057\u3066\u8170\u75db\u3002
\n\u5e74\u9f62\u304c\u4e0a\u304c\u308b\u3068\u4eba\u306b\u306f\u8a71\u3057\u306b\u304f\u3044\u60a9\u307f\u304c\u5897\u3048\u3066\u3044\u304d\u307e\u3059\u306d\u3002<\/p>\n
\u3055\u3066\u3001\u4eca\u56de\u306fAWS Lambda\u3067
\nGolang\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30afGin\u3092\u5229\u7528\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n
Gin\u306fGo\uff08Golang\uff09\u3067\u8a18\u8ff0\u3055\u308c\u305fWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u3002
\n \u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u512a\u308c\u305f\u751f\u7523\u6027\u304c\u5fc5\u8981\u306a\u5834\u5408\u306f\u3001Gin\u304c\u6c17\u306b\u5165\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\n
AWS Lambda\u3067Gin\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u306e\u30e1\u30ea\u30c3\u30c8\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002<\/p>\n
Gin\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u518d\u5229\u7528\u6027\u3068\u53ef\u642c\u6027\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002
\nGin\u306fWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u306e\u3067\u3001\u30b5\u30fc\u30d0\u30fc\u3067\u3082\u52d5\u304b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\n\u5c11\u3057\u306e\u4fee\u6b63\u3067AWS Lamda\u304b\u3089\u30b3\u30f3\u30c6\u30ca\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306b\u5909\u66f4\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
Golang\u3067\u306fAWS Lambda\u306b\u7279\u5316\u3057\u305f\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\n\u4ed6\u306e\u8a00\u8a9e\u3067\u3042\u308c\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c\u5b58\u5728\u3057\u307e\u3059\u3002<\/p>\n
\u300cLambda\u3067Spring Boot\u3092\u4f7f\u3048\u308b\u304b\u300d<\/strong>\u3068\u3044\u3046\u8a71\u3092\u52c9\u5f37\u4f1a\u3067\u8033\u306b\u3057\u307e\u3057\u305f\u3002 Gin\u3082\u4eba\u6c17\u306e\u3042\u308bWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3001\u30b9\u30bf\u30fc\u6570\u306f30,000\u3092\u8d85\u3048\u3066\u3044\u307e\u3059\u3002 \u3053\u306e\u3088\u3046\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u4ed6\u306b\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n \u4ee5\u4e0b\u306e\u524d\u63d0\u6761\u4ef6\u3067\u69cb\u7bc9\u3057\u307e\u3057\u305f\u3002<\/p>\n \u4eca\u56de\u306fServerless Framework\u3092\u5229\u7528\u3057\u307e\u3059\u3002<\/p>\n awslabs\u304c\u63d0\u4f9b\u3057\u3066\u3044\u308baws-lambda-go-api-proxy\u3092\u5229\u7528\u3057\u3066\u3001 \u8a18\u8ff0\u5185\u5bb9\u306bGin\u7279\u6709\u306e\u5909\u66f4\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/strong> \u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092Makefile\u306b\u7528\u610f\u3057\u307e\u3057\u305f\u3002<\/p>\n \u5b9f\u884c\u3067\u304d\u307e\u3057\u305f\uff01<\/p>\n <\/p>\n Hey\u3068\u3044\u3046\u30c4\u30fc\u30eb\u3092\u4f7f\u3063\u3066\u6e2c\u3063\u3066\u307f\u307e\u3057\u305f\u3002 \u3044\u3068\u3082\u7c21\u5358\u306bLambda\u3067Gin\u3092\u5229\u7528\u3067\u304d\u307e\u3057\u305f\u3002
\n\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u304b\u3089\u4fe1\u983c\u3055\u308c\u3066\u3044\u308b\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092Lambda\u3067\u5229\u7528\u3067\u304d\u308c\u3070\u3001
\n\u751f\u7523\u6027\u3082\u4e0a\u304c\u308a\u3001\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u304b\u3089\u591a\u304f\u306e\u6280\u8853\u7684\u77e5\u898b\u3082\u5f97\u3089\u308c\u308b ...
\n\u3053\u306e\u3088\u3046\u306a\u8a71\u304c\u51fa\u308b\u306e\u306f\u5f53\u7136\u3067\u3059\u3002
\n\uff08Lambda\u3067Spring Boot\u3092\u4f7f\u3046\u306e\u304c\u672c\u756a\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u306b\u9069\u3057\u3066\u3044\u308b\u304b\u306f\u3001\u5225\u306e\u8a71\u3067\u3059\u304c\u3002\uff09<\/p>\n
\nLambda\u3067Web\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u5fc5\u8981\u306a\u6a5f\u80fd\u3092\u30d5\u30eb\u30b9\u30af\u30e9\u30c3\u30c1\u3067\u4f5c\u308b\u3088\u308a\u3001
\nGin\u306a\u3069\u306e\u6709\u540d\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u5229\u7528\u3057\u305f\u307b\u3046\u304c\u751f\u7523\u6027\u306f\u4e0a\u304c\u308b\u3067\u3057\u3087\u3046\u3002<\/strong><\/p>\n\n
AWS Lambda\u3067Gin\u3092\u5229\u7528\u3057\u3066\u307f\u308b<\/h2>\n
\n
\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210<\/h3>\n
- src\n - hanlder\n - main.go\n- Makefile\n- serverless.yml<\/code><\/pre>\n
main.go\u306e\u5185\u5bb9<\/h3>\n
\nLambda\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u60c5\u5831\u3092Gin\u306e\u6a5f\u80fd\u306b\u53d7\u3051\u6e21\u3057\u307e\u3059\u3002<\/p>\npackage main\n\nimport (\n "log"\n "context"\n\n "github.com\/aws\/aws-lambda-go\/events"\n "github.com\/aws\/aws-lambda-go\/lambda"\n "github.com\/awslabs\/aws-lambda-go-api-proxy\/gin"\n "github.com\/gin-gonic\/gin"\n)\n\nvar ginLambda *ginadapter.GinLambda\n\nfunc init() {\n \/\/ stdout and stderr are sent to AWS CloudWatch Logs\n log.Printf("Gin cold start")\n r := gin.Default()\n r.GET("\/ping", func(c *gin.Context) {\n c.JSON(200, gin.H{\n "message": "pong",\n })\n })\n\n ginLambda = ginadapter.New(r)\n}\n\nfunc Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {\n \/\/ If no name is provided in the HTTP request body, throw an error\n return ginLambda.ProxyWithContext(ctx, req)\n}\n\nfunc main() {\n lambda.Start(Handler)\n}\n<\/code><\/pre>\n
serverless.yml\u306e\u5185\u5bb9<\/h3>\n
\n\u666e\u6bb5\u901a\u308a\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\nservice: aws-lambda-go-api-proxy-gin\n\nprovider:\n name: aws\n runtime: go1.x\n stage: ${opt:stage, self:custom.defaultStage}\n region: ap-northeast-1\n iamRoleStatements:\n - Effect: "Allow"\n Action:\n - "logs:*"\n Resource: "*"\n\npackage:\n exclude:\n - .\/**\n include:\n - .\/bin\/**\n\ncustom:\n defaultStage: dev\n\nfunctions:\n api:\n handler: bin\/main\n timeout: 900\n events:\n - http:\n path: ping\n method: get\n<\/code><\/pre>\n
\u30c7\u30d7\u30ed\u30a4<\/h3>\n
build:\n go mod download\n env GOOS=linux go build -ldflags="-s -w" -o bin\/main src\/handler\/main.go\n\ndeploy: build\n sls deploy --verbose --aws-profile $(PROFILE)<\/code><\/pre>\n
\u5b9f\u884c\u3057\u3066\u307f\u308b<\/h3>\n
\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6e2c\u3063\u3066\u307f\u308b<\/h3>\n
\n\u30b3\u30fc\u30eb\u30c9\u30b9\u30bf\u30fc\u30c8\u30670.4\u79d2<\/strong>\b\u306a\u3089\u3088\u3055\u305d\u3046\u3067\u3059\uff01<\/p>\nSummary:\n Total: 0.5447 secs\n Slowest: 0.4115 secs\n Fastest: 0.0246 secs\n Average: 0.1200 secs\n Requests\/sec: 367.1871\n\n Total data: 3600 bytes\n Size\/request: 18 bytes\n\nResponse time histogram:\n 0.025 [1] |\n 0.063 [145] |\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\n 0.102 [1] |\n 0.141 [2] |\u25a0\n 0.179 [2] |\u25a0\n 0.218 [0] |\n 0.257 [0] |\n 0.295 [1] |\n 0.334 [8] |\u25a0\u25a0\n 0.373 [32] |\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\u25a0\n 0.411 [8] |\u25a0\u25a0\n\n\nLatency distribution:\n 10% in 0.0328 secs\n 25% in 0.0381 secs\n 50% in 0.0442 secs\n 75% in 0.1740 secs\n 90% in 0.3583 secs\n 95% in 0.3691 secs\n 99% in 0.3913 secs\n\nDetails (average, fastest, slowest):\n DNS+dialup: 0.0151 secs, 0.0246 secs, 0.4115 secs\n DNS-lookup: 0.0030 secs, 0.0000 secs, 0.0124 secs\n req write: 0.0000 secs, 0.0000 secs, 0.0010 secs\n resp wait: 0.1048 secs, 0.0245 secs, 0.3504 secs\n resp read: 0.0000 secs, 0.0000 secs, 0.0002 secs\n\nStatus code distribution:\n [200] 200 responses<\/code><\/pre>\n
\n\u3053\u3053\u304b\u3089\u306b\u3055\u3089\u306b\u30c7\u30fc\u30bf\u30bd\u30fc\u30b9\u3092DynamoDB\u304b\u3089RDB\u306b\u5909\u66f4\u3057\u3084\u3059\u3044\u8a2d\u8a08\u3068\u3059\u308c\u3070
\n\u3088\u308a\u518d\u5229\u7528\u6027\u3068\u53ef\u642c\u6027\u304c\u5411\u4e0a\u3057\u307e\u3059\u306d\u3002<\/p>\n