36
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS SAMを利用してGolangなLambdaをデプロイする

Last updated at Posted at 2018-01-16

AWS LambdaがGoのサポートを開始したのでAWSSAMを利用してのデプロイを試してみました

実行環境

$ go version
go version go1.9.2 darwin/amd64
$ aws --version
aws-cli/1.11.149 Python/3.4.2 Darwin/17.3.0 botocore/1.7.7

ソースコード

Lambda Function Handler (Go)のコードをそのまま利用します。

main.go
package main

import (
	"context"
	"fmt"

	"github.com/aws/aws-lambda-go/lambda"
)

func HandleRequest(ctx context.Context, name string) (string, error) {
	return fmt.Sprintf("Hello %s!", name), nil
}

func main() {
	lambda.Start(HandleRequest)
}

テンプレート

template.yml
AWSTemplateFormatVersion: "2010-09-09"
Transform: 'AWS::Serverless-2016-10-31'
Resources:
  App:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda-go-sample # ファイル名
      Runtime: go1.x 
      CodeUri: build # ビルドファイル設置ディレクトリ
      Timeout: 1

デプロイ

GOARCH=amd64 GOOS=linux go build -o build/lambda-go-sample
aws cloudformation package \
	--template-file template.yml \
	--s3-bucket [パッケージアップロード先s3バケット] \
	--s3-prefix lambda-go-sample \
	--output-template-file .template.yml
aws cloudformation deploy \
	--template-file .template.yml \
	--stack-name lambda-go-sample \
	--capabilities CAPABILITY_IAM

実行結果

$ aws cloudformation describe-stack-resources --stack-name lambda-go-sample
{
    "StackResources": [
        {
            "Timestamp": "2018-01-16T02:42:50.589Z",
            "StackName": "lambda-go-sample",
            "LogicalResourceId": "App",
            "ResourceType": "AWS::Lambda::Function",
            "ResourceStatus": "CREATE_COMPLETE",
            "PhysicalResourceId": "lambda-go-sample-App-1U7U6E46GZAW3",
            "StackId": "arn:aws:cloudformation:ap-northeast-1:xxx:stack/lambda-go-sample/de118db0-fa66-11e7-b9ae-50fa13f400ad"
        },
        {
            "Timestamp": "2018-01-16T02:42:45.996Z",
            "StackName": "lambda-go-sample",
            "LogicalResourceId": "AppRole",
            "ResourceType": "AWS::IAM::Role",
            "ResourceStatus": "CREATE_COMPLETE",
            "PhysicalResourceId": "lambda-go-sample-AppRole-WVOATNOP8JWV",
            "StackId": "arn:aws:cloudformation:ap-northeast-1:xxx:stack/lambda-go-sample/de118db0-fa66-11e7-b9ae-50fa13f400ad"
        }
    ]
}
$ aws lambda invoke --function-name lambda-go-sample-App-1U7U6E46GZAW3 --payload '"Lambda"' out.txt
{
    "StatusCode": 200
}
$ cat out.txt
"Hello Lambda!"

あとがき

pythonやnodeと違って事前にビルドしたものをデプロイする必要がありますが、CD環境さえ整えてしまえば些細な問題ですね。
次はGitHub + CodePipeline + CodeBuild + CloudFormationによるGolangなAWS LambdaのCD環境の構築方法についてまとめたいと思います。

参考

36
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?