AWS SAMで好きなベースイメージを使用したコンテナLambdaでPythonを動作させる

概要

先の記事でコンテナLambdaを実行できるようになりました。

AWS SAMでコンテナLambdaを作成する

ですが、ローカルでいろいろ触っている開発環境がUbuntuなので、できれば同じものを使いたいと思い、Ubuntu 22.04のコンテナイメージを使用してLambdaのHello Worldを実行させてみます。

AWSのLambda用に提供されているコンテナイメージの内容

ECS用なのかもしれませんが、先の記事で実施した手順で作成したSAMのDockerfileでは、
下記のコンテナイメージを使用しています。

1
FROM public.ecr.aws/lambda/python:3.9

このOSを調べたところAmazon Linux 2のようでした。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ uname -a
Linux ddb82d29db91 5.15.0-67-generic #74-Ubuntu SMP Wed Feb 22 14:14:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
VARIANT_ID="202302240956-2.0.1033.0"

sam-app/hello_world/Dockerfile の中身は以下のとおりです。

1
2
3
4
5
6
7
8
FROM public.ecr.aws/lambda/python:3.9

COPY app.py requirements.txt ./

RUN python3.9 -m pip install -r requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]

Ubuntuのコンテナイメージに変更する

sam-app/hello_world/Dockerfile のFROMをUbuntuのものに変更してみます。

1
2
3
4
5
6
7
8
9
- FROM public.ecr.aws/lambda/python:3.9
+ FROM ubuntu:22.04

COPY app.py requirements.txt ./

RUN python3.9 -m pip install -r requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]

ちなみに、FROM ubuntu:22.04 は、以下の内容のコンテナイメージです。

1
2
3
4
5
6
7
8
9
10
11
12
PRETTY_NAME="Ubuntu 22.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.2 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

以下のように出力され、sam buildが失敗しました。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ sam build
Building image for HelloWorldFunction function
Setting DockerBuildArgs: {} for HelloWorldFunction function
Step 1/4 : FROM ubuntu:22.04
22.04: Pulling from library/ubuntu
76769433fd8a: Pull complete
Status: Downloaded newer image for ubuntu:22.04 ---> 74f2314a03de
Step 2/4 : COPY app.py requirements.txt ./
---> 37401066b32b
Step 3/4 : RUN python3.9 -m pip install -r requirements.txt -t .
---> Running in 417b8d8b6715
/bin/sh: 1: python3.9: not found

Build Failed
Error: HelloWorldFunction failed to build: The command '/bin/sh -c python3.9 -m pip install -r requirements.txt -t .' returned a non-zero code: 127

UbuntuのイメージにはPython3.9が入っていないようなので、
Dockerfile を少し修正して中身を出力させてみます。

1
2
3
4
5
6
7
8
9
FROM ubuntu:22.04

COPY app.py requirements.txt ./

+ RUN apt list --installed
- RUN python3.9 -m pip install -r requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]

