Lambda function using a Docker image

AWS

Docker on Windows

Docker Desktop

Docker Desktop: 開発者向けの#1コンテナ化ツール | Docker
Dockerデスクトップは、開発者向けの共同コンテナ化ソフトウェアです。 今すぐ Docker Desktop を Mac、Windows、または Linux でダウンロードしましょう。

Rancher Desktop

Rancher Desktop by SUSE
Open source desktop application that provides Kubernetes, Container Management, bundled utilities on the desktop

WSL

Windows Subsystem for Linux のドキュメント
Windows Subsystem for Linux のドキュメントの概要。

Base Image for AWS Lambda

ECR Public Gallery
Amazon ECR Public Gallery is a website that allows anyone to browse and search for public container images, view develop...

Dockerfile

Lambda Function Code

import sys, boto3

def lambda_handler(event, context):
    print(f"sys.version={sys.version}")
    print(f"boto3.__version__={boto3.__version__}")

requirements.txt

boto3

Dockerfile

FROM public.ecr.aws/lambda/python:3.13

COPY requirements.txt ${LAMBDA_TASK_ROOT}

RUN pip3 install -r requirements.txt

COPY app.py ${LAMBDA_TASK_ROOT}

CMD [ "app.lambda_handler" ]

Build

docker buildx build --platform linux/amd64 --provenance=false -t IMAGE_NAME:TAG.

ECR Login

aws ecr get-login-password --region REGION | docker login --username AWS --password-stdin AWS_ACCOUNT.dkr.ecr.REGION.amazonaws.com

Create Repository

aws ecr create-repository --repository-name REPOSITORY_NAME --region REGION --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE

Deploying the image

docker tag IMAGE_NAME:TAG AWS_ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY_NAME:latest
docker push AWS_ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY_NAME:latest

SAM Template

ここではLambda関数をSAMでデプロイします。

samconfig.toml

version = 0.1

[default.global.parameters]
region = REGION
stack_name = STACK_NAME
s3_bucket = BUCKET_NAME
s3_prefix = PREFIX

template.yaml

Resources:
  Lambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: LAMBDA_FUNCTION_NAME
      PackageType: Image
      ImageUri: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/REPOSITORY_NAME:latest
      Role: !Sub arn:aws:iam::${AWS::AccountId}:role/ROLE_NAME

  Logs:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub /aws/lambda/${Lambda}

Deploying Lambda Functions

sam deploy --image-repository REPOSITORY_URI

Reference 参考

コンテナイメージを使用した Lambda 関数の作成 - AWS Lambda
AWS により提供されたベースイメージまたは代替のベースイメージを使用して、Lambda 関数のコンテナイメージを作成します。
Home
Docker Documentation is the official Docker library of resources, manuals, and guides to help you containerize applicati...
Docker ドキュメント日本語化プロジェクト — Docker-docs-ja 27.0 ドキュメント
タイトルとURLをコピーしました