Does somebody know what I'm doing wrong with `appl...
# typescript
s
Does somebody know what I'm doing wrong with
apply
?
Copy code
const db = new aws.rds.Instance(`postgresdb-${stage}`, {
...
});

export const dbEndpoint = db.endpoint.apply((e) => `${e}`);
But I'm still getting an output
Copy code
Outputs:
  + dbEndpoint : output<string>
c
You don't need an
apply
with that output since you are not doing any string interpolation:
Copy code
export const dbEndpoint = db.endpoint;
Did that not work for you for some reason?
s
Yeah same thing
Copy code
Outputs:
  + dbEndpoint : output<string>
I'm on
Copy code
pulumi version
v3.33.1
c
Are you looking at the
preview
phase? Because during preview, outputs will be unknown if the resource hadn't been created from a previous update phase.
s
Yeah I'm looking at preview but actually the resource was created before (I was expecting a different output even in preview) but I can give a try in real. Thanks!
c
Hmm if the RDS instance was created before in the same stack, then I would expect its endpoint to be known during subsequent preview phases. It's also possible that it's showing as
output<string>
here since the stack output property is new.
1
If your instance exists then the only thing that would change is the creation of the stack output property. In other words, the resource changes should show
x unchanged
, assuming you don't have any other resources that are changing.
s
That's right, I get
Copy code
Resources:
    18 unchanged
Still, I can't figure out the logic behind. On the real execution (same than in the preview)
Copy code
export const POSTGRES_ENDPOINT = db.endpoint;
export const POSTGRES_PASSWORD = dbPassword;
export const postgresUrl = `postgresql://${dbUser}:${POSTGRES_PASSWORD}@${POSTGRES_ENDPOINT}/${dbName}?schema=public`;
export const POSTGRES_URL_APPLY = pulumi
  .all([db.endpoint, dbPassword])
  .apply(([endpoint, password]) => {
    console.log("This is e", endpoint, password);
    return `postgresql://${dbUser}:${password}@${endpoint}/${dbName}?schema=public`;
  });
I still can't figure out the logic of this, while endpoint and password are valid as long as we don't perform operations on them, the moment I want to put them into the URL I get the same error
Copy code
POSTGRES_ENDPOINT : "<http://postgresdb-testxxx.xxx.us-east-1.rds.amazonaws.com:5432|postgresdb-testxxx.xxx.us-east-1.rds.amazonaws.com:5432>"
    POSTGRES_PASSWORD : [secret]
POSTGRES_URL_APPLY: "<postgresql://dbAppUser>:Calling [toString] on an [Output<T>] is not supported.\n\nTo get the value of an Output<T> as an Output<string> consider either:\n1: o.apply(v => `prefix${v}suffix`)\n2: pulumi.interpolate `prefix${v}suffix`\n\nSee <https://pulumi.io/help/outputs> for more details.\nThis function may throw in a future version of @pulumi/pulumi.@Calling [toString] on an [Output<T>] is not supported.\n\nTo get the value of an Output<T> as an Output<string> consider either:\n1: o.apply(v => `prefix${v}suffix`)\n2: pulumi.interpolate `prefix${v}suffix`\n\nSee <https://pulumi.io/help/outputs> for more details.\nThis function may throw in a future version of @pulumi/pulumi./dbtesting?schema=public" => [secret]
c
If you want to use
Output<string>
in string interpolation, you should use
pulumi.interpolate
. It's a special function that understands how to resolve the
Output<T>
values to their
T
type. So like this:
Copy code
export const postgresUrl = pulumi.interpolate `postgresql://${dbUser}:${POSTGRES_PASSWORD}@${POSTGRES_ENDPOINT}/${dbName}?schema=public`
Note that the return type of an interpolate itself is an
Output<string>
, which the Pulumi engine will know how to deal with. The problem with using regular JS interpolation is the output properties of resources you use inside the backticks are special. They are promise-like and Node will not know how to deal with them.
s
Thanks @clever-sunset-76585, I think I'm starting to get this right. Makes sense, thanks again!