sam build するとDocker Buildのログに以下のように出力されます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Step 3/4 : RUN apt list --installed
---> Running in a0c4c4c28918

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Listing...
adduser/now 3.118ubuntu5 all [installed,local]
apt/now 2.4.8 amd64 [installed,local]
base-files/now 12ubuntu4.3 amd64 [installed,local]
base-passwd/now 3.5.52build1 amd64 [installed,local]
bash/now 5.1-6ubuntu1 amd64 [installed,local]
bsdutils/now 1:2.37.2-4ubuntu3 amd64 [installed,local]
coreutils/now 8.32-4.1ubuntu1 amd64 [installed,local]
dash/now 0.5.11+git20210903+057cd650a4ed-3build1 amd64 [installed,local]
debconf/now 1.5.79ubuntu1 all [installed,local]
debianutils/now 5.5-1ubuntu2 amd64 [installed,local]
diffutils/now 1:3.8-0ubuntu2 amd64 [installed,local]
dpkg/now 1.21.1ubuntu2.1 amd64 [installed,local]
e2fsprogs/now 1.46.5-2ubuntu1.1 amd64 [installed,local]
findutils/now 4.8.0-1ubuntu3 amd64 [installed,local]
gcc-12-base/now 12.1.0-2ubuntu1~22.04 amd64 [installed,local]
gpgv/now 2.2.27-3ubuntu2.1 amd64 [installed,local]
grep/now 3.7-1build1 amd64 [installed,local]
gzip/now 1.10-4ubuntu4.1 amd64 [installed,local]
hostname/now 3.23ubuntu2 amd64 [installed,local]
init-system-helpers/now 1.62 all [installed,local]
libacl1/now 2.3.1-1 amd64 [installed,local]
libapt-pkg6.0/now 2.4.8 amd64 [installed,local]
libattr1/now 1:2.5.1-1build1 amd64 [installed,local]
libaudit-common/now 1:3.0.7-1build1 all [installed,local]
libaudit1/now 1:3.0.7-1build1 amd64 [installed,local]
libblkid1/now 2.37.2-4ubuntu3 amd64 [installed,local]
libbz2-1.0/now 1.0.8-5build1 amd64 [installed,local]
libc-bin/now 2.35-0ubuntu3.1 amd64 [installed,local]
libc6/now 2.35-0ubuntu3.1 amd64 [installed,local]
libcap-ng0/now 0.7.9-2.2build3 amd64 [installed,local]
libcap2/now 1:2.44-1build3 amd64 [installed,local]
libcom-err2/now 1.46.5-2ubuntu1.1 amd64 [installed,local]
libcrypt1/now 1:4.4.27-1 amd64 [installed,local]
libdb5.3/now 5.3.28+dfsg1-0.8ubuntu3 amd64 [installed,local]
libdebconfclient0/now 0.261ubuntu1 amd64 [installed,local]
libext2fs2/now 1.46.5-2ubuntu1.1 amd64 [installed,local]
libffi8/now 3.4.2-4 amd64 [installed,local]
libgcc-s1/now 12.1.0-2ubuntu1~22.04 amd64 [installed,local]
libgcrypt20/now 1.9.4-3ubuntu3 amd64 [installed,local]
libgmp10/now 2:6.2.1+dfsg-3ubuntu1 amd64 [installed,local]
libgnutls30/now 3.7.3-4ubuntu1.2 amd64 [installed,local]
libgpg-error0/now 1.43-3 amd64 [installed,local]
libgssapi-krb5-2/now 1.19.2-2ubuntu0.1 amd64 [installed,local]
libhogweed6/now 3.7.3-1build2 amd64 [installed,local]
libidn2-0/now 2.3.2-2build1 amd64 [installed,local]
libk5crypto3/now 1.19.2-2ubuntu0.1 amd64 [installed,local]
libkeyutils1/now 1.6.1-2ubuntu3 amd64 [installed,local]
libkrb5-3/now 1.19.2-2ubuntu0.1 amd64 [installed,local]
libkrb5support0/now 1.19.2-2ubuntu0.1 amd64 [installed,local]
liblz4-1/now 1.9.3-2build2 amd64 [installed,local]
liblzma5/now 5.2.5-2ubuntu1 amd64 [installed,local]
libmount1/now 2.37.2-4ubuntu3 amd64 [installed,local]
libncurses6/now 6.3-2 amd64 [installed,local]
libncursesw6/now 6.3-2 amd64 [installed,local]
libnettle8/now 3.7.3-1build2 amd64 [installed,local]
libnsl2/now 1.3.0-2build2 amd64 [installed,local]
libp11-kit0/now 0.24.0-6build1 amd64 [installed,local]
libpam-modules-bin/now 1.4.0-11ubuntu2.3 amd64 [installed,local]
libpam-modules/now 1.4.0-11ubuntu2.3 amd64 [installed,local]
libpam-runtime/now 1.4.0-11ubuntu2.3 all [installed,local]
libpam0g/now 1.4.0-11ubuntu2.3 amd64 [installed,local]
libpcre2-8-0/now 10.39-3ubuntu0.1 amd64 [installed,local]
libpcre3/now 2:8.39-13ubuntu0.22.04.1 amd64 [installed,local]
libprocps8/now 2:3.3.17-6ubuntu2 amd64 [installed,local]
libseccomp2/now 2.5.3-2ubuntu2 amd64 [installed,local]
libselinux1/now 3.3-1build2 amd64 [installed,local]
libsemanage-common/now 3.3-1build2 all [installed,local]
libsemanage2/now 3.3-1build2 amd64 [installed,local]
libsepol2/now 3.3-1build1 amd64 [installed,local]
libsmartcols1/now 2.37.2-4ubuntu3 amd64 [installed,local]
libss2/now 1.46.5-2ubuntu1.1 amd64 [installed,local]
libssl3/now 3.0.2-0ubuntu1.8 amd64 [installed,local]
libstdc++6/now 12.1.0-2ubuntu1~22.04 amd64 [installed,local]
libsystemd0/now 249.11-0ubuntu3.6 amd64 [installed,local]
libtasn1-6/now 4.18.0-4build1 amd64 [installed,local]
libtinfo6/now 6.3-2 amd64 [installed,local]
libtirpc-common/now 1.3.2-2ubuntu0.1 all [installed,local]
libtirpc3/now 1.3.2-2ubuntu0.1 amd64 [installed,local]
libudev1/now 249.11-0ubuntu3.6 amd64 [installed,local]
libunistring2/now 1.0-1 amd64 [installed,local]
libuuid1/now 2.37.2-4ubuntu3 amd64 [installed,local]
libxxhash0/now 0.8.1-1 amd64 [installed,local]
libzstd1/now 1.4.8+dfsg-3build1 amd64 [installed,local]
login/now 1:4.8.1-2ubuntu2.1 amd64 [installed,local]
logsave/now 1.46.5-2ubuntu1.1 amd64 [installed,local]
lsb-base/now 11.1.0ubuntu4 all [installed,local]
mawk/now 1.3.4.20200120-3 amd64 [installed,local]
mount/now 2.37.2-4ubuntu3 amd64 [installed,local]
ncurses-base/now 6.3-2 all [installed,local]
ncurses-bin/now 6.3-2 amd64 [installed,local]
passwd/now 1:4.8.1-2ubuntu2.1 amd64 [installed,local]
perl-base/now 5.34.0-3ubuntu1.1 amd64 [installed,local]
procps/now 2:3.3.17-6ubuntu2 amd64 [installed,local]
sed/now 4.8-1ubuntu2 amd64 [installed,local]
sensible-utils/now 0.0.17 all [installed,local]
sysvinit-utils/now 3.01-1ubuntu1 amd64 [installed,local]
tar/now 1.34+dfsg-1ubuntu0.1.22.04.1 amd64 [installed,local]
ubuntu-keyring/now 2021.03.26 all [installed,local]
usrmerge/now 25ubuntu2 all [installed,local]
util-linux/now 2.37.2-4ubuntu3 amd64 [installed,local]
zlib1g/now 1:1.2.11.dfsg-2ubuntu9.2 amd64 [installed,local]

