Skip to content

Commit 7d16ec7

Browse files
committed
update tags
1 parent 37551cf commit 7d16ec7

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

terraform/tig_ecr.tf

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
data "aws_ecr_authorization_token" "token" {}
22

33
locals {
4-
# This regex splits the URI into: [0] registry/path, [1] name, [2] separator (: or @), and [3] tag or sha
5-
# Example: 123.dkr.ecr.url/my-image@sha256:12345...
6-
uri_parts = regex("^(.*)/(.*)([:@])(.*)$", var.lambda_container_image_uri)
7-
8-
raw_image_name = local.uri_parts[1]
9-
separator = local.uri_parts[2]
10-
image_version = local.uri_parts[3]
4+
# 1. Get the very last part of the URI (e.g., "dyen-cumulus-tig:0.13.0@sha256:abcd...")
5+
uri_parts = split("/", var.lambda_container_image_uri)
6+
image_and_version = element(local.uri_parts, length(local.uri_parts) - 1)
117

8+
# 2. Extract strictly the repository name by stopping at the first ":" or "@"
9+
raw_image_name = split("@", split(":", local.image_and_version)[0])[0]
10+
11+
# 3. Form the valid ECR repository name (No colons or @ symbols!)
1212
ecr_image_name = "${local.environment}-${local.raw_image_name}"
13-
14-
# For ECR, we usually want to push as a tag even if the source was a SHA.
15-
# If the source was a SHA, we'll strip 'sha256:' to use a clean string as a tag.
16-
safe_tag = replace(local.image_version, "sha256:", "")
17-
ecr_image_tag = replace(local.image_version, "sha256:", "")
13+
14+
# 4. Extract the raw version string by removing the repo name
15+
raw_version = replace(local.image_and_version, local.raw_image_name, "")
16+
17+
# 5. Sanitize the version into a perfectly valid ECR tag
18+
# Turns ":0.13.0@sha256:abcd" into a clean "0.13.0-abcd"
19+
# Turns "@sha256:abcd" into "abcd"
20+
ecr_image_tag = trim(replace(replace(local.raw_version, "sha256", ""), "/[:@]+/", "-"), "-")
1821
}
1922

2023
resource "aws_ecr_repository" "lambda-image-repo" {
@@ -38,8 +41,8 @@ resource "null_resource" "upload_ecr_image" {
3841
docker pull --platform=linux/arm64 ${var.lambda_container_image_uri}
3942
4043
# Tag and Push to the new repo
41-
docker tag ${var.lambda_container_image_uri} ${aws_ecr_repository.lambda-image-repo.repository_url}:${local.safe_tag}
42-
docker push ${aws_ecr_repository.lambda-image-repo.repository_url}:${local.safe_tag}
44+
docker tag ${var.lambda_container_image_uri} ${aws_ecr_repository.lambda-image-repo.repository_url}:${local.ecr_image_tag}
45+
docker push ${aws_ecr_repository.lambda-image-repo.repository_url}:${local.ecr_image_tag}
4346
EOF
4447
}
4548
}

0 commit comments

Comments
 (0)