aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.4k stars 3.8k forks source link

aws-batch-alpha: Invalid ECR image URI #25456

Closed gael-ft closed 1 year ago

gael-ft commented 1 year ago

Describe the bug

In a stack, when you import an ECR repository (don't know if importing it or not makes a difference), then trying to use it within image property of container results (after synthesis or deploy) to a URI like:

***.dkr.ecr.eu-west-1.[object Object]/my/image:dev

Note the [object Object] which is the issue here.

Looks like Stack tokens are not correctly resolved.

Expected Behavior

***.dkr.ecr.eu-west-1.amazonaws.com/my/image:dev

Current Behavior

***.dkr.ecr.eu-west-1.[object Object]/my/image:dev

Reproduction Steps

export interface MyConstructProps {
  // Note they might be defined in different account / region compared to the stack itself
  readonly repoAccount: string;
  readonly repoRegion: string;
}

export class MyConstruct extends Construct {
  constructor(scope: Construct, id: string, props: MyConstructProps) {
    const stack = cdk.Stack.of(this);
    const ecrRepoArn = stack.formatArn({
      service: 'ecr',
      region: props.repoRegion,
      account: props.repoAccount,
      resource: 'repository',
      resourceName: 'my/image'
    });
    const ecrRepo = ecr.Repository.fromRepositoryAttributes(this, 'EcrRepository', {
      repositoryArn: ecrRepoArn,
      repositoryName: `my/image`
    });

    // for my self it's inside another Construct, but that shouldn't change anything

    const imageTag = 'dev';

    new batch_alpha.EcsJobDefinition(this, 'JobDefinition', {
      // ...
      container: new batch_alpha.EcsFargateContainerDefinition(this, 'ContainerDefinition', {
        image: ecs.ContainerImage.fromEcrRepository(ecrRepo, imageTag),
        // Note this is what I am using as workaround
        // image: ecs.ContainerImage.fromRegistry(stack.resolve(ecrRepo.repositoryUri + `:${imageTag}`)),
        // ...
      }),
      // ...
    });
  }
}

Possible Solution

When looking at Lambda function using Docker images, this is how the template looks like:

"Code": {
  "ImageUri": {
    "Fn::Join": [
      "",
      [
        "***.dkr.ecr.eu-west-1.",
        {
          "Ref": "AWS::URLSuffix"
        },
        "/my/image:dev"
      ]
    ]
  }
},

Additional Information/Context

No response

CDK CLI Version

2.77.0 (build 06a0b19)

Framework Version

No response

Node.js Version

v16.13.0

OS

MacOS

Language

Typescript

Language Version

No response

Other information

No response

pahud commented 1 year ago

Thanks for your report. Closing in favor of https://github.com/aws/aws-cdk/issues/25250

github-actions[bot] commented 1 year ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.