Hi, is it possible to have the same resource exist...
# general
m
Hi, is it possible to have the same resource exist in 2 different stacks? Let's say that I have a pulumi project with 2 stacks,
staging
and
prod
, and there is one resource that needs to be global (let's say an S3 bucket, for simplicity). Can this bucket "exists" in both stacks, without being created twice, assuming that it's
resource_name
and configurations are exactly the same between them ?
d
You could
import
it (docs) but be mindful that either stack can delete it. You could also use the
.get()
method for one of the stacks. In your case,
prod
creates it, and
staging
would use
.get
m
I am aware of imports or "get" methods, the problem is that this is a pulumi defined resource, it's not created outside of pulumi
i literally define it in the code (it's not a bucket, it's an OIDC provider, but the same concept applies)
d
yep, you'd need to create it in one stack, then import/`.get` afterwards in the second stack
l
I've done this using stack references and conditional code in my Pulumi program based on which stack is being updated. If it's "staging", create the resource. If it's "production", use a stack reference to the staging stack to get a "reference" to the resource (i.e. to get its connection string or whatever).
m
I see. so in pseudo code:
Copy code
current_stack = pulumi.get_stack()

if current_stack == 'staging':
    resource = aws.Resource(...)
else:
    resource = aws.Resource().get()
correct ?
d
correct. And like Donnie says, you can pass the name/ID around with stack references. Or hard code it if you're going for simplicity and know what it is
m
got it, tyvm
l
FWIW, I don't need an "import" to do what I'm doing. In C#, it's something like this:
Copy code
var stagingStackReference = new StackReference("staging");

var communicationsServicesConnectionStringOutput = stagingStackReference.GetOutput("CommunicationsServicesConnectionString");
var signalRConnectionStringOutput = stagingStackReference.GetOutput("SignalRConnectionString");
I don't need the import, I believe, because I'm explicitly outputting the values I need for "production" when the "staging" stack runs.
m
yeah i think the import was suggested before I said that the resource too is created via pulumi
b
@millions-pharmacist-626 why do you need to do this?
m
Hi @billowy-army-68599, in my specific case it's because I'm configuring a Github OIDC provider in AWS, and only 1 can exist
b
ah, and you have one AWS account? I’d generally make this its own Pulumi project in this situation and have a single stack in there
that way you can just reference it as a stack reference
m
you have one AWS account?
yep, only one
I’d generally make this its own Pulumi project in this situation and have a single stack in there
yeah sometimes I do this, in this specific case I couldn't be assed to do so and was wondering if there was a specific alternative. The stack check that has been suggested above is good enough tbh