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

calm-parrot-72437

09/15/2020, 5:18 PM
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

green-school-95910

09/15/2020, 5:21 PM
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

lemon-agent-27707

09/15/2020, 5:22 PM
Creating resources within apply is not recommended. It breaks the dependency graph and can cause problems.
g

green-school-95910

09/15/2020, 5:22 PM
It should not break dependency graph anymore, I sent a PR a while ago to fix that. But it is still not recommended
c

clever-sunset-76585

09/15/2020, 5:24 PM
@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

calm-parrot-72437

09/15/2020, 5:35 PM
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

clever-sunset-76585

09/15/2020, 5:38 PM
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

calm-parrot-72437

09/15/2020, 5:41 PM
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

green-school-95910

09/15/2020, 6:01 PM
Where does the
createUserData
function come from?
c

clever-sunset-76585

09/15/2020, 6:02 PM
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

green-school-95910

09/15/2020, 6:09 PM
@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

calm-parrot-72437

09/15/2020, 6:14 PM
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

clever-sunset-76585

09/15/2020, 6:19 PM
@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

green-school-95910

09/15/2020, 6:22 PM
True, I never noticed that on the resource docs always show the value that comes inside the input. TIL
c

calm-parrot-72437

09/15/2020, 6:26 PM
before userData though we'd be assigning an Output<string> to a string.. where the pulumi.interpolate would be
c

clever-sunset-76585

09/15/2020, 6:27 PM
@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

calm-parrot-72437

09/15/2020, 6:27 PM
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

clever-sunset-76585

09/15/2020, 6:55 PM
Awesome! Glad it worked.
3 Views