Golang で作った関数を AWS Lambda で動かす
package main
import (
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
type Request struct {
Value string `json:"value"`
}
type Response struct {
Message string `json:"message"`
}
func echo(event Request) (Response, error) {
return Response{Message: fmt.Sprintf("Request value is %s", event.Value)}, nil
}
func main() {
lambda.Start(echo)
}
go.mod 作成します
go mod init main
$ go mod init main
go: creating new go.mod: module main
go: to add module requirements and sums:
go mod tidy
モジュールをダウンロードします
go mod tidy
コンパイルします
GOOS=linux GOARCH=amd64 go build -o echo
zip ファイルにします
zip handler.zip ./echo
ファイルの構成は下記のようになります
$ ls
echo echo.go go.mod go.sum handler.zip
AWS Lambda 関数の作成
Lambda関数を作成します。

一から作成を選択。
関数名に echo を入力。
ランタイムは Go 1.x を選択。
関数の作成をクリック。

関数が作成されました。次にコードソースをアップロードします。
アップロード元から .zipファイルを選択します。

アップロードをクリックしファイルを選択し、ファイルをアップロードします。
保存をクリックします。

コードがアップロードされたことを確認します。
ランタイム設定を編集します。

ハンドラを echo へ書き換え、保存します。

テストタブをクリックし、下記のようにイベントJSONを編集します。
テストをクリックします。

実行結果が成功になることを確認します。

詳細を開くと、実際の結果を確認することができます。
