I'm being an idiot can someone save me from myself...
# typescript
c
I'm being an idiot can someone save me from myself? I've got: const instance = password.apply(pw => { const vm = new aws.ec2.Instance(....); return vm; }) export const ipaddress = instance.privateIp; The instance is created but pulumi conceals the value of the exported ipaddress. How can I fix this up so I can see the stack exports from the commandline?
g
Instead of transforming the password into the full resource transform it into the input that needs it and create the resource outside of apply. This way you'll be able to reference the outputs of the resource
☝️ 1
l
Creating resources within apply is not recommended. It breaks the dependency graph and can cause problems.
g
It should not break dependency graph anymore, I sent a PR a while ago to fix that. But it is still not recommended
c
@calm-parrot-72437 as Evan and Luis pointed out, creating the
Instance
resource inside
apply()
is not ideal. You should be able to pass the
password
as an input straight to whichever property uses that value in
new aws.ec2.Instance(…)
. Basically, do this:
Copy code
const instance = new aws.ec2.Instance("", {
  someProp: password <--
});
for a quick workaround with the code you already have, you can run
pulumi stack output --show-secrets
to reveal secret values in the command-line.
c
First thing I tried look like this: const instance = new aws.ec2.Instance(appName, { userData: createUserData( ["start_license_server"], { "start_license_server": { commands: { "01_start_server": { command:
start ${password}
, }, }, }, }, ), }); ..but the pulumi update details showed the userData created by pcloudinit package had pulumi warnings in it, so figured i had messed up
but i guess ya'll are suggesting I use the apply on the data sent to createUserData, which i'll try
c
when you use an output property in string interpolation you should use “pulumi.interpolate `start ${password}`”
Slack’s formatting messed that up, but here’s the docs to read about interpolation with output properties: https://www.pulumi.com/docs/intro/concepts/programming-model/#outputs-and-strings
c
command: pulumi.interpolate
start ${password}
? typescript doesn't like that, it wants a string for command not an Output<string>
?
but neither .apply() or pulumi.interpolate will work after the command: .. which is why i ended up creating the instance inside the apply. I'd much prefer to do it correctly and learning than working around with --show-secrets..
g
Where does the
createUserData
function come from?
c
As I mentioned in my earlier message, Slack’s code formatting mucked up what I typed. The link I posted above has a few examples of how to use interpolation with resource outputs properties. Since they are special properties, they can’t be used directly in JS string interpolation. They have to be prefixed with
pulumi.interpolate
which is
Output
-aware.
Copy code
const instance = new aws.ec2.Instance(appName, {
    userData: createUserData(
        ["start_license_server"],
        {
            "start_license_server": {
                commands: {
                    "01_start_server": {
                        command: pulumi.interpolate `start ${password}`,
                    },
                },
            },
        },
    ),
});
I am not sure what your
createUserData
function does, so I can’t guarantee ☝️ will work straightaway.
g
@clever-sunset-76585 He is right, the input type of
userData
is string instead of
Input<string>
. So typescript will reject it: https://www.pulumi.com/docs/reference/pkg/aws/ec2/instance/#userdata_nodejs
c
createUserData is from pcloudinit, i.e., https://github.com/joeduffy/pcloudinit
while i'm sure joe is partial to pulumi, the library is presumably useful outside of pulumi
c
@green-school-95910 in our resource docs we don’t show the types as wrapped types of
Input
or
Output
, but in fact
userData
property of
InstanceArgs
is of type
Input<string>
. Here’s the nodejs docs for that: https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/aws/ec2/#InstanceArgs-userData
g
True, I never noticed that on the resource docs always show the value that comes inside the input. TIL
c
before userData though we'd be assigning an Output<string> to a string.. where the pulumi.interpolate would be
c
@calm-parrot-72437 yes, it appears that
command
property only takes a string as you called out, so yeah you are back to transforming the output property with an apply. Can you try this?
Copy code
const userData = password.apply(p => {
    createUserData(
        ["start_license_server"],
        {
            "start_license_server": {
                commands: {
                    "01_start_server": {
                        command: `start ${p}`,
                    },
                },
            },
        },
    )
});

const instance = new aws.ec2.Instance(appName, {
    userData,
});
c
i see. thank you, yes will try.
👌 1
yeah, worked, needed to assign the output of createUserData to a variable and return that.. but i probably did something wrong, i'm a ts noob.
👍 1
thank you so much!
🙂 1
c
Awesome! Glad it worked.