https://pulumi.com logo
Title
g

gorgeous-minister-41131

02/16/2023, 12:42 AM
What is the best/recommended way to compute a simple sha256 hash of a dictionary whose keys a a type
str
, and their values could be
pulumi.Output[str]
?
g

great-sunset-355

02/16/2023, 12:55 PM
that's something you do not want to do, unless you can afford doing this in the scope of
apply
e

echoing-dinner-19531

02/16/2023, 1:10 PM
I think something like:
Output.json_dumps(dictionary, sort_keys=True).apply(lambda json: sha256(json.encode('utf-8')).hexdigest())
g

gorgeous-minister-41131

02/16/2023, 5:34 PM
I'm fine with the expense, but it looks like the Output.json_dumps() creates a non-deterministic value.. which is weird since the dictionary's resultant values haven't changed..
the actual outcome results in the same sha256, but Pulumi keeps 'detecting' change where I'm using the resulting output of json_dumps (since it's just an Output[str])
e

echoing-dinner-19531

02/16/2023, 6:03 PM
Have you set "sort_keys=True"? I'd think that should result in deterministic results. Can you share a copy of what the engine thinks the diff is?
g

gorgeous-minister-41131

02/17/2023, 2:05 AM
I did, I just realized what it was... I was trying to use the output directly on something instead of running yet another apply against it, so it was returning with the "You can't run str On an Output[T]" bogus... I really think that should raise an exception... 🙂 not create surprise rubbish strings that aren't truly the Output value.
It says it might start throwing in the future... hopefully that happens. 😄 Anyway, works great @echoing-dinner-19531, thanks! I wasn't even aware Output had its own json_dumps; saves me from having to do all that work myself.