This message was deleted.
# general
s
This message was deleted.
i
another use case, I want to compile JSON.stringify() for new pulumi.asset.StringAsset(JSON), to write to my storage account with some Output<string> value. Any suggestions? @colossal-beach-47527
is there any way to convert Output into promise and wait for value? I think the problem, that StringAsset() is accepting or string / or Promise, and I am not waiting for values there… is it?
Copy code
// @ts-ignore
const additionalConfig = configuration.promise().then((t) =>
  xml.json2xml(t, {
    spaces: 4,
    indentText: true,
    compact: true,
  })
);

export const conf = new azure.storage.Blob(
  "data.xml",
  {
    storageAccountName: storageAccount.name,
    storageContainerName: container.name,
    name: "data.xml",
    type: "Block",
    source: new pulumi.asset.StringAsset(additionalConfig),
  },
  { dependsOn: [container, storageAccount] }
);
this works as workaronud
why Output.promise() is internal? 🙂
c
I ran into the same thing when I first started using Pulumi, and I had a lot of trouble getting used to the idea that promises don't work very well with Pulumi. I figure part of that is because Pulumi isn't waiting for an API call to return, but it's actually waiting on an object to create. Internally, Pulumi knows how to resolve the dependencies, so I'm able to pass Pulumi objects to other Pulumi objects and it all works out.
Using
.apply
can sometimes get you the value you're after as well.
I'm not sure if this is the type of thing you're trying to accomplish:
Copy code
pulumi.output(secretsToStore).apply(JSON.stringify)
i
.apply returns an Output<string>, if you do toString on it (fs.write) you will get issues 😞
same happened with js2xml (https://www.npmjs.com/package/xml-js), I was not able to make it until using the workaround.
I need to generate xml config on the fly, and put that into blob storage
c
I’ll admit that I don’t know a lot of the details about
Output< >
and
pulumi.apply
work. But my (perhaps incorrect) understanding is that you should really try to avoid relying on the state of a resource for the execution of your Pulumi program. e.g. if you want to write the
ec2VM.id
to a file or something. Because there isn’t a good way to reliably obtain that value. If you are running a
pulumi preview
(sometimes called a dry run), the resource is never created. So
ec2VM.id
never actually has a value. (And throwing a runtime error is probably better than giving you “” or undefined, or something that looks correct but isn’t.) The way
Output< >
is used internally in the Pulumi engine is to ensure that operations are sequenced correctly. e.g. if resource B requires the id of resource A, then Pulumi will wait for the first resource to be created, then read the constructed value, etc. Typically this “just works”, but in more advanced cases you’d need to do something like:
Copy code
// Wait for resource A, B, C to be created.
// Then pass all their values to a lambda,
// return some Output< >
pulumi
    .all([resA, resB, resC])
    .apply( ([resAval, resBval, resCval]) => {
    return ...
});
But the above code again only works when doing a
pulumi up
. (When
resA
,
resB
, and
resC
have been created and/or are “known”.) That is my vague/hazy understanding, so as for your specifc question:
If I want to fs.write 
Output<string>
 , how I can do that?
My (again, perhaps incorrect/incomplete/wrong) understanding is: 1. You might not want to be doing that, it might be a code smell. 2. as an FYI, getting the
Output<>
won’t work in “previews” and only “updates”. 3. “something something
pulumi.all(…).apply(…)
might get you the value, but unfortunately will itself return an
Output< >
. (So you’d see it as a side-effect during the Pulumi program’s execution, but never during a preview, etc.) Hopefully that is some non-zero amount of helpful. (And maybe someone else on the team more knowledgeable can point you in a clearer direction.)
i
Thanks for the reply @colossal-beach-47527, not sure if I am doing smth wrong, as I said my use case: 1. create blob storage 2. create a config.xml with 2 values (blob storage, blob access key) 3. store this file inside blob storage 4. get file path and create final resource, that needs to know where is config.xml
the final resource is 3rd party service, I can’t change the way it works 😞 can’t change configuration of it using ENVs, etc
they require me to have this xml config files already inside blob storage account