I am developing series of lambdas that are using docker images. The first step is to create them and registering in AWS ECR (not sure if everything I am doing is ok, so any advice is welcomed :-) ):
terraform {
...
required_providers {
docker = {
source = "kreuzwerker/docker"
version = ">= 2.12"
}
}
}
resource aws_ecr_repository lambda_repo {
name = "lambda"
}
resource docker_registry_image lambda_image {
name = "<account_id>.dkr.ecr.<region>.amazonaws.com/lambda:latest"
build {
context = "./code/lambda"
}
depends_on = [
aws_ecr_repository.lambda_repo
]
keep_remotely = true
}
resource aws_lambda_function lambda {
...
image_uri = "<account_id>.dkr.ecr.<region>.amazonaws.com/lambda:latest"
source_code_hash = docker_registry_image.lambda_image.sha256_digest
...
}
So with this code:
- docker_registry_image > lambda_image : build the image and uploaded it in AWS
- aws_lambda_function > lambda : if the image "lambda:latest" the lambda is updated with the new code
The problem I have is how to "force" docker_registry_image > lambda_image to rebuild the image and update the "lambda:latest" when the Dockerfile or app.py (the main code that is added in the file) has changed. Also I am not sure if this is the way to build the images.
Thanks!!
I was stuck with the exact same problem, and was disappointed to find your question hadn't been answered. I struggled a good bit, but I just clicked late tonight and got mine working.
The problem is incorrect thinking based on bad Docker habits (guilty of the same here!):
latest
is a bad habit: it's based on tag mutability, which isn't how docker was designed, and pullinglatest
is non-deterministic, anyway - you never know what you're going to get. Usually,latest
will pull the most recent version on adocker pull
.Here is the better way to think about this:
docker_registry_image
and thekreusewerker/docker
provider are based on tag immutability.docker_registry_image
gets "replaced" in Terraform state (you'll see that in the Terraform plan when you try it), but the effect in your ECR repository is to add a new image with a the next sequential tag number, not to replace the actual image as one usually thinks with Terraform.image_tag
:docker_registry_image
so that it uses theimage_tag
variable (also touching up thedocker_registry_image
name so you're not doing to much error-prone string building):aws_lambda_function
. Change theimage_uri
to the name of thedocker_registry_image
so that those two are never out of sync:image_tag
variable by 1. Then try aterraform plan
and you'll see that thedocker_registry_image
andaws_lambda_function
will be replaced. A good exercise, would be to look at your ECR repo and Lambda function in the console while you do this. You'll see the images appearing in your ECR repo, and the Lambda function's image uri being updated with the newimage_tag
.Hope this helps. I sure feel a whole lot better tonight!