{"id":461,"date":"2018-06-08T18:25:27","date_gmt":"2018-06-08T09:25:27","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/06\/08\/aws-sam-go"},"modified":"2018-06-08T18:25:27","modified_gmt":"2018-06-08T09:25:27","slug":"aws-sam-go","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/06\/08\/aws-sam-go\/","title":{"rendered":"AWS SAM\u3068Golang\u3067Lambda\u95a2\u6570\u306e\u958b\u767a\u74b0\u5883\u3092\u3064\u304f\u308b"},"content":{"rendered":"
AWS SAM\u3092\u4f7f\u3046\u6a5f\u4f1a\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u3001AWS SAM\u3068Golang\u3067\u306eLambda\u95a2\u6570\u306e\u958b\u767a\u74b0\u5883\u3092\u7d39\u4ecb\u3044\u305f\u3057\u307e\u3059\u3002<\/p>\n
\u307e\u305a\u306fAWS SAM\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb( \u4eca\u56de\u306f\u3001API Gateway\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3092\u30a4\u30d9\u30f3\u30c8\u3068\u3057\u3001\u30b7\u30f3\u30d7\u30eb\u306b\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u3060\u3051\u306e\u95a2\u6570\u3092\u4f5c\u6210\u3057\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059( \u3053\u3053\u3067\u5b9a\u7fa9\u3057\u305f \u5168\u4f53\u7684\u306bDocker\u306b\u3088\u308b\u30b3\u30f3\u30c6\u30ca\u74b0\u5883\u3092\u60f3\u5b9a\u3057\u3066\u304a\u308a\u3001 \u57fa\u672c\u7684\u306b\u306f \u30a4\u30e1\u30fc\u30b8\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306bCLI\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3060\u3051\u306e\u30b7\u30f3\u30d7\u30eb\u306a\u3082\u306e\u3067\u3059\u3002<\/p>\n SAM Local\u3067\u306f\u3001\u30d3\u30eb\u30c9\u5f8c\u306e\u751f\u6210\u7269\u3092\u30db\u30b9\u30c8\u5074\u304b\u3089\u30b3\u30f3\u30c6\u30ca\u5074\u306b\u30de\u30a6\u30f3\u30c8\u3059\u308b\u4ed5\u7d44\u307f\u306e\u305f\u3081\u3001Go\u5074\u3067\u306f\u3001\u30db\u30b9\u30c8\u5074\u306b\u751f\u6210\u7269\u3092\u6301\u3063\u3066\u304f\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n \u74b0\u5883\u304c\u3067\u304d\u3066\u304d\u305f\u306e\u3067\u3001Go\u3067\u95a2\u6570\u306e\u5b9f\u88c5\u3092\u66f8\u3044\u3066\u3086\u304d\u307e\u3059\u3002\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u3060\u3051\u306e\u95a2\u6570\u306a\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u6e96\u5099\u304c\u3067\u304d\u305f\u306e\u3067\u3001\u95a2\u6570\u3092\u8d77\u52d5\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n \u4e0a\u8a18\u306e\u3088\u3046\u306b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u8d77\u52d5\u3057\u3066\u3001SAM Local\u304c\u52d5\u3044\u3066\u304f\u308c\u307e\u3059\u3002API Gateway\u3078\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u30c8\u30ea\u30ac\u30fc\u3068\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u4ee5\u4e0b\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001 \u4e00\u65b9\u3001SAM Local\u306f\u3001SAM Local\u306eDocker\u30d7\u30ed\u30bb\u30b9\u3092\u7acb\u3061\u4e0a\u3052\u3066\u3001\u305d\u306e\u4e2d\u3067Lambda\u306e\u30a4\u30e1\u30fc\u30b8(\u4eca\u56de\u3060\u3068 \u4e0a\u8a18\u306f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u52d5\u4f5c\u78ba\u8a8d\u3068\u3057\u3066\u306e\u30d5\u30ed\u30fc\u3067\u3059\u304c\u3001\u30c6\u30b9\u30c8\u3092\u66f8\u3051\u3070\u3001\u666e\u901a\u306bGo\u306e\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3082\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/p>\n \u74b0\u5883\u5909\u6570\u306f\u3001Git\u3067\u8ffd\u308f\u306a\u3044 \u305f\u3060\u3057\u3001\u30c7\u30d7\u30ed\u30a4\u6642\u306b\u3001 \u3053\u308c\u3067\u30c7\u30d7\u30ed\u30a4\u6642\u306b\u3001 \u203b Stage\u3092\u74b0\u5883\u5909\u6570\u3068\u3057\u3066\u6e21\u305b\u3070\u3001\u672c\u756a\u7528\/\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u7528\u306eLambda\u95a2\u6570\u3092\u5207\u308a\u5206\u3051\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n \u4ee5\u4e0a\u3001AWS SAM\u3068Golang\u3092\u4f7f\u7528\u3057\u3066Lambda\u95a2\u6570\u306e\u958b\u767a\u74b0\u5883\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n \u4ee5\u524d\u306f\u3042\u307e\u308a\u3067\u304d\u308b\u3053\u3068\u304c\u5c11\u306a\u304b\u3063\u305f\u5370\u8c61\u3067\u3059\u304c\u3001\u3044\u307e\u306fServerless\u306a\u3069\u3068\u305d\u3053\u307e\u3067\u5927\u5dee\u304c\u306a\u304f\u306a\u3063\u3066\u304d\u305f\u306e\u3067\u306f\u306a\u3044\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002\u3080\u3057\u308dCloudFormation\u3067\u7d30\u304b\u3044\u8a2d\u5b9a\u304c\u3067\u304d\u308b\u5206\u3001\u4eca\u5f8c\u306fAWS SAM\u3092\u4f7f\u3046\u3053\u3068\u304c\u5897\u3048\u305d\u3046\u3067\u3059\u3002<\/p>\n \u53c2\u8003\u306b\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n \u306a\u304a\u3001\u4ee5\u4e0b\u306e\u307a\u30fc\u30b8\u3067MMM\u306eAWS Lambda\u3078\u306e\u53d6\u308a\u7d44\u307f\u3092\u3054\u7d39\u4ecb\u3057\u3066\u3044\u307e\u3059\u3002\u305c\u3072\u5408\u308f\u305b\u3066\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\ntemplate.yml<\/code>)\u304b\u3089\u4f5c\u6210\u3057\u3066\u3086\u304d\u307e\u3059\u3002<\/p>\n
Parameters<\/code>\u306b\u95a2\u3057\u3066\u306f\u5f8c\u306b\u8aac\u660e\u3057\u307e\u3059)\u3002<\/p>\n
AWSTemplateFormatVersion: 2010-09-09\nTransform: AWS::Serverless-2016-10-31\n\nParameters:\n ProjectName:\n Type: String\n Stage:\n Type: String\n AllowedValues:\n - prod\n - stg\n Default: stg\n\nResources:\n HelloFunction:\n Type: AWS::Serverless::Function\n Properties:\n FunctionName: !Join [ "-", [ !Ref ProjectName, !Ref Stage, HelloFunction ] ]\n AutoPublishAlias: !Ref Stage\n Handler: main\n Runtime: go1.x\n Tracing: Active\n Environment:\n Variables:\n Stage: !Ref Stage\n Events:\n GetEvent:\n Type: Api\n Properties:\n Path: \/\n Method: post<\/code><\/pre>\n
HelloFunction<\/code>\u3092\u30c7\u30d7\u30ed\u30a4\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3086\u304d\u307e\u3059\u3002<\/p>\n
\u30b3\u30f3\u30c6\u30ca\u74b0\u5883<\/h2>\n
docker-compose.yml<\/code>\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
version: '3'\nservices:\n sam-local:\n build: .\n command: .\/start-sam.sh\n ports:\n - '3000:3000'\n volumes:\n - .:\/var\/opt\/\n - \/var\/run\/docker.sock:\/var\/run\/docker.sock # AWS SAM Local\u3092docker-compose\u3068\u4f7f\u3046\u3068\u304d\u306b\u5fc5\u8981\n depends_on:\n - db\n - go\n environment:\n - VOLUME=$PWD\n env_file:\n - .env # \u74b0\u5883\u5909\u6570\u3092\u30b3\u30f3\u30c6\u30ca\u5185\u3067\u8aad\u307f\u8fbc\u307f\n\n go:\n command: .\/gobuild.sh\n build:\n context: .\/\n dockerfile: .\/Dockerfile_go\n volumes:\n - .:\/go\/src\/your-project\/\n\n db:\n environment:\n - MYSQL_ROOT_PASSWORD=docker\n - MYSQL_PASSWORD=docker\n - MYSQL_USER=docker\n - MYSQL_DATABASE=reportdb\n build: .\/docker\/mysql\n ports:\n - "3306:3306"<\/code><\/pre>\n
sam-local<\/code>\u30b3\u30f3\u30c6\u30ca\u3092\u7acb\u3061\u4e0a\u3052\u3066\u305d\u3053\u3067\u52d5\u4f5c\u78ba\u8a8d\u3001
go<\/code>\u30b3\u30f3\u30c6\u30ca\u306f\u30d3\u30eb\u30c9\u307e\u3067\u3092\u8cac\u52d9\u3068\u3057\u3066\u884c\u3063\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n
FROM alpine\n\nENV SAM_CLI_VERSION=0.3.0 \n PYTHONUSERBASE=\/usr\/local\n\nRUN apk add --no-cache py-pip git bash && \n pip install --user aws-sam-cli==${SAM_CLI_VERSION} awscli\n\nWORKDIR \/var\/opt\nCOPY . \/var\/opt\/\n\nEXPOSE 3000<\/code><\/pre>\n
FROM golang:1.10.2-alpine\n\nRUN apk add --no-cache git bash && \n go get -u github.com\/golang\/dep\/cmd\/dep && \n go get -u github.com\/golang\/lint\/golint\n\nWORKDIR \/go\/src\/your-project\/\nCOPY . \/go\/src\/your-project\/\nRUN dep ensure\n\nCMD CGO_ENABLED=0 GOOS=linux go build -v -a -installsuffix cgo -o .\/main .\/main.go<\/code><\/pre>\n
gobuild.sh<\/code><\/p>\n
#!\/bin\/sh\ndep ensure # \u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\u306fdep\u3092\u4f7f\u7528\nCGO_ENABLED=0 GOOS=linux go build -v -a -installsuffix cgo -o .\/main .\/main.go<\/code><\/pre>\n
start-sam.sh<\/code><\/p>\n
#!\/bin\/sh\nset -e\n\nuntil ls -l \/var\/opt\/main; do\n >&2 echo "go build is not done. - sleeping" # Go\u306e\u30d3\u30eb\u30c9\u304c\u7d42\u308f\u308b\u307e\u3067\u5f85\u6a5f\n sleep 2\ndone\n\n>&2 echo "go build is done - executing command"\nenv | sort\nsam local start-api --docker-volume-basedir "${VOLUME}\/" --host 0.0.0.0 --template template.yml # \u30db\u30b9\u30c8\u5074\u306e\u30d5\u30a1\u30a4\u30eb\u4e00\u5f0f\u3092\u30b3\u30f3\u30c6\u30ca\u306b\u30de\u30a6\u30f3\u30c8\u3057\u3064\u3064\u3001SAM Local\u8d77\u52d5<\/code><\/pre>\n
Go\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3<\/h2>\n
package main\n\nimport (\n "os"\n "log"\n "github.com\/aws\/aws-lambda-go\/events"\n "github.com\/aws\/aws-lambda-go\/lambda"\n)\n\nfunc Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {\n\n log.Printf("Processing Lambda request %s\n", request.RequestContext.RequestID)\n\n return events.APIGatewayProxyResponse{\n Body: "Hello " + request.Body,\n Headers: map[string]string{ "x-custom-header" : "my custom header value" },\n StatusCode: 200,\n }, nil\n\n}\n\nfunc main() {\n lambda.Start(Handler)\n}<\/code><\/pre>\n
\u95a2\u6570\u306e\u8d77\u52d5<\/h2>\n
docker-compose build\ndocker-compose up\n# (\u7701)\nsam-local_1 | 2018-06-06 08:19:51 Mounting HelloFunction at http:\/\/0.0.0.0:3000\/ [POST]\nsam-local_1 | 2018-06-06 08:19:51 You can now browse to the above endpoints to invoke your functions. You do not need to restart\/reload SAM CLI while working on your functions changes will be reflected instantly\/automatically. You only need to restart SAM CLI if you update your AWS SAM template\nsam-local_1 | 2018-06-06 08:19:51 * Running on http:\/\/0.0.0.0:3000\/ (Press CTRL+C to quit)<\/code><\/pre>\n
Hello Paul<\/code>\u3068\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u8fd4\u3063\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n
curl -H 'Content-Type:application\/json' http:\/\/localhost:3000 -X POST -d "Paul"\n\nHello Paul<\/code><\/pre>\n
lambci\/lambda:go1.x<\/code>)\u3092\u5229\u7528\u3057\u3066\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u4ed5\u7d44\u307f\u3067\u3059\u306e\u3067\u3001\u30b3\u30f3\u30c6\u30ca\u5074\u3067\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30ed\u30b0\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n
sam-local_1 | 2018-06-08 08:55:18 Invoking main (go1.x)\nsam-local_1 | 2018-06-08 08:55:18 Found credentials in environment variables.\nsam-local_1 |\nsam-local_1 | Fetching lambci\/lambda:go1.x Docker container image......\nsam-local_1 | 2018-06-08 08:55:23 Mounting \/Users\/prop\/go\/src\/your-project as \/var\/task:ro inside runtime container\nsam-local_1 | START RequestId: aa4db6b2-37ef-1628-cadc-3390123c9a16 Version: $LATEST\nsam-local_1 | 2018\/06\/08 08:55:24 Processing Lambda request c6af9ac6-7b61-11e6-9a41-93e8deadbeef\nsam-local_1 | END RequestId: aa4db6b2-37ef-1628-cadc-3390123c9a16\nsam-local_1 | REPORT RequestId: aa4db6b2-37ef-1628-cadc-3390123c9a16 Duration: 8.44 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 5 MB\nsam-local_1 | 2018-06-08 08:55:24 No Content-Type given. Defaulting to 'application\/json'.\nsam-local_1 | 2018-06-08 08:55:24 172.26.0.1 - - [08\/Jun\/2018 08:55:24] "POST \/ HTTP\/1.1" 200 -<\/code><\/pre>\n
\u30c6\u30b9\u30c8<\/h2>\n
package main\n\nimport (\n "testing"\n "github.com\/aws\/aws-lambda-go\/events"\n "github.com\/stretchr\/testify\/assert"\n)\n\nfunc TestHandler(t *testing.T) {\n tests := []struct {\n request events.APIGatewayProxyRequest\n expect string\n err error\n }{\n {\n request: events.APIGatewayProxyRequest{Body: "Paul"},\n expect: "Hello Paul",\n err: nil,\n },\n }\n\n for _, test := range tests {\n response, err := Handler(test.request)\n assert.IsType(t, test.err, err)\n assert.Equal(t, test.expect, response.Body)\n }\n}<\/code><\/pre>\n
docker-compose run go go test\nPASS\nok your-project 0.016s<\/code><\/pre>\n
\u74b0\u5883\u5909\u6570\u306e\u8aad\u307f\u8fbc\u307f<\/h2>\n
.env<\/code>\u30d5\u30a1\u30a4\u30eb\u3092\u7528\u610f\u3057\u3001\u305d\u308c\u3092\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u5185\u3067\u8aad\u307f\u8fbc\u3080\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
template.yml<\/code>\u5185\u3067\u74b0\u5883\u5909\u6570\u3092\u8aad\u307f\u8fbc\u3080\u305f\u3081\u306b\u3001
Parameters<\/code>\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002
template.yml<\/code>\u5185\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304f\u3053\u3068\u3067\u3001
!Ref Stage<\/code>\u3068\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n
Parameters:\n ProjectName:\n Type: String\n Stage:\n Type: String\n AllowedValues:\n - prod\n - stg\n Default: stg<\/code><\/pre>\n
--parameter-overrides<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u74b0\u5883\u5909\u6570\u3092\u6e21\u3057\u3066\u3084\u308c\u3070OK\u3067\u3059\u3002\u30c7\u30d7\u30ed\u30a4\u306e\u30b3\u30de\u30f3\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
sam deploy \n --template-file .\/packaged.yml \n --stack-name $StackName \n --capabilities CAPABILITY_IAM \n --no-fail-on-empty-changeset \n --parameter-overrides $(cat .env | tr '\n' ' ')<\/code><\/pre>\n
--parameter-overrides<\/code>\u3067
$(cat .env | tr '
\n' ' ')<\/code>\u3092\u6307\u5b9a\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u74b0\u5883\u5909\u6570\u3092\u4e88\u3081CircleCI(CI\/CD\u306fCircleCI\u60f3\u5b9a\u3067\u3059\u2026)\u3067\u8a2d\u5b9a\u3057\u3066\u304a\u304d\u3001\u305d\u3053\u304b\u3089.env<\/code>\u30d5\u30a1\u30a4\u30eb\u3092\u52d5\u7684\u306b\u751f\u6210\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u3059\u3002<\/p>\n
\u307e\u3068\u3081<\/h2>\n