When using `Deployment.RunAsync<T>` what's t...
# dotnet
b
When using
Deployment.RunAsync<T>
what's the preferred method of awaiting in `T`'s ctor since a ctor can't be async. Doing people do
Task.Run
or is there another way? An example would be wanting to call
await someStack.GetValueAsync()
t
First, try avoiding that (e.g. call
GetOutput
instead of
GetValueAsync
. Second, there is a pattern of 1. Using a helper async method. 2. Returning outputs from it. 3. Invoking it from the constructor and assigning the result to an
Output<T>
property. This way, Pulumi would know how to await it properly using the output. See an example here: https://github.com/pulumi/pulumi/blob/master/pkg/codegen/internal/test/testdata/aws-eks.pp.cs#L13-L17
b
That makes sense. I'm missing something when trying to do similar where instead of creating resources, get values from a StackReference.
Copy code
namespace StackReferenceExample {
    public class SimpleStack : Stack {
        [Output( "vpc" )]
        public Output<Output<object?>> Vpc { get; set; }

        public SimpleStack( ) {

            Vpc = Output.Create( Initialize( ) );
        }

        private async Task<Output<object?>> Initialize( ) {
            var vpcStack = new StackReference( "orionadvisortech/vpc/sandbox" );
            var vpc = vpcStack.GetOutput( "vpc" );

            return vpc;
        }
    }
}
t
Copy code
Output<Output<object?>>
doesnโ€™t look correct
b
Probably. This seems to be all the docs on stack refs, not a ton. So I'm doing alot of guessing and wading through the source.
t
Copy code
public class SimpleStack : Stack {
            [Output( "vpc" )]
            public Output<object?> Vpc { get; set; }
            public SimpleStack( ) {
                Vpc = Output.Create( Initialize( ) );
            }
            private async Task<object?> Initialize( ) {
                var vpcStack = new StackReference( "orionadvisortech/vpc/sandbox" );
                var vpc = await vpcStack.GetValueAsync( "vpc" );
                return vpc;
            }
        }
if you need
async
for something else
๐Ÿ‘ 1
Copy code
public SimpleStack( ) {
                var vpcStack = new StackReference( "orionadvisortech/vpc/sandbox" );
                this.Vpc = vpcStack.GetOutput( "vpc" );
            }
otherwise
๐Ÿ‘ 1
Copy code
public SimpleStack( ) {
                Vpc = Output.Create( Initialize( ) ).Apply(v => v);
            }
            private async Task<Output<object?>> Initialize( ) {
                var vpcStack = new StackReference( "orionadvisortech/vpc/sandbox" );
                var vpc = vpcStack.GetOutput( "vpc" );
                return vpc;
            }
If
vpc
is a secret and you need async for something else
My preference is 2, 1, 3
b
Why would this cause this build error?
Maybe not a build error, it happens on
pulumi preview
ignore ๐Ÿ™‚
changed to:
Vpc = vpcStack.GetOutput( "vpc" ).Apply(x => (string)x);
This all makes sense now, thanks for the help!
๐Ÿ‘ 1