コンテナ用のイメージなので、余計なものは入れてない感じでしょうか、
PythonをインストールするようにDockerFileを修正します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM ubuntu:22.04

COPY app.py requirements.txt ./

RUN apt-get update \
&& apt install -y \
python3 \
python3-pip\
&& apt clean
RUN apt list --installed | grep python
RUN python3 --version
RUN pip3 --version
# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]

sam build してPython3がインストールされたことを確認します。
Python 3.10.6とpip 22.0.2がインストールされました。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Step 4/7 : RUN apt list --installed | grep python
---> Running in 18bda2c0bf99

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

libpython3-dev/jammy-updates,now 3.10.6-1~22.04 amd64 [installed,automatic]
libpython3-stdlib/jammy-updates,now 3.10.6-1~22.04 amd64 [installed,automatic]
libpython3.10-dev/jammy-updates,jammy-security,now 3.10.6-1~22.04.2 amd64 [installed,automatic]
libpython3.10-minimal/jammy-updates,jammy-security,now 3.10.6-1~22.04.2 amd64 [installed,automatic]
libpython3.10-stdlib/jammy-updates,jammy-security,now 3.10.6-1~22.04.2 amd64 [installed,automatic]
libpython3.10/jammy-updates,jammy-security,now 3.10.6-1~22.04.2 amd64 [installed,automatic]
python3-dev/jammy-updates,now 3.10.6-1~22.04 amd64 [installed,automatic]
python3-distutils/jammy-updates,now 3.10.6-1~22.04 all [installed,automatic]
python3-lib2to3/jammy-updates,now 3.10.6-1~22.04 all [installed,automatic]
python3-minimal/jammy-updates,now 3.10.6-1~22.04 amd64 [installed,automatic]
python3-pip/jammy-updates,jammy-security,now 22.0.2+dfsg-1ubuntu0.2 all [installed]
python3-pkg-resources/jammy-updates,jammy-security,now 59.6.0-1.2ubuntu0.22.04.1 all [installed,automatic]
python3-setuptools/jammy-updates,jammy-security,now 59.6.0-1.2ubuntu0.22.04.1 all [installed,automatic]
python3-wheel/jammy-updates,jammy-security,now 0.37.1-2ubuntu0.22.04.1 all [installed,automatic]
python3.10-dev/jammy-updates,jammy-security,now 3.10.6-1~22.04.2 amd64 [installed,automatic]
python3.10-minimal/jammy-updates,jammy-security,now 3.10.6-1~22.04.2 amd64 [installed,automatic]
python3.10/jammy-updates,jammy-security,now 3.10.6-1~22.04.2 amd64 [installed,automatic]
python3/jammy-updates,now 3.10.6-1~22.04 amd64 [installed]
Removing intermediate container 18bda2c0bf99
---> 927836e6ff31
Step 5/7 : RUN python3 --version
---> Running in 9aab0892f73c
Python 3.10.6
Removing intermediate container 9aab0892f73c
---> b15f71f093e8
Step 6/7 : RUN pip3 --version
---> Running in 70eb5869c8c1
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
Removing intermediate container 70eb5869c8c1

