Working with Pulumi Objects and Python Serialization
Pulumi has the concept of Inputs and Outputs which sometimes require a bit of awareness when working with them. A recent use case we had was the need to output a complex Pulumi object into an environment variable to use within an Azure app service docker deployment.
The storage account keys object was already being exported to the stack’s output using the Pulumi export function, with the addition of an apply() to make it work:
pulumi.export("sa_keys",
pulumi.Output.all(sa.name, rg.name).apply(
lambda args: storage.list_storage_account_keys(
account_name=args[0], resource_group_name=args[1]
),
),
)
This creates a JSON string in the outputs, which contains the storage account keys.
However, the mistake was attempting the same setup directly in the environment variables for the WebApp configuration, as follows:
web.NameValuePairArgs(
name=”SA_KEYS”,
value=pulumi.Output.all(
general_storage_account.name, resource_group.name
).apply(
lambda args: storage.list_storage_account_keys(
account_name=args[0], resource_group_name=args[1]
),
),
),
This resulted in a ValueError:
ValueError: unexpected input of type ListStorageAccountKeysResult
Obviously, the issue is that the list_storage_account_keys() call returns an object, whereas the value= option requires a string. So, for a change we got the apply() correct, but messed up on simpler issue of the correct output type.
Quick fix was to add a json.dumps() and used the __dict__ value of of the list_storage_account_keys() call:
web.NameValuePairArgs(
name=”SA_KEYS”,
value=pulumi.Output.all(
general_storage_account.name, resource_group.name
).apply(
lambda args: json.dumps(
storage.list_storage_account_keys(
account_name=args[0], resource_group_name=args[1]
).__dict__
),
),
),
And the result is a valid JSON string in the environment variable for the storage account keys.