From 7ef8649ab89f18dfd685b6c38a13c338139162c3 Mon Sep 17 00:00:00 2001
From: Aditya Mysore <adityamysore17@gmail.com>
Date: Wed, 18 Dec 2024 14:43:41 -0500
Subject: [PATCH] Update LATEST_VERSION_ID in Buildspec configuration of
 CodeBuild Project and image tag (if user does not provide a desired image
 tag) in the ECS Task definition to fix issue with S3 object version IDs that
 can start with a "." which is not a valid docker image tag.

---
 main.tf | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/main.tf b/main.tf
index 96ff215..ec13a72 100644
--- a/main.tf
+++ b/main.tf
@@ -634,7 +634,7 @@ resource "aws_ecs_task_definition" "streamlit_ecs_task_definition" {
   container_definitions = jsonencode([
     {
       name      = "${var.app_name}-container",
-      image     = "${aws_ecr_repository.streamlit_ecr_repo.repository_url}:${var.ecs_task_desired_image_tag != null ? var.ecs_task_desired_image_tag : data.aws_s3_object.streamlit_assets.version_id}",
+      image     = "${aws_ecr_repository.streamlit_ecr_repo.repository_url}:${var.ecs_task_desired_image_tag != null ? var.ecs_task_desired_image_tag : "v${data.aws_s3_object.streamlit_assets.version_id}"}",
       cpu       = var.task_cpu    # CPU units for Fargate (must be number, not string)
       memory    = var.task_memory # Memory in MiB for Fargate (must be number, not string)
       essential = true,
@@ -1034,9 +1034,9 @@ resource "aws_codebuild_project" "streamlit_codebuild_project" {
       phases:
         pre_build:
           commands:
-            # Fetch the S3 object version id of the latest version of the app and store as environment variable
+            # Fetch the S3 object version id of the latest version of the app, prepend with "v" and store as environment variable
             - echo Fetching the S3 object version id of the latest version of $APP_NAME...
-            - export LATEST_VERSION_ID=${data.aws_s3_object.streamlit_assets.version_id}
+            - export LATEST_VERSION_ID=v${data.aws_s3_object.streamlit_assets.version_id}
             - echo $LATEST_VERSION_ID
             # Log into Amazon ECR
             - echo Logging in to Amazon ECR...