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

prehistoric-kite-30979

01/27/2021, 4:36 PM
I'm following along with (copy-pasting from) https://github.com/joeduffy/aws-tags-example/tree/master/autotag-ts. And am hitting the
root stack was referenced before it was initialzed
error. https://github.com/pulumi/pulumi/blob/master/sdk/nodejs/runtime/stack.ts#L218
I have diverged slightly from the example...
Copy code
export function registerAutoTags(tags?: Record<string, string>): void {
    const metadata = new pulumi.Config("tetrate:metadata")
    pulumi.runtime.registerStackTransformation((args) => {
        if (isTaggable(args.type)) {
            args.props["tags"] = { ...args.props["tags"], ...tags, ...{
                "pulumi:project": pulumi.getProject(),
                "pulumi:stack": pulumi.getStack(),
                "tetrate:owner": metadata.require("owner"),
                "tetrate:source": metadata.require("source"),
            } }
            return { props: args.props, opts: args.opts }
        }
        return undefined
    })
}
(this function is called immediately after the imports)
Is there something else I should be doing?
b

brave-planet-10645

01/27/2021, 4:45 PM
Firstly, good for you trying to get your head around transformations. If we had exams, this would be on the advanced exams
I've not done stack transformations, but I've done resource transformations so let me hunt out some code and see if I can help
So I think it's because you've got the
metadata.require
in there. I would pass in the metadata to the parameters of the
registerAutoTags
method
p

prehistoric-kite-30979

01/27/2021, 7:06 PM
gave that a go but no luck
I also tried the following and I get the same error
Copy code
export function registerAutoTags(tags?: Record<string, string>): void {
    pulumi.runtime.registerStackTransformation((args) => {
        if (isTaggable(args.type)) {
            args.props["tags"] = { ...args.props["tags"], ...tags, ...{
                "test": "test",
            } }
            return { props: args.props, opts: args.opts }
        }
        return undefined
    })
}
Copy code
export function registerAutoTags(tags?: Record<string, string>): void {
    pulumi.runtime.registerStackTransformation((args) => {
        return undefined
    })
}
same error here ^^
l

little-cartoon-10569

01/27/2021, 7:56 PM
Are you exporting the function from index.ts or another module? I don't know if you can export it from index.ts...
p

prehistoric-kite-30979

01/27/2021, 9:32 PM
I am, will try something else
changed to non-index.ts but no luck
l

little-cartoon-10569

01/27/2021, 9:41 PM
Just don't export it.
You only need to export it if it's in one module and used in a different one.
If the code is in index.ts, and used there, then just define the function locally.
p

prehistoric-kite-30979

01/27/2021, 9:50 PM
ah, I see what you mean
yes that appears to work
👍 1
thanks for the help!
👍 1
hm actually it appears to never get called
Copy code
pulumi.runtime.registerStackTransformation((args) => {
    console.log("TEST")
        args.props["tags"] = { ...args.props["tags"], ...{
            "pulumi:project": pulumi.getProject(),
            "pulumi:stack": pulumi.getStack(),
        } }
        return { props: args.props, opts: args.opts }
})
l

little-cartoon-10569

01/27/2021, 10:08 PM
Are you creating resources?
p

prehistoric-kite-30979

01/27/2021, 10:09 PM
yes
vpc via awsx
l

little-cartoon-10569

01/27/2021, 10:10 PM
Console logs appear within the Pulumi output as a message, they don't go to the usual console.
p

prehistoric-kite-30979

01/27/2021, 10:10 PM
yeah I dont see the logs or the tags though
l

little-cartoon-10569

01/27/2021, 10:13 PM
Hmm... not sure.. I don't suppose there'd be a problem due to tagging untaggable resources. It's TypeScript, the spurious properties would just be ignored...
My equivalent function is this:
Copy code
function autoTagger(autoTags: any): ResourceTransformation {
  return function (args: ResourceTransformationArgs) {
    if (isTaggable(args.type) && (args.opts as any)["import"] == undefined) {
      args.props.tags = {
        Name: `${args.name}`, ...autoTags, ...args.props.tags,
      };
      return { props: args.props, opts: args.opts };
    }
    return undefined;
  }
}
p

prehistoric-kite-30979

01/27/2021, 10:16 PM
will copy and report back
l

little-cartoon-10569

01/27/2021, 10:17 PM
That function gets passed to pulumi.runtime.registerStackTransformation(). It sets a default Name tag, then the tags passed into it, then the tags explicitly set on the resource being transformed. So it's not quite the same as yours. But the syntax is slightly different, so maybe you'll see something that helps.
The "import" bit is to skip transformation on any resource that has the "import" opt. That won't apply to you.
p

prehistoric-kite-30979

01/28/2021, 1:39 PM
Couldn't get it to work so have raised an issue here https://github.com/pulumi/pulumi/issues/6214