https://pulumi.com logo
#typescript
Title
# typescript
m

miniature-arm-21874

04/01/2020, 3:52 PM
How can I get the name of a created repository to use as a string in getImage?
Copy code
const repo = new awsx.ecr.Repository('my-ecr-repository');

const image = aws.ecr.getImage({
  repositoryName: repo.repository.name.apply((name) => name),
  imageTag: 'latest',
});
I get
Type 'Output<string>' is not assignable to type 'string'. [Error/2322]
for repositoryName
w

white-balloon-205

04/01/2020, 6:11 PM
You need to move where the
apply
is called - the
getImage
call doesn't accept `Output`s as inputs currently.
Copy code
const repo = new awsx.ecr.Repository('my-ecr-repository');
const image = repo.repository.name.apply(name => aws.ecr.getImage({
  repositoryName: name,
  imageTag: 'latest',
}));
m

miniature-arm-21874

04/01/2020, 7:03 PM
Ah nice one I'll try that. The reason i'm trying to get an image is because i want
repo/service:latest
but when i first spin up the stack there isn't a latest image because it's not been pushed to the repo yet (obviously). I want to basically try to get the latest image and if it doesn't exist, build a function there and then and tag it as latest to use in the initial deploy. is that something which is possible/sensible?
@white-balloon-205 I just tried your example above
Copy code
const repo = new awsx.ecr.Repository('example-ecr-repository');

let image;
try {
  image = repo.repository.name.apply((name) => {
    return aws.ecr.getImage({
      repositoryName: name,
      imageTag: 'latest',
    });
  });
} catch (e) {
  image = 'caught';
}

console.log('------------------');
console.log(image);
I have a couple of questions- 1. i thought apply was supposed to wait for the output... in this example on first creation of the stack the repo doesn't exist yet so i get a
RepositoryNotFoundException 'example-ecr-repository-8e2cb70' does not exist in the registry with id '4744319xxxxx'
but it's got the name and continuing before the repo exists. is it possible to wait for the creation?
ecr.getImage
doesn't appear to support
dependsOn
2. the try catch is totally ignored so how can i achieve this? I need to make an image if the
latest
one doesn't exist yet
I've just found I can use the premise you've described here https://github.com/pulumi/pulumi/issues/3364 to use try/catch.
Copy code
const image = pulumi
  .all([repo.repository.repositoryUrl, repo.repository.name])
  .apply(async ([url, name]) => {
    try {
      await aws.ecr.getImage(
        {
          repositoryName: name,
          imageTag: 'latest',
        },
        { async: true }
      );

      return `${url}:latest`;
    } catch (e) {
      return 'nginx:latest';
    }
  });
this solves question 2, but also accidentally solves question 1 because the catch now catches the RepositoryNotFoundException. not sure why it's not waiting for it's creation first before calling apply(fn)
3 Views