Hi, how do I use a Secret as a string? I am using ...
# general
g
Hi, how do I use a Secret as a string? I am using Typescript. When I use
apply
, pulumi throws
Error: Secret outputs cannot be captured by a closure.
If I use
pulumi.interpolate
, the IDE complains that
Type 'Output<string>' is not assignable to type 'string | string[] | undefined'.
v
How are you accessing the secret? Is it stored in pulumi config? If so, you shouldn't need to use interpolate, as the secret is just treated as an output sting anyway
g
I am storing it in the Pulumi config. When I read it as
config.requireSecret
, the type is
Output<String>
and not string
v
What resource are you trying to pass it in to?
Outputs should be able to be passed in to any pulumi resources inputs
g
A custom one. I am writing a dynamic provider and I am reading the API credentials through secrets and passing it as
Authorization
for the API using the
got
library. Currently, using a workaround of `config.require()`instead of
config.requireSecret
but I would ideally like it be to a forced secret instead of it being an optional one
v
Have you tried type casting it?
${secret} as string
g
Does not work, it throws the same error -
Error: Secret outputs cannot be captured by a closure
My code snippet if that helps -
Copy code
const nameComUserName = config.requireSecret("name-com-user-name");
const nameComToken = config.requireSecret("name-com-token");

...

"Authorization": `Basic ${Buffer.from(`${nameComUserName}` as string +":"+`${nameComToken}` as string , 'binary').toString('base64')}`
v
Do you have the full code? That doesn't really help too much
b
Instead of trying to get a string out of your output, you should do your API call inside of an apply
Your resource declared within the apply may not show in your preview when that output isn't being evaluated, but that's the trade off you're making
Actually since it's a config secret and not an eventual output the “not showing up during preview” issue may never occur since the secret should always be evaluate-able. But anyways just do your work inside the apply
g
Thank you
516 Views