Skip to content

Commit

Permalink
fix: correctly parse ECR URIs and tags to OSMLContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
drduhe committed Jan 31, 2025
1 parent 188e708 commit 88d5397
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 44 deletions.
1 change: 0 additions & 1 deletion .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ rules:
require-await: off
"@typescript-eslint/no-unused-expressions":
- error
- allowShortCircuit: true
- allowTernary: true
"@typescript-eslint/no-unsafe-assignment": warn
"@typescript-eslint/interface-name-prefix": off
Expand Down
68 changes: 25 additions & 43 deletions lib/osml/osml_container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Amazon.com, Inc. or its affiliates.
* Copyright 2023-2025 Amazon.com, Inc. or its affiliates.
*/

import { RemovalPolicy, SymlinkFollowMode } from "aws-cdk-lib";
Expand Down Expand Up @@ -225,77 +225,59 @@ export class OSMLContainer extends Construct {
);
}

// Determine the tag to use for the container image, defaulting to "latest" if not provided.
const tag = "latest";

// Check if the CONTAINER_URI indicates an Amazon ECR repository by checking the ARN format.
const ecrArnRegex =
/^arn:([^:]+):ecr:([^:]+):[^:]+:repository\/[a-zA-Z0-9-._]+(:[a-zA-Z0-9-._]+)?$/;

if (ecrArnRegex.test(this.config.CONTAINER_URI)) {
let repositoryArn = this.config.CONTAINER_URI;
let extractedTag = tag;

// Extract tag if present
const tagSeparatorIndex = this.config.CONTAINER_URI.lastIndexOf(":");
if (
tagSeparatorIndex > this.config.CONTAINER_URI.indexOf("repository/")
) {
repositoryArn = this.config.CONTAINER_URI.substring(
0,
tagSeparatorIndex
);
extractedTag = this.config.CONTAINER_URI.substring(
tagSeparatorIndex + 1
);
}
let extractedTag: string | undefined;

// Find the last `:` in the URI to check for a tag
const tagSeparatorIndex = this.config.CONTAINER_URI.lastIndexOf(":");
if (tagSeparatorIndex > this.config.CONTAINER_URI.lastIndexOf("/")) {
extractedTag = this.config.CONTAINER_URI.substring(tagSeparatorIndex + 1);
}

// Use extracted tag if present; otherwise, default to the configured tag or "latest"
const tag = extractedTag ?? "latest";

// ECR repository URI format: <aws_account_id>.dkr.ecr.<region>.amazonaws.com/repository_name:tag
const ecrUriRegex =
/^[0-9]{12}\.dkr\.ecr\.[a-z0-9-]+\.amazonaws\.com\/[a-zA-Z0-9-_]+/;

if (ecrUriRegex.test(this.config.CONTAINER_URI)) {
const repositoryUri = this.config.CONTAINER_URI;

// Import the existing ECR repository using the ARN provided in the CONTAINER_URI.
this.repository = Repository.fromRepositoryArn(
// Import the existing ECR repository using the repository URI.
this.repository = Repository.fromRepositoryName(
this,
"ImportedECRRepo",
repositoryArn
repositoryUri.split("/").pop() || ""
);

// Create a ContainerImage object from the imported ECR repository and specified tag.
this.containerImage = ContainerImage.fromEcrRepository(
this.repository,
extractedTag
tag
);

// Set the containerUri to the full URI of the container image in ECR.
this.containerUri = this.repository.repositoryUriForTag(extractedTag);
this.containerUri = `${repositoryUri}:${tag}`;

if (this.buildDockerImageCode) {
// Create a DockerImageCode object for Lambda using the imported ECR repository.
this.dockerImageCode = DockerImageCode.fromEcr(this.repository, {
tagOrDigest: extractedTag
tagOrDigest: tag
});
}
} else {
// If the CONTAINER_URI does not indicate an ECR repository, assume it is a public or private Docker registry.
// Assume it's a Docker Hub or other external registry image
this.repositoryAccessMode = "Vpc";

// Create a ContainerImage object from the provided Docker registry URI and tag.
this.containerImage = ContainerImage.fromRegistry(
this.config.CONTAINER_URI
);

// Set the containerUri to the full URI of the container image in the Docker registry.
this.containerUri = this.config.CONTAINER_URI;

if (this.buildDockerImageCode) {
// Define the Dockerfile content dynamically based on containerUri
const dockerfileContent = `FROM ${this.config.CONTAINER_URI}`;

// Create a temporary Dockerfile to build the Docker image with
const tmpDockerfile = "Dockerfile.tmp";

// Write the temp Dockerfile to the build directory
const dockerfilePath = path.join(__dirname, tmpDockerfile);
fs.writeFileSync(dockerfilePath, dockerfileContent);

// Create a DockerImageCode object for Lambda using the DockerImageAsset
this.dockerImageCode = DockerImageCode.fromImageAsset(__dirname, {
file: tmpDockerfile,
followSymlinks: SymlinkFollowMode.ALWAYS
Expand Down

0 comments on commit 88d5397

Please sign in to comment.