それでは、DockerFileを修正してLambdaのHello Worldをlocal invokeしてみます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM ubuntu:22.04

COPY app.py requirements.txt ./

RUN apt-get update \
&& apt install -y \
python3 \
python3-pip\
&& apt clean

RUN python3 -m pip install -r requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]

実行してみましたが、No response from invoke container…と出力されてしまい、正常に処理されませんでした。

1
2
3
4
5
6
7
8
9
$ sam local invoke
Invoking Container created from helloworldfunction:python3.9-v1
Building image.................
Using local image: helloworldfunction:rapid-x86_64.

Function 'HelloWorldFunction' timed out after 3 seconds
END RequestId: d6d8dfd6-4d74-4157-8238-a17ad333a970
REPORT RequestId: d6d8dfd6-4d74-4157-8238-a17ad333a970 Init Duration: 0.36 ms Duration: 3000.00 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 128 MB
No response from invoke container for HelloWorldFunction

awslambdaricを使用してPython用のコンテナイメージを作成する

色々調べてみたところ、AWSが提供しているイメージではないものを使用してPythonを動かす場合、awslambdaricというライブラリをインストールする必要があるようです。

代替のベースイメージから Python イメージを作成します

下記のページのUsageに従ってDockerFileを修正してみます。

AWS Lambda Runtime Interface Client for Python (awslambdaric)

前半のFor Build部分は不要なのかもしれませんが、まずはガイドに沿ってDockerFileを修正します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Define custom function directory
ARG FUNCTION_DIR="/function"

# ----For build-----
FROM ubuntu:22.04 as build-image

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY app.py requirements.txt ${FUNCTION_DIR}

# Install the function's dependencies
RUN pip install \
--target ${FUNCTION_DIR} \
awslambdaric

# ----For make image-----
FROM ubuntu:22.04

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

# Install the function's dependencies
RUN apt-get update \
&& apt install -y \
python3 \
python3-pip\
&& apt clean

RUN python3 -m pip install -r requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
ENTRYPOINT [ "/usr/bin/python3", "-m", "awslambdaric" ]
CMD ["app.lambda_handler"]

途中でビルドに失敗しました

1
2
3
4
5
6
$ sam build
(中略)
Step 6/13 : COPY app.py requirements.txt ${FUNCTION_DIR}

Build Failed
Error: HelloWorldFunction failed to build: When using COPY with more than one source file, the destination must be a directory and end with a /

DockerFileの最初のARGが悪さしているようなので、宣言部分を以下のように修正してみました。

1
2
3
# Define custom function directory
- ARG FUNCTION_DIR="/function"
+ ARG FUNCTION_DIR="/function/"

今度はpipが無いと言ってきました。

1
2
3
4
5
6
7
8
$ sam build
(中略)
Step 7/15 : RUN pip install --target ${FUNCTION_DIR} awslambdaric
---> Running in db7aacc682cf
/bin/sh: 1: pip: not found

Build Failed
Error: HelloWorldFunction failed to build: The command '/bin/sh -c pip install --target ${FUNCTION_DIR} awslambdaric' returned a non-zero code: 127

For Build部分にpipのインストールを追加してみます

1
2
3
4
5
6
7
8
9
# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev \
+ python3-pip

ビルドが通ったので、sam local invokeをしてみます。

1
2
3
4
5
6
7
8
$ sam local invoke
Invoking Container created from helloworldfunction:python3.9-v1
Building image.................
Using local image: helloworldfunction:rapid-x86_64.

