📝

【AWS】DockerイメージをAWS Lambdaにデプロイする手順

2022/08/22に公開

Dockerイメージをビルド&ECRにプッシュ

# Dockerイメージを作成し、ECRにプッシュ
sh build_and_push.sh {Dockerイメージ名}
build_and_push.sh
#!/usr/bin/env bash

image=$1
path=$2

if [ "$image" == "" ]
then
    echo "Usage: $0 <image-name> <path>"
    exit 1
fi

if [ "$path" == "" ]
then
    echo "Usage: $0 <image-name> <path>"
    exit 1
fi

# Get the account number associated with the current IAM credentials
account=$(aws sts get-caller-identity --query Account --output text)

if [ $? -ne 0 ]
then
    exit 255
fi


# Get the region defined in the current configuration (default to ap-northeast-1 if none defined)
region=$(aws configure get region)
region=${region:-ap-northeast-1}


fullname="${account}.dkr.ecr.${region}.amazonaws.com/${image}:latest"

# If the repository doesn't exist in ECR, create it.

aws ecr describe-repositories --repository-names "${image}" > /dev/null 2>&1

if [ $? -ne 0 ]
then
    aws ecr create-repository --repository-name "${image}" > /dev/null
fi

# Get the login command from ECR and execute it directly
aws ecr get-login-password --region "${region}" | docker login --username AWS --password-stdin "${account}".dkr.ecr."${region}".amazonaws.com

# Build the docker image locally with the image name and then push it to ECR
# with the full name.

docker build -t ${image} ${path}
docker tag ${image} ${fullname}

docker push ${fullname}

Lambda関数を作成する

以下の操作を行い、「関数の作成」ボタンをクリック

Lambda関数を更新する

Discussion