前回の続きで Java の Lambda をコンテナで動かすメモ。
以下の Usage に従う。
イメージの作成
ソースは前回のものを流用する。
You can use maven-dependency-plugin to collect runtime dependencies:
のところに書かれているプラグインを追加する。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Maven のようのサンプルの通りに Dockerfile を作成する。ハンドラーのクラス名はアプリケーションに合わせる。
FROM public.ecr.aws/lambda/java:11
# Copy function code and runtime dependencies from Maven layout
COPY target/classes ${LAMBDA_TASK_ROOT}
COPY target/dependency/* ${LAMBDA_TASK_ROOT}/lib/
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "example.Handler::handleRequest" ]
依存関係を集める。
mvn compile dependency:copy-dependencies -DincludeScope=runtime
target/dependency
に依存関係の jar が集められる。
イメージをビルドする。
docker build -t lambda-java-example .
イメージを確認する。
$ docker images lambda-java-example
REPOSITORY TAG IMAGE ID CREATED SIZE
lambda-java-example latest ef8b4bafb132 About a minute ago 521MB
ローカルでテストする。
docker run -p 9000:8080 lambda-java-example
別のターミナルから curl する。動いた。
$ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
"200 OK"%
ECR に Push
リポジトリを作成する。
aws ecr create-repository --repository-name lambda-java-example
ECR にログインする。
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
AWS_REGION=$(aws configure get region)
aws ecr get-login-password | docker login --username AWS --password-stdin https://${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
タグ付けして Push する。
docker tag lambda-java-example ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/lambda-java-example
docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/lambda-java-example
Lambda 関数のデプロイ
マネコンからポチポチでデプロイする。関数を作成する。
ECR に置いたイメージを指定する。
テストを実行する。
ログ出力は以下。
START RequestId: 635c8316-8e65-4a3f-878c-21379e3449a1 Version: $LATEST
ENVIRONMENT VARIABLES: {
"PATH": "/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin",
"_AWS_XRAY_DAEMON_ADDRESS": "169.254.79.129",
"LAMBDA_TASK_ROOT": "/var/task",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "128",
"TZ": ":/etc/localtime",
"AWS_SECRET_ACCESS_KEY": "snip",
"AWS_DEFAULT_REGION": "ap-northeast-1",
"AWS_EXECUTION_ENV": "AWS_Lambda_java11",
"AWS_LAMBDA_LOG_GROUP_NAME": "/aws/lambda/my-java-container-function",
"_HANDLER": "example.Handler::handleRequest",
"LANG": "en_US.UTF-8",
"LAMBDA_RUNTIME_DIR": "/var/runtime",
"AWS_SESSION_TOKEN": "snip",
"AWS_ACCESS_KEY_ID": "snip",
"LD_LIBRARY_PATH": "/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib",
"_X_AMZN_TRACE_ID": "Root\u003d1-627e2882-5cd481374a239ec47fa28425;Parent\u003d5cfb039c3b620232;Sampled\u003d0",
"AWS_SECRET_KEY": "snip",
"PWD": "/var/task",
"AWS_LAMBDA_RUNTIME_API": "127.0.0.1:9001",
"AWS_REGION": "ap-northeast-1",
"AWS_LAMBDA_LOG_STREAM_NAME": "2022/05/13/[$LATEST]73be34ef59f24517b4c71e518ef27d37",
"AWS_LAMBDA_INITIALIZATION_TYPE": "on-demand",
"AWS_XRAY_DAEMON_ADDRESS": "169.254.79.129:2000",
"_AWS_XRAY_DAEMON_PORT": "2000",
"AWS_XRAY_CONTEXT_MISSING": "LOG_ERROR",
"AWS_LAMBDA_FUNCTION_VERSION": "$LATEST",
"AWS_ACCESS_KEY": "snip",
"SHLVL": "0",
"AWS_LAMBDA_FUNCTION_NAME": "my-java-container-function"
}CONTEXT: {
"memoryLimit": 128,
"awsRequestId": "635c8316-8e65-4a3f-878c-21379e3449a1",
"logGroupName": "/aws/lambda/my-java-container-function",
"logStreamName": "2022/05/13/[$LATEST]73be34ef59f24517b4c71e518ef27d37",
"functionName": "my-java-container-function",
"functionVersion": "$LATEST",
"invokedFunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:my-java-container-function",
"deadlineTimeInMs": 1652435079657,
"logger": {}
}EVENT: {
"key1": "value1",
"key2": "value2",
"key3": "value3"
END RequestId: 635c8316-8e65-4a3f-878c-21379e3449a1
REPORT RequestId: 635c8316-8e65-4a3f-878c-21379e3449a1 Duration: 804.33 ms Billed Duration: 2186 ms Memory Size: 128 MB Max Memory Used: 83 MB Init Duration: 1381.58 ms