https://pulumi.com logo
#automation-api
Title
# automation-api
w

worried-lunch-10568

09/20/2021, 10:53 AM
Hey! What is the correct way to get a stack reference in async task in C#?
Copy code
class Program
    {
        static async Task<int> Main(string[] args)
        {
            var stack = new Pulumi.StackReference("path to stack");
I get error
Copy code
Unhandled exception. System.InvalidOperationException: Trying to acquire Deployment.Instance before 'Run' was called.
   at Pulumi.Deployment.get_Instance()
   at Pulumi.Deployment.get_InternalInstance()
   at Pulumi.Resource..ctor(String type, String name, Boolean custom, ResourceArgs args, ResourceOptions options, Boolean remote, Boolean dependency)        
   at Pulumi.CustomResource..ctor(String type, String name, ResourceArgs args, CustomResourceOptions options, Boolean dependency)
   at Pulumi.CustomResource..ctor(String type, String name, ResourceArgs args, CustomResourceOptions options)
   at Pulumi.StackReference..ctor(String name, StackReferenceArgs args, CustomResourceOptions options)
   at Program.Main(String[] args) in C:\...\Program.cs:line 34
   at Program.<Main>(String[] args)
b

brave-planet-10645

09/20/2021, 11:25 AM
All your code needs to be in the class that inherits from
Stack
You don't use
StackReference
to get stack information, you use it to access outputs from one stack in another: https://www.pulumi.com/docs/intro/concepts/stack/#stackreferences
w

worried-lunch-10568

09/20/2021, 12:19 PM
The use case that I'm trying to implement is to use
getVirtualNetwork
method and get existing subnets to calculate next cidr block and then use that cidr block as input for another Stack. If stack references are not designed to be used like that is there the right way for this case?
b

brave-planet-10645

09/20/2021, 12:59 PM
No, that sounds find. So in the first stack you would have the vnet id as the output and then the other stacks would use that and the
getVirtualNetwork
method and to get the address blocks
w

worried-lunch-10568

09/21/2021, 4:52 AM
so, it's not possible to do it as a part of automation? the steps i was envisioning were: 1. automation script will get created subnets from Stack output's virtual network 2. script will figure out next available cidr block and put that as an input for other Stack I wanted to use
getVirtualNetwork
as part of the automation script but couldn't figure this out. I hope it's clearer what I'm trying to achieve here
r

red-match-15116

09/21/2021, 3:02 PM
Correct, you cannot use getVirtualNetwork in the automation script. You can only use that function within the pulumi program itself. However you can use the relevant SDK directly to get that information within the script if you’d rather.
w

worried-lunch-10568

09/22/2021, 7:04 AM
got it, thanks!
15 Views