Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using Docker backed AWS Lambda how to "deploy new image" with CLI?

I have deployed my lambda with Terraform. It is pointing successfully to ECR and the image runs with the expected ENTRYPOINT. However, every time I push new code to ECR, I have to go to the web console, click "Deploy New Image" else the lambda runs the old image.

Also, I tried using :latest as the tag, but that didn't seem to help. I was able to force a new image by uploading :2, :3 and then manually pointing the lambda at those. If there is a way to use the :latest tag that would be a nice convenience.

I'd be happier to do this with bash or boto, but no obvious way to do so from my reading of the docs.

This similar question was shut down because lambdas used to not be backed by docker images.

like image 381
MatthewMartin Avatar asked Sep 19 '25 10:09

MatthewMartin


2 Answers

Use the aws cli to update the lambda function.

Assuming you've got a profile or have appropriate credentials set in your environment you can call aws lambda update-function-code and provide your required image-uri. As you've defined the uri already you can query the function for the value.

aws lambda update-function-code --function-name $MY_FUNC --image-uri $(aws lambda get-function --function-name $MY_FUNC | jq -r '.Code.ImageUri')

I'm using jq to extract the ImageUri but I'm sure there are as many other ways and opinions about how to go about it.

If you have a look at the entire .Code object returned from aws lambda get-function --function-name $MY_FUNC you can check the sha256 and see that refect your updated function image.

Current docs: AWS CLI update-function-code docs

like image 86
Matt Bracewell Avatar answered Sep 23 '25 06:09

Matt Bracewell


It is wise to separate the image storage from the deployment process. I propose using semver for versioning the docker images. https://semver.org/

Then you can store the version number for the lambda docker image in a variable in terraform and pass it to your lambda module.

Of course this will be an extra step of running terraform apply for deployment, but this will lock down the state of your system and will make it easier to reproduce the environment state.

like image 42
wasserholz Avatar answered Sep 23 '25 06:09

wasserholz