I tried to implement the autotag function and put ...
# general
q
I tried to implement the autotag function and put the function in a module to be able to reuse but I got this error message
Copy code
error: Running program '/PATH/aws/eks/network/pulumi' failed with an unhandled exception:
  Error: The root stack resource was referenced before it was initialized.
    at Object.registerStackTransformation (/PATH//pulumi/adfenix_generic/node_modules/@pulumi/pulumi/runtime/stack.js:211:15)
    at AdfenixGeneric.registerAutoTags (/Users/ulfmansson/RubymineProjects/infra/lib/pulumi/adfenix_generic/index.ts:86:24)
    at Object.<anonymous> (/PATH/aws/eks/network/pulumi/index.ts:14:17)
The code in the module looks like this
Copy code
import * as pulumi from "@pulumi/pulumi";

export function registerAutoTags(autoTags: Record<string, string>): void {
    pulumi.runtime.registerStackTransformation((args) => {
        if (AdfenixGeneric.isTaggable(args.type)) {
            args.props["tags"] = {...args.props["tags"], ...autoTags};
            return {props: args.props, opts: args.opts};
        }
        return undefined;
    });
}
My code:
Copy code
import {registerAutoTags} from "@mymodule/tags"
registerAutoTags({
    "environment": environment
});
l
Yes it's very fussy. I got it working eventually, but only by having my typescript project be above both the calling module and the autotag module.
That is, I needed both modules to be in directories under the same tsconfig.json, so they both use the same one.
Same for when testing, with the added caveat that I needed to run the tests in
pulumi.runtime.stack.runInPulumiStack()
. Though I can't remember if that was for the same error message...
I also got it working by passing one
pulumi.runtime
from my "main" module around, into all the other modules that needed it, but that was ick.
Another thing I tried that worked was to change the code in the module to return the autotagging function, so that my main modules called
pulumi.runtime.registerStackTransformation(mymodule.autotags({ "environment": environment}))
. That was less icky, but still not ideal.
The tsconfig solution is my current preferred one.
I've just realized that it wouldn't be the location of tsconfig.json that's important, it must be packages.json/node_modules.
I just tend to keep them all together.
q
Thanks for your thoughts I will do some testing. I think I will go for the solution that return the function. Not perfect...
@little-cartoon-10569 Do you have an example of the autotags function how it looked like?
l
I deleted the branch.. here's a quick rework, not yet tested...
Copy code
export function autoTagger(autoTags: any): ResourceTransformation {
  return function (args: ResourceTransformationArgs) {
    if (isTaggable(args.type)) {
      args.props.tags = {
        // Order is important. Later items override earlier items
        // if the key conflicts. So the "Name" is a default that can
        // be overwritten either by the parameters to `autoTagger`,
        // or by an explicitly-set Name tag in `args.props["tags"]`.
        Name: `${args.name}`, ...autoTags, ...args.props.tags,
      };
      return { props: args.props, opts: args.opts };
    }
    return undefined;
  }
}
I used
import { ResourceTransformation, ResourceTransformationArgs } from "@pulumi/pulumi";
but of course you could just use
pulumi.ResourceTransformation
etc..
Tested now, that works. Note that in my code, I have the autotags have lower priority than the original ones, which is the opposite of how it's implemented in Joe's blog post. That suits my use cases.
The original problem is what's described in https://github.com/pulumi/pulumi/issues/4669