https://pulumi.com logo
#dotnet
Title
# dotnet
b

bumpy-grass-54508

05/18/2021, 1:24 PM
Can Assets and Archives safely be created in an Output.Apply? I know resources are not meant to be created there but AssetOrArchive does not look to be related. I have not gathered enough info to have a fully reproducible problem (will create a github issue once I do), but I ask because I am having issues dynamically creating an AWS Lambda function based on a StringAsset:
Copy code
Output<string> handlerCode = ... // based on other outputs in the stack
var handlerArchive = handlerCode.Apply(code => (Archive)new AssetArchive(new Dictionary<string, AssetOrArchive>
{
    ["index.js"] = new StringAsset(code),
}));

var function = new Pulumi.Aws.Lambda.Function("function", new Pulumi.Aws.Lambda.FunctionArgs
{
    Code = handlerArchive,
    Handler = "index.handler",
    Role = role.Arn,
    Runtime = Pulumi.Aws.Lambda.Runtime.NodeJS14dX,
});
creating the function works fine, and subsequent
up
operations sometimes work fine - but in one particular stack I am working on I consistently get this error
Copy code
System.InvalidOperationException: Expected Pulumi.Archive but got System.String deserializing Pulumi.Aws.Lambda.Function.code
   at Pulumi.Serialization.Converter.ConvertObject(String context, Object val, Type targetType)
   at Pulumi.Serialization.Converter.ConvertValue(String context, Value value, Type targetType, ImmutableHashSet`1 resources)
   at Pulumi.Deployment.CompleteResourceAsync(Resource resource, Boolean remote, Func`2 newDependency, ResourceArgs args, ResourceOptions options, ImmutableDictionary`2 completionSources)
   at Pulumi.Deployment.Runner.<>c__DisplayClass9_0.<<WhileRunningAsync>g__HandleCompletion|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Pulumi.Deployment.Runner.WhileRunningAsync()<{%reset%}>)
want to double check I should even be creating this Archive in an Apply or if that is not supported
t

tall-librarian-49374

05/18/2021, 1:37 PM
Not exactly an answer to your question, but you should be able to avoid this. I would try to build a dictionary inside apply and move the resource constructor outside, somewhat similar to https://github.com/pulumi/pulumi-azure/blob/e07905225513d0adfaceac1633ac8e5d8c815c43/sdk/nodejs/appservice/zMixins.ts#L408-L424
b

bumpy-grass-54508

05/18/2021, 1:47 PM
ah that go code does look promising - but it looks like in the go sdk you can add assets and outputs to this map? in c# sdk i believe you can only use a dictionary of AssetOrArchive, so that's why I wrapped the whole archive in an apply instead of just the stringasset:
oops ignore me that is not go code 🤦‍♂️ i mean typescript
yeah it looks like the typescript asset and archive types each take in things like
string | Promise<string>
and
AssetMap | Promise<AssetMap>
etc, but the c# AssetArchive only takes in a
Dictionary<string, AssetOrArchive>
and StringAsset only accepts a
string
I have created a github issue with some more info https://github.com/pulumi/pulumi/issues/7076 Still working on getting a minimal workable example that actually fails similarly to my stack
t

tall-librarian-49374

05/18/2021, 2:46 PM
ah, that’s a notable difference in the API indeed. I am not sure why it exists.
b

bumpy-grass-54508

05/18/2021, 9:13 PM
I updated the github issue but it looks like it isn't a problem with outputs at all. it is
refresh
that is rewriting the archive's representation in the state file. output<t> was just a red herring and/or a bad hunch! hah
t

tall-librarian-49374

05/18/2021, 9:15 PM
aha! I think this has been reported before.
Ah, I can see you linked it already
b

bumpy-grass-54508

05/18/2021, 9:18 PM
haha inb4 - i can at least for now skip
pulumi refresh
and keep going. i might be able to poke around the refresh implementation at a later point but let me know if i can provide any more info to help out!