2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LambdaのコンテナのイメージをECRのlatestへ更新する

Posted at

作成済みLambdaのコンテナの更新

マネジメントコンソールからLambdaコンテナを更新しに行くと、
イメージの登録が完了するまでにそれなりに時間がかかる。
CLIで楽にやりたい。

image.png

Lambdaのマネコンから登録されているイメージURIを見に行くと、
レジストリURI@sha256のURIとなっている。
この部分をCLIで更新する。

手段1. ハッシュ値による更新

aws ecr describe-imagesを使ってハッシュを取得し、URIを生成する。
生成したURLでaws lambda update-function-codeする。

ecr_deploy.bash
accountID="アカウントID";
ecrRepoName="ECRイメージ名";
aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin $accountID.dkr.ecr.ap-northeast-1.amazonaws.com
docker build -t $ecrRepoName .
docker tag $ecrRepoName:latest $accountID.dkr.ecr.ap-northeast-1.amazonaws.com/$ecrRepoName:latest
docker push $accountID.dkr.ecr.ap-northeast-1.amazonaws.com/$ecrRepoName:latest

digest=`aws ecr describe-images --repository-name $ecrRepoName | jq -r '.imageDetails[] | select(.imageTags==["latest"]) | .imageDigest'`
ECR_URI=$accountID.dkr.ecr.ap-northeast-1.amazonaws.com/$ecrRepoName@$digest;

#lambda更新
aws lambda update-function-code --function-name s3EventECR --image-uri $ECR_URI

結果

無事Lambdaのコンテナイメージが更新された。
image.png

手段2. latestタグで更新

:latestタグだけでも更新可能な様子。やってみる。

<省略>
ECR_URI=$accountID.dkr.ecr.ap-northeast-1.amazonaws.com/$ecrRepoName:latest;

#lambda更新
aws lambda update-function-code --function-name s3EventECR --image-uri $ECR_URI;

結果

無事Lambdaのコンテナイメージが更新された。
image.png
latest表示だが、デプロイしたタイミングでのlatestイメージでしかない。
これだと、いつのイメージなのか見分けられない。
1つ目のハッシュ付きURIの方法の方が運用上はよさそう。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?