I'm following along with (copy-pasting from) <http...
# typescript
p
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
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
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
Are you exporting the function from index.ts or another module? I don't know if you can export it from index.ts...
p
I am, will try something else
changed to non-index.ts but no luck
l
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
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
Are you creating resources?
p
yes
vpc via awsx
l
Console logs appear within the Pulumi output as a message, they don't go to the usual console.
p
yeah I dont see the logs or the tags though
l
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
will copy and report back
l
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
Couldn't get it to work so have raised an issue here https://github.com/pulumi/pulumi/issues/6214