https://pulumi.com logo
Title
r

rapid-motherboard-85863

02/08/2023, 2:40 PM
Hello, stupid question time. We have abstracted away the creation of resources into modules which can be re-used in other projects. How can I get access to the stack outputs within these modules without passing them into the method. I wanted to keep this as clean as possible. I tried using stack references to the current instance stack name but the Pulumi threw a duplicate resource urn error
b

billowy-army-68599

02/08/2023, 3:44 PM
when you say “module” are you using Component?
r

rapid-motherboard-85863

02/08/2023, 3:51 PM
No when I say modules I just mean a bunch of classes that abstract away common things for each resource e.g. correct naming standard and security settings etc.
b

billowy-army-68599

02/08/2023, 3:52 PM
You extend the pulumi Component resource class you can just make the outputs a part of it.
It’s not super clear how you’ve structured things, if you have any code we can review it might help
A duplicate resource urn error is going to always happen if you create two resources with the same name
r

rapid-motherboard-85863

02/08/2023, 3:54 PM
but I haven't that was what confused me. I was just creating a StackReference to the current stack. I was surprised I needed to do that as I thought I could just get a static instance off the Pulumi.Deployment.Instance
Is there a c# example anywhere?
yes that’s what I’m referring to
r

rapid-motherboard-85863

02/08/2023, 4:01 PM
I did look into that before but it seemed more like where you want to group the creation of multiple resources into a single operation.
b

billowy-army-68599

02/08/2023, 4:03 PM
yeah it may not be what you need based on what you’re saying, I assumed with the usage of modules that might be it
r

rapid-motherboard-85863

02/08/2023, 4:04 PM
They are literally just there to apply consistency to naming and settings across projects and also make the Stack code more readable i.e. not 500 lines long
so my stack code is more or less like this:
var asp = appServicePlanModule.CreateResource();
Which is fine for a simple resource like an App Service Plan. But then for a function app it needs the ASP, AppInsights, StorageAccount etc.
b

billowy-army-68599

02/08/2023, 4:07 PM
so you have an example of what one looks like?
r

rapid-motherboard-85863

02/08/2023, 4:10 PM
public Component CreateResource(CustomResource? parent = null, CustomResource? associatedResource = null, CustomResource? dependsOn = null, string index = "01")
        {
            var ai = new Component($"ai-{index}", new ComponentArgs
            {
                ApplicationType = ApplicationType.Web,
                ResourceGroupName = _configOptions.ResourceGroupName,
                Location = _configOptions.Location,
                ResourceName = $"{_configOptions.ResourcePrefix}-ai-{_configOptions.EnvironmentIdentifier.ToLower()}-{_configOptions.ProjectIdentifier.ToLower()}-{index}",
                Kind = "web"
            });

            return ai;
        }
That's the simplest one
I'm going to re-factor it anyway as I had everything working off the same interface to try and ensure consistency and for mocking but I don't think its really helping
b

billowy-army-68599

02/08/2023, 4:13 PM
Component($"ai-{index}"
This is going to create the same resource name every time it’s invokved, you need to have something in there that invites randomness like a
name
property you can pass
r

rapid-motherboard-85863

02/08/2023, 4:13 PM
that's the point of the index
b

billowy-army-68599

02/08/2023, 4:26 PM
I’m not sure familiar with csharp, but if you’re setting
index = "01"
and then invoking like so
appServicePlanModule.CreateResource()
isn’t that setting the index as 1 every time?
r

rapid-motherboard-85863

02/08/2023, 4:27 PM
no that's just a default value being set
so if I wanted to create two app service plans I would explicitly set the index to something else
I might refactor this so it has to be explicit rather than defaulted
b

billowy-army-68599

02/08/2023, 4:29 PM
I do think a Component is a better way to handle these defaults, you can create a coomponent with a single resource in it that just sets some sane defaults
r

rapid-motherboard-85863

02/08/2023, 4:31 PM
yeah I think I'll give it another look through
thanks for your time