11data "aws_ecr_authorization_token" "token" {}
22
33locals {
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
2023resource "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