END RequestId: dde7e0cf-a1dd-48b3-bc9b-e32552c7c247
REPORT RequestId: dde7e0cf-a1dd-48b3-bc9b-e32552c7c247 Init Duration: 0.40 ms Duration: 206.61 ms Billed Duration: 207 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}

正常に動作しました。

awslambdaricを使用したコンテナイメージのDockerFile

正常に動作した時のDockerFileは、以下の内容です。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Define custom function directory
ARG FUNCTION_DIR="/function/"

# ----For build-----
FROM ubuntu:22.04 as build-image

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev \
python3-pip

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY app.py requirements.txt ${FUNCTION_DIR}

# Install the function's dependencies
RUN pip install \
--target ${FUNCTION_DIR} \
awslambdaric

# ----For make image-----
FROM ubuntu:22.04

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

# Install the function's dependencies
RUN apt-get update \
&& apt-get install -y \
python3 \
python3-pip\
&& apt clean

RUN python3 -m pip install -r requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
ENTRYPOINT [ "/usr/bin/python3", "-m", "awslambdaric" ]
CMD ["app.lambda_handler"]

本当に「—-For fuild—-」部分の記述が必要なのかと思い、
削除して必要な記述を移植して試してみました。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Define custom function directory
ARG FUNCTION_DIR="/function/"

# ----For make image-----
FROM ubuntu:22.04

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY app.py requirements.txt ${FUNCTION_DIR}

# Install the function's dependencies
RUN apt-get update \
&& apt-get install -y \
python3 \
python3-pip\
&& apt clean

RUN pip install \
--target ${FUNCTION_DIR} \
awslambdaric

RUN python3 -m pip install -r requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
ENTRYPOINT [ "/usr/bin/python3", "-m", "awslambdaric" ]
CMD ["app.lambda_handler"]

こちらのDockerFileでも正常にビルドが通り、local invokeも問題ありませんでした。

1
2
3
4
5
6
7
$ sam local invoke
Invoking Container created from helloworldfunction:python3.9-v1
Building image.................
Using local image: helloworldfunction:rapid-x86_64.

END RequestId: 1305afc7-bce9-4e77-b7f3-ff77bf3b34ae
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}REPORT RequestId: 1305afc7-bce9-4e77-b7f3-ff77bf3b34ae Init Duration: 0.34 ms Duration: 157.30 ms Billed Duration: 158 msMemory Size: 128 MB Max Memory Used: 128 MB

awslambdaric の Usageに、「linux2014ホイールをサポートしていない場合は、必要なビルド依存関係もインストールする必要があります」のような記載があるので、使用するOSによっては必要なのかもしれません。

デプロイと動作確認

2023/03/19 追記

デプロイ確認していなかったので追記します。

sam deployコマンドをガイド使用で実行しました。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
$ sam deploy --guided

Configuring SAM deploy
======================

Looking for config file [samconfig.toml] : Found
Reading default arguments : Success

Setting default arguments for 'sam deploy'
=========================================
Stack Name [sam-app]:
AWS Region [us-east-1]: ap-northeast-1
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [Y/n]:
#SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]:
#Preserves the state of previously provisioned resources when an operation fails
Disable rollback [y/N]:
HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
Save arguments to configuration file [Y/n]:
SAM configuration file [samconfig.toml]:
SAM configuration environment [default]:

Looking for resources needed for deployment:

Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-3uw8pxxxxxx
A different default S3 bucket can be set in samconfig.toml
Image repositories: Not found.
#Managed repositories will be deleted when their functions are removed from the template and deployed
Create managed ECR repositories for all functions? [Y/n]: y

527d44233031: Pushed
1bfd440fd305: Pushed
90ddf71bf98e: Pushed
c6eabdd8bf04: Pushed
f515ae579fe5: Pushed
202fe64c3ce3: Pushed
helloworldfunction-84e20c8b6b22-python3.9-v1: digest: sha256:90b7b576b36830b5c31c79ef81cdd61a4b54ed1aff598fa59f86e5988909f7ce size: 1577

Deploying with following values
===============================
Stack name : sam-app
Region : ap-northeast-1
Confirm changeset : True
Disable rollback : False
Deployment image repository :
{
"HelloWorldFunction": "123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/samapp7427b055/helloworldfunction19d43fc4repo"
}
Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-3uw8pxxxxxx
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {}
Signing Profiles : {}

