https://pulumi.com logo
#python
Title
# python
a

ancient-thailand-47587

01/09/2024, 11:03 PM
Hi I’m following https://www.pulumi.com/blog/automatically-enforcing-aws-resource-tagging-policies/#automatically-applying-tags to try auto tagging. I’m getting this error when I run the code
Copy code
error: Program failed with an unhandled exception:
    Traceback (most recent call last):
      File "/Users/vincent/workspace/sre/platform-vpc-install/__main__.py", line 5, in <module>
        from autotag import register_auto_tags
    ModuleNotFoundError: No module named 'autotag'
Am I missing a pip install of a package? The doc doesn’t say to pip install anything…
b

billions-river-87988

01/09/2024, 11:08 PM
that error means its getting an error trying to import the function 'register_auto_tags' from the file 'autotag.py' in the same folder (containing the code for that function from that guide)...
a

ancient-thailand-47587

01/09/2024, 11:11 PM
So am I supposed to put the 2 functions from the first block of code (
register_auto_tags
and
auto_tag
in a file called
autotag
?
b

billions-river-87988

01/09/2024, 11:11 PM
yeah,
autotag.py
a

ancient-thailand-47587

01/09/2024, 11:12 PM
In my IDE, VSCode, it’s complaining about unable to import. OK, will try…
b

billions-river-87988

01/09/2024, 11:13 PM
i might be slightly off, but thats the gist of it. you might need an
__init__.py
somewhere
a

ancient-thailand-47587

01/09/2024, 11:13 PM
And what about the line
Copy code
from taggable import is_taggable
Where is that being imported from?
b

billions-river-87988

01/09/2024, 11:14 PM
probably the same... you can also just copy those functions all into the
__main__.py
file you are using and get rid of the imports
the imports are just the authors way of breaking up the program somewhat (but its not that clear from the guide)
a

ancient-thailand-47587

01/09/2024, 11:14 PM
hm okay, will try. Feels like I’m and the doc is missing a pip install of something…
all the code is there, you can see how its organized
a

ancient-thailand-47587

01/09/2024, 11:15 PM
Thank you!
b

billions-river-87988

01/09/2024, 11:16 PM
fun fact, that code repo was created by the CEO of Pulumi
a

ancient-thailand-47587

01/09/2024, 11:17 PM
Cool!, looks like it only requires pulumi and pulumi-aws 🤞
Does auto-tag require a policy as well? I was under the impression that it’s separate, where you can enforce a policy OR use auto-tag
b

billions-river-87988

01/09/2024, 11:21 PM
not sure what you mean, i dont think so
a

ancient-thailand-47587

01/09/2024, 11:22 PM
I see a
policy-config.json
which lead me to think auto-tagging requires a policy enforcement.
b

billions-river-87988

01/09/2024, 11:25 PM
ah, i see. no, the guide is showing first how to setup policy checks on tags (which is an optional feature) using the CrossGuard policy engine. The second part (in python), which you are working on, is an example of using a stack transform to apply tags to resources in the stack automatically (thus satisfying the policy check on tagging).
a

ancient-thailand-47587

01/09/2024, 11:25 PM
Another question - I see
taggable.py
has not been updated in 4 years. Is there an updated list of taggable_resource_types?
b

billions-river-87988

01/09/2024, 11:26 PM
no, its just an example and not definitive
im not even sure how valid that still is-
a

ancient-thailand-47587

01/09/2024, 11:26 PM
Is there an updated list? or a pip package available?
OK, thank you. Appreciate all the help and tips!
b

billions-river-87988

01/09/2024, 11:27 PM
another thing you can try, which might be way easier is to set the default tags on the provider- let me find an example in python
a

ancient-thailand-47587

01/09/2024, 11:30 PM
Thank you. I have a question about providers. Should I ask in a different thread?
b

billions-river-87988

01/09/2024, 11:31 PM
i can try to answer (I work at pulumi)
a

ancient-thailand-47587

01/09/2024, 11:31 PM
This last example looks to be a pretty nice way to do it.
Ah I see. I’ve been trying to figure out how to query for a provider. I tried to query for a provider and it errors out. I tried to use a try/catch but it doesn’t raise an exception, the program just errors… I haven’t been able to figure out how to do it properly…
The provider is not a dynamic resource and after first run I see it get created.
The provider is in a different stack as well.
I’ve been trying to use this
Copy code
aws.Provider().get_provider(<created-provider-name>)
I don’t think this is the correct usage to query for a create provider.
b

billions-river-87988

01/09/2024, 11:47 PM
usually i create a new provider like in that AI example and pass it along to each resource, when overriding the default-
a

ancient-thailand-47587

01/09/2024, 11:49 PM
My particular use case is configuring bucket replication. I have to create a bucket using 2 providers in a stack. The next stack I’m trying to use the same providers created from previous to configure replication.
In the second stack, if I try to create new providers, it complains that the providers exist and I don’t know how to query for existing resources.
b

billions-river-87988

01/09/2024, 11:51 PM
i'd probably need to see a code example
a

ancient-thailand-47587

01/09/2024, 11:54 PM
Stack 1 I have 2 regions which execute
Copy code
aws.s3.BucketV2(
            resource_name=self.get_name(),
            args=self.get_bucket_args(),
            opts=pulumi.ResourceOptions(provider=aws.Provider(
            f"{self.__name}-{region}", region=region))))
here the Provider also gets created.
Now a bucket has been created in each region and a provider has been created per bucket ( not the best resuse, trying to get a hang of the ropes here) at the end of executing I see the buckets got created and the providers
Now in stack 2 I’m trying to use the providers, which were created in stack 1 to configure replication, Is that possible?
b

billions-river-87988

01/09/2024, 11:58 PM
stacks are just instantiations of a pulumi program (for example, dev or prod), do you have 2 separate programs?
a

ancient-thailand-47587

01/10/2024, 12:00 AM
Essentially, I have the first stack which creates the buckets, and it’s state file. Then the second stack which I’m trying to configure bucket replication with it’s own state file.
b

billions-river-87988

01/10/2024, 12:02 AM
then there really shouldn't be any interaction between the providers... provider is just a runtime thing that allows a resource to communicate with the underlying API
a

ancient-thailand-47587

01/10/2024, 12:03 AM
But if I try to create another provider with the same name in stack 1 it says the resource already exists.
b

billions-river-87988

01/10/2024, 12:03 AM
are you sure its the provider and not the resource?
a

ancient-thailand-47587

01/10/2024, 12:03 AM
Maybe I should try to use a dynamic provider
I’m pretty sure, will have to put some code in to check again… I removed it since I haven’t figured out how to use it yet and was going to come back to it.
b

billions-river-87988

01/10/2024, 12:05 AM
i see you are using
self.get_name()
maybe its just a naming collision on your resources
a

ancient-thailand-47587

01/10/2024, 12:07 AM
I’ll give it a shot again, and come back to the thread. Thank you for your time!
b

billions-river-87988

01/10/2024, 12:07 AM
no problem, good luck