Hey all, Wondering if anyone can help advise on t...
# general
g
Hey all, Wondering if anyone can help advise on this scenario: Objective: Use an
Output
from
project A/stack A
in
project B/stack B
,
project C/stack C
in multiple files as inputs to resources (keeping things DRY) Current Setup: • Function declared in
A
which uses
StackReference
to get an
Output
from
A
(seems odd to use
StackReference
for the current stack but is there a better way?)
• Files in
B
and
C
import the function from
A
and save the
Output
to a variable • The variable gets passed as input for creating resources in
B
and
C
• The CI pipeline runs a
preview
on all stacks (
A
,
B
,
C
etc) and fails on
B
because it doesn't recognise the variable which is dependent on the
Output
from
A
, since
A
hasn't had an
up
yet to actually produce the
Output
Am I missing something here in terms of the dependency? Happy to provide more context if helpful. Thanks in advance ✌️
b
Are Stack A, B and C from the same Pulumi project?
g
good question, I should've clarified that - they're each in separate projects. we're using a module approach within the organisation. I'll update the post!
b
So in stack A, wouldn’t you just export an output, then reference it in Stack B and Stack C as a stack reference?
which language SDK are you using?
g
using Python. the thinking was I didn't want to repeat the same code writing the
StackReference
in multiple places to get the same
Output
so my logic was to create a function in the module where it came from, to be reused in multiple places elsewhere
b
Generally that’s a 1 line code, but I will often abstract that away into a class, something like this:
Copy code
class VpcStackInfo(pulumi.StackReference):
    vpc_id = pulumi.Output[Any]
    private_subnet_ids = pulumi.Output[Any]
    public_subnet_ids = pulumi.Output[Any]
    cidr_block = pulumi.Output[Any]

    def __init__(self, stack_name: str):
        super().__init__(f"ignistech/vpc/{stack_name}")

        self.vpc_id = self.require_output("vpc_id")  # type: ignore
        self.private_subnet_ids = self.require_output("private_subnet_ids")  # type: ignore
        self.public_subnet_ids = self.require_output("public_subnet_ids")  # type: ignore
        self.cidr_block = self.require_output("cidr_block")  # type: ignore
Then reference it like this:
Copy code
vpc = stackinfo.VpcStackInfo(STACK)
I really try not to use cross referenced functions, you run into lots of issues that way
g
thanks for your help and suggestions I'll give that a go!
I've used the class approach and I think it's better, thanks! the
preview
is still failing for
B
because
A
hasn't been
up
'd yet I think. But all the changes are going together in the pipeline. Is there something I'm missing for dependency on
A
by
B
that needs to be declared for the
preview
to work?