Initiating deployment
=====================

HelloWorldFunction may not have authorization defined.
Uploading to sam-app/4470b5f049d3dc4958aa2ba32e3b5067.template 1338 / 1338 (100.00%)


Waiting for changeset to be created..

CloudFormation stack changeset
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Add HelloWorldFunctionHelloWorldPermissionProd AWS::Lambda::Permission N/A
+ Add HelloWorldFunctionRole AWS::IAM::Role N/A
+ Add HelloWorldFunction AWS::Lambda::Function N/A
+ Add ServerlessRestApiDeployment47fc2d5f9d AWS::ApiGateway::Deployment N/A
+ Add ServerlessRestApiProdStage AWS::ApiGateway::Stage N/A
+ Add ServerlessRestApi AWS::ApiGateway::RestApi N/A
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Changeset created successfully. arn:aws:cloudformation:ap-northeast-1:123456789012:changeSet/samcli-deploy1679231685/14c3a52e-dc52-4cd3-a6e7-175ae9464787

デプロイするかどうかを聞かれるので、yを選択します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y

2023-03-19 22:14:55 - Waiting for stack create/update to complete

CloudFormation events from stack operations (refresh every 0.5 seconds)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole -
CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole Resource creation Initiated
CREATE_COMPLETE AWS::IAM::Role HelloWorldFunctionRole -
CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction -
CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction Resource creation Initiated
CREATE_COMPLETE AWS::Lambda::Function HelloWorldFunction -
CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi -
CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi Resource creation Initiated
CREATE_COMPLETE AWS::ApiGateway::RestApi ServerlessRestApi -
CREATE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc2d5f9d -
CREATE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermissionProd -
CREATE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermissionProd Resource creation Initiated
CREATE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc2d5f9d Resource creation Initiated
CREATE_COMPLETE AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc2d5f9d -
CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage -
CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage Resource creation Initiated
CREATE_COMPLETE AWS::ApiGateway::Stage ServerlessRestApiProdStage -
CREATE_COMPLETE AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermissionProd -
CREATE_COMPLETE AWS::CloudFormation::Stack sam-app -
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CloudFormation outputs from deployed stack
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Outputs
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Key HelloWorldFunctionIamRole
Description Implicit IAM Role created for Hello World function
Value arn:aws:iam::123456789012:role/sam-app-HelloWorldFunctionRole-1DUKMX2AJL0O4

Key HelloWorldApi
Description API Gateway endpoint URL for Prod stage for Hello World function
Value https://9b3ce2vfxj.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/

Key HelloWorldFunction
Description Hello World Lambda Function ARN
Value arn:aws:lambda:ap-northeast-1:123456789012:function:sam-app-HelloWorldFunction-vKAnLRafGQsg
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Successfully created/updated stack - sam-app in ap-northeast-1

curlコマンドでテストすると、hello worldが返ってきました。

1
2
$ curl  https://9b3ce2vfxj.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/          
{"message": "hello world"}

環境の削除

最後に、環境を削除します。
コンテナイメージでデプロイしていると、ECRのリポジトリも削除するかどうかを聞かれるようです。

sam delete
1
2
3
4
5
6
7
8
9
10
11
        Are you sure you want to delete the stack sam-app in the region ap-northeast-1 ? [y/N]: y
Are you sure you want to delete the folder sam-app in S3 which contains the artifacts? [y/N]: y
Found ECR Companion Stack sam-app-7427b055-CompanionStack
Do you you want to delete the ECR companion stack sam-app-7427b055-CompanionStack in the region ap-northeast-1 ? [y/N]: y
ECR repository samapp7427b055/helloworldfunction19d43fc4repo may not be empty. Do you want to delete the repository and all the images in it ? [y/N]: y
- Deleting ECR repository samapp7427b055/helloworldfunction19d43fc4repo
- Deleting ECR Companion Stack sam-app-7427b055-CompanionStack
- Deleting S3 object with key sam-app/4470b5f049d3dc4958aa2ba32e3b5067.template
- Deleting Cloudformation stack sam-app

Deleted successfully

まとめ

AWSが提供するLambda用コンテナイメージ以外のイメージ(代替イメージ)を使う場合には、依存ライブラリのインストールや、ENTRYPOINTの設定などが必要なことがわかりました。

今回はPython用を作成しましたが、node.js等もそのうち調べてみたいと思います。