-
Docker のウェブサイトの「Get Docker」(Docker を入手する) ページにアクセスします。要件を満たす Docker Desktop アプリケーションを選択します。表示される指示に従って Docker Desktop をインストールします。
-
ローカルマシンで、次の 3 つのファイルを含む 1 つのフォルダを作成します。 Dockerfile、ライブラリを含む requirements.txt、インポートステートメントを含む app.py。
注: Amazon Elastic Container Registry (Amazon ECR) パブリックレジストリには最新バージョンの Python を使用してください。
次の Dockerfile の例では Python 3.8 を使用しています。
FROM public.ecr.aws/lambda/python:3.8
# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}
# Install the function's dependencies using file requirements.txt
# from your project folder.
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ]
次の app.py ファイルの例には、インポートステートメントの例が含まれています。
import sysdef handler(event, context):
return 'Hello from AWS Lambda using Python' + sys.version + '!'
-
docker build コマンドとイメージ名を使用して Docker イメージをビルドします。
以下は、"hello-world" を使用した docker build コマンドの例です。
docker build -t hello-world .
注: Amazon ECR パブリックリポジトリからプルする際に認証エラーが発生した場合は、「Amazon ECR Public troubleshooting」(Amazon ECR パブリックのトラブルシューティング) を参照してください。
-
docker run コマンドを使用して Docker イメージを起動します。
以下は、"hello-world" を使用した docker run コマンドの例です。
docker run -p 9000:8080 hello-world
-
GitHub のウェブサイトで AWS の Lambda Runtime Interface Emulator (RIE) を使用してアプリケーションをテストします。新しいターミナルウィンドウから curl コマンドを実行して、次のエンドポイントにイベントをポストします。
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
このコマンドは、コンテナイメージで実行されている関数を呼び出し、応答を返します。
-
Docker CLI を Amazon ECR レジストリに対して認証します。
コマンドのアカウント ID と AWS リージョンをお使いのアカウント ID とリージョンに変更してください。
以下は、Amazon ECR レジストリに対して Docker CLI を認証するコマンドの例です。
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
-
create-repository コマンドを使用して Amazon ECR にリポジトリを作成します。
以下は create-repository コマンドの例です。
aws ecr create-repository --repository-name hello-world --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
-
docker tag コマンドを使用して、リポジトリ名と一致するようにイメージにタグを付けます。次に、docker push コマンドを使用してイメージを Amazon ECR にデプロイします。
コマンドのアカウント ID と AWS リージョンをお使いのアカウント ID とリージョンに変更してください。
以下は docker tag コマンドの例です。
docker tag hello-world:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
以下は docker push コマンドの例です。
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
-
Amazon ECR コンテナレジストリのコンテナイメージを使用して、Lambda 関数を作成して実行します。詳細については、「コンテナイメージを使用した Lambda 関数の作成」を参照してください。