3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Go 言語を学ぶAdvent Calendar 2023

Day 5

aws lambdaカスタムランタイムでGoを動かす

Posted at

aws lambdaでGo

アドベントカレンダー5日目です。
Goはaws lambdaで正式にサポートされていましたが、Amazon Linux AMI のメンテナンスサポートが 2023 年 12 月 31 日に終了するに伴い、Go 用ランタイムもサポートが終了してしまいます。
https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/lambda-golang.html

そのため、これからはprovided.al2イメージ上に作成する必要があります。

Dockerfile作成

touch app/day5/Dockerfile
touch app/day5/main.go

day5を独立したモジュールとしたいため、初期化しておきます。

cd app/day5
got mod init github.com/shuyaeer/learn-go/app/day5

Dockerfileです。ビルドしたバイナリをprovided:al2上で実行しています。

FROM golang:latest as build

WORKDIR /app

COPY go.mod go.sum ./

COPY main.go .
RUN go build -tags lambda.norpc -o main main.go

FROM public.ecr.aws/lambda/provided:al2
COPY --from=build /app/main ./main
ENTRYPOINT [ "./main" ]

メインのプログラムです。シンプルに文字列を返すものにしました。

package main

import (
	"context"

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

func handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	response := events.APIGatewayProxyResponse{
		StatusCode: 200,
		Body:       "\"Hello from Lambda!\"",
	}
	return response, nil
}

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

ビルド

docker build -t lambda-go-sample:test .

ローカルでlambdaを実行するためのエミュレータをインストール

mkdir aws-lambda-rie

curl -Lo aws-lambda-rie/aws-lambda-rie \
 https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie

chmod +x aws-lambda-rie/aws-lambda-rie

コンテナ起動

 $ docker run -v `pwd`/aws-lambda-rie:/aws-lambda \ 
   -p 9000:8080 --entrypoint /aws-lambda/aws-lambda-rie \
  lambda-go-sample:test ./main
  05 Dec 2023 14:52:33,937 [INFO] (rapid) exec './main' (cwd=/var/task, handler=)
05 Dec 2023 14:52:59,385 [INFO] (rapid) INIT START(type: on-demand, phase: init)
05 Dec 2023 14:52:59,390 [INFO] (rapid) The extension's directory "/opt/extensions" does not exist, assuming no extensions to be loaded.
START RequestId: cf4d9fad-94cd-4731-aa8c-a38f774d384d Version: $LATEST
05 Dec 2023 14:52:59,394 [INFO] (rapid) Starting runtime without AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN , Expected?: false
05 Dec 2023 14:52:59,475 [INFO] (rapid) INIT RTDONE(status: success)
05 Dec 2023 14:52:59,475 [INFO] (rapid) INIT REPORT(durationMs: 92.176000)
05 Dec 2023 14:52:59,477 [INFO] (rapid) INVOKE START(requestId: c97ef940-dce9-4b25-881a-dbb5eee68382)
05 Dec 2023 14:52:59,525 [INFO] (rapid) INVOKE RTDONE(status: success, produced bytes: 0, duration: 46.652000ms)

別ターミナルからリクエストを送ってみます。

 curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    92  100    90  100     2   4754    105 --:--:-- --:--:-- --:--:--  6571
{
  "statusCode": 200,
  "headers": null,
  "multiValueHeaders": null,
  "body": "\"Hello from Lambda!\""
}

よさそうです。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?