hello everybody, i am pretty new to Pulumi and str...
# typescript
s
hello everybody, i am pretty new to Pulumi and struggling with Outputs! i could use the help of all you experts. because i've got a lot to write, i'll stick this in a thread 🧵
i have a project called
infrastructure
, which i successfully spun up. in this project is a Kubernetes cluster. in the script i used to create it, I exported a whole bunch of outputs which i will need to reference in other projects. this code is from the next project i want to spin up, where i try to reference the values of the first project. my teammate - who has since left the company sad panda - created this handy function which passes your logic into an `apply`:
Copy code
export const useClusterAttributes = (infraStackName: string, func: (outputs: ClusterAttributes) => any) => {
    const stackRef = new pulumi.StackReference(infraStackName)
    stackRef.getOutput('clusterName').apply(clusterName => { console.log(`The clusterName should be a string here, right?: ${clusterName}`) })
    return pulumi.all([
        stackRef.getOutput('vpcId'),
        stackRef.getOutput('publicSubnetIds'),
        stackRef.getOutput('privateSubnetIds'),
        stackRef.getOutput('clusterName'),
        stackRef.getOutput('kubeconfig'),
        stackRef.getOutput('clusterOidcProviderArn'),
        stackRef.getOutput('clusterOidcProviderUrl'),
    ])
        .apply(([
            vpcId,
            publicSubnetIds,
            privateSubnetIds,
            clusterName,
            kubeconfig,
            clusterOidcProviderArn,
            clusterOidcProviderUrl,
        ]) => {
            console.log(`At least should be a string here: ${clusterName}`);
            func({
                vpcId,
                publicSubnetIds,
                privateSubnetIds,
                clusterName,
                kubeconfig,
                clusterOidcProviderArn,
                clusterOidcProviderUrl,
            })
        })
}
i added those
console.log
statements, we'll get to them in a sec.
this is how you use this function:
Copy code
useClusterAttributes(infraStackName, async ({
    vpcId,
    clusterName,
    kubeconfig,
    clusterOidcProviderArn,
    clusterOidcProviderUrl,
}) => {
    const k8sProvider = ensureProvider(clusterName, kubeconfig)
   ... declare more resources down here ...
})
this is an abstraction away from a simpler
apply
syntax. the goal of
userClusterAttributes
is to prevent us from rewriting
pulumi.all
and
apply
commands for the same outputs again and again. please let me know if anyone needs clarification on this function and i'd be happy to explain more!
the real problem is that all of my Outputs -
clusterName
,
vpcId
, etc. - all evaluate to
undefined
. so when i try to create that
k8sProvider
, i get this error:
Error: Missing resource name argument (for URN creation)
and my
console.log
statements from earlier?
Copy code
hello orgname/infrastructure/stackname
    OutputImpl {
      __pulumiOutput: true,
      resources: [Function (anonymous)],
      allResources: [Function (anonymous)],
      isKnown: Promise { <pending> },
      isSecret: Promise { <pending> },
      promise: [Function (anonymous)],
      toString: [Function (anonymous)],
      toJSON: [Function (anonymous)]
    }
    The clusterName should be a string here, right?: undefined
    At least should be a string here: undefined
can anyone tell me why all my
stackRef.getOutput
Outputs are evaluating to
undefined
?
btw, i did confirm that the outputs are actually defined when they are exported from my first project, `infrastructure`:
b
@stocky-account-72012 how are you referencing your other stack? what backend are you using?
s
our infrastructure is deployed in AWS. these are the lines that reference the other stack:
Copy code
const stackName = pulumi.getStack();
const infraStackName = `orgname/infrastructure/${stackName}`;

useClusterAttributes(infraStackName, async ({
    vpcId,
    clusterName,
    kubeconfig,
    clusterOidcProviderArn,
    clusterOidcProviderUrl,
}) => {
..........
})
and, in a separate file is the definition of `useClusterAttributes`:
Copy code
export const useClusterAttributes = (infraStackName: string, func: (outputs: ClusterAttributes) => any) => {
    const stackRef = new pulumi.StackReference(infraStackName)
.......
b
and you’re double, triple sure the org, stack and project name are correct?
s
yep, i'm sure. i have done two things to verify that: 1. i used
console.log
to spit out the
infraStackName
inside the
useClusterAttributes
function, and the value it put out was
orgname/infrastructure/stackname
and looked correct. 2. i also tried altering the
infraStackName
variable in my main script to intentionally put a mistake in it. i then got this error:
Copy code
error: Preview failed: unknown stack "orgname/infrararastructure/stackname"
b
ah, hold on
you don’t need to do an
.apply
on the stack ref
take these:
Copy code
stackRef.getOutput('vpcId'),
        stackRef.getOutput('publicSubnetIds'),
        stackRef.getOutput('privateSubnetIds'),
        stackRef.getOutput('clusterName'),
        stackRef.getOutput('kubeconfig'),
        stackRef.getOutput('clusterOidcProviderArn'),
        stackRef.getOutput('clusterOidcProviderUrl'),
outside the apply
s
could you use line numbers to help clarify?
ah no sorry, I misread
s
ok, i think i've found something! one sec while i write it up
so previously the exports were done with the name
process
in front of them, e.g.
processClusterName
,
processVpcId
, etc. I rewrote them all to not have
process
, and I am searching my codebase and there is no instance of anything that says
processClusterName
anymore. also, i'm pasting the screenshot i showed above from my terminal when i ran
pulumi up
on my stack. it wrote that the outputs were just
clusterName
, no
process
.
BUT, i just saw in my web browser, when looking at this stack, that there are still outputs called "process"!
i changed my code to search for
processClusterName
and it actually found something. no more
undefined
🎉 so now my question is: why don't my terminal's outputs match what appears to actually be running?
should i tear down this whole stack and rebuild it? is that necessary whenever you change your outputs?
b
you should just run a new
up
and it should update them
s
i've run several today 😕 i'll run another now
(this will take a while, the Kubernetes clusters are slow to spin up)
OMG you know what. i think i know what happened. i feel absolutely silly 🤦‍♂️ if i'm right, it's all a stack mixup. total user error 😅 we will confirm once this
pulumi up
is done
ok, it's not a stack mixup, but i see what happened. i missed an error when deploying the
infrastructure
project before. i thought i had wiped out all those old outputs, but i missed that the deployment failed. as always, the issue lied between the chair and the keyboard 🤦‍♂️ thank you for your help @billowy-army-68599!! you walking me through it led to finding this, so you definitely contributed here. i should be able to straighten this out now that i have a handle on things.
❤️ 1