Terraform recreate Kubernetes deployment when docker images tag updates

1/16/2022

I have different git repos for microservice A,B,C. Also one git repo where the terraform config is stored. When one microservice's code changes a pipeline is triggered and the docker image is published to the google cloud artifact registry.

I then manually run terraform apply. My terraform files specify a kubernetes deployment for each service. The newest docker image version for each deployment is specified in the terraform file (hardcoded).

I want to run terraform apply automatically, after the pipeline of a service runs. But because the current tag is hardcoded in the terraform files it needs to be updated manually.

Approach 1: I could make the tag as an input variable but everytime one service's new image gets build the others tags stays the same. So I wouldn't know the not changed ones.

Approach 2: Using the tag latest is also not possible, because the image changes, but the kubernetes deployment in terraform gets not rebuild because the tag stayed the same.

image_A = "${var.gcp_region}-docker.pkg.dev/${var.gcp_project}/${local.artifact_repository_id}/A:28"

image_B = "${var.gcp_region}-docker.pkg.dev/${var.gcp_project}/${local.artifact_repository_id}/B:20"

image_C = "${var.gcp_region}-docker.pkg.dev/${var.gcp_project}/${local.artifact_repository_id}/C:48"

Does someone have an idea how to solve this problem?

-- zel873ju
continuous-integration
docker
google-cloud-platform
kubernetes
terraform

0 Answers