web-technical-blog

web開発に関する技術メモ

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

AWS CLIでデプロイ(Windows環境)

$ GOOS=linux go build -o main
$ zip deployment.zip main
$ aws lambda create-function \
--region us-west-2 \
--function-name HelloFunction \
--zip-file fileb://./deployment.zip \
--runtime go1.x \
--tracing-config Mode=Active \
--role arn:aws:iam::account_id:role/role_name \
--handler main

aws lambda create-functionのオプション

https://docs.aws.amazon.com/cli/latest/reference/lambda/create-function.html

depoly方法

https://github.com/aws/aws-lambda-go https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/deploying-lambda-apps.html


AWS SAMを利用してLambdaをデプロイする(Mac環境)

  • template.ymlのroleは指定する

テンプレート

$ cat 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 # ビルドファイルの設置ディレクトリを設定
      Role: arn:aws:iam::account_id:role/role_name # Roleを設定する
      Timeout: 1

デプロイ

$ GOARCH=amd64 GOOS=linux go build -o build/lambda-go-sample

$ aws cloudformation package \
    --template-file template.yml \
    --s3-bucket <bucket-name> \
    --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

実行

$ aws lambda invoke --function-name lambda-go-sample-App-xxxx --payload '"Lambda"' out.txt

https://qiita.com/ikeisuke/items/3c0c422888ae8ae09831