https://pulumi.com logo
#general
Title
# general
m

millions-pharmacist-626

09/18/2023, 2:59 PM
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

dry-keyboard-94795

09/18/2023, 3:03 PM
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

millions-pharmacist-626

09/18/2023, 3:04 PM
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

dry-keyboard-94795

09/18/2023, 3:05 PM
yep, you'd need to create it in one stack, then import/`.get` afterwards in the second stack
l

little-library-54601

09/18/2023, 3:05 PM
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

millions-pharmacist-626

09/18/2023, 3:06 PM
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

dry-keyboard-94795

09/18/2023, 3:07 PM
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

millions-pharmacist-626

09/18/2023, 3:07 PM
got it, tyvm
l

little-library-54601

09/18/2023, 3:10 PM
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

millions-pharmacist-626

09/18/2023, 3:11 PM
yeah i think the import was suggested before I said that the resource too is created via pulumi
b

billowy-army-68599

09/18/2023, 5:05 PM
@millions-pharmacist-626 why do you need to do this?
m

millions-pharmacist-626

09/18/2023, 5:06 PM
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

billowy-army-68599

09/18/2023, 5:10 PM
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

millions-pharmacist-626

09/18/2023, 5:12 PM
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