https://pulumi.com logo
#python
Title
# python
p

polite-mechanic-60124

12/07/2021, 9:15 PM
I'm running into a python pulumi oddity. I'm rendering a jinja template with variables that are pulumi secrets ie of type pulumi.Output[str] and base64ing it to be used by a user_data field.
Copy code
user_data=pulumi.Output.all(
    {
        "cluster_size": cluster_size,
        "secret_id": secret_id,
    }
).apply(
    lambda args: render_user_data_output(
        templating_env(),
        "userdata-server.sh.jinja",
        *args,
    )
),
Now for the weird part. If render_user_data_output returns a string, I get a base64 encoded pulumi object address in memory. If it returns a pulumi Output, then it renders the base64 encoded secret. Both versions defined below
Copy code
def render_user_data_output(
        env: Environment, template_name: str, kwargs: Mapping[str, Any]
) -> str:
    rendered_template = render_template_file(env, template_name, kwargs)
    return base64.b64encode(rendered_template.encode("utf-8")).decode("utf-8")
Copy code
def render_user_data_output(
        env: Environment, template_name: str, kwargs: Mapping[str, Any]
) -> pulumi.Output:
    rendered_template = render_template_file(env, template_name, kwargs)
    return pulumi.Output.from_input(base64.b64encode(rendered_template.encode("utf-8")).decode("utf-8"))
Does anyone know what's going on here?
p

prehistoric-activity-61023

12/07/2021, 9:21 PM
just to make sure, the version returning
pulumi.Output
works as expected?
p

polite-mechanic-60124

12/07/2021, 9:21 PM
Yep
And I'm on python 3.7
p

prehistoric-activity-61023

12/07/2021, 9:24 PM
is
render_template_file
defined by you?
if so, can you paste its code here as well so others could easily repro this?
p

polite-mechanic-60124

12/07/2021, 9:38 PM
Yep, 1 minute
Copy code
from jinja2 import Environment, PackageLoader, StrictUndefined, select_autoescape

def templating_env() -> Environment:
    """Returns a Jinja templating environment that pulls templates from the `infra.templates` package.

    If any expected variables are missing from a template rendering,
    an error will be thrown.

    """
    return Environment(
        loader=PackageLoader("infra"),
        autoescape=select_autoescape(),
        undefined=StrictUndefined,
    )


def render_template_file(
    env: Environment, template_name: str, kwargs: Mapping[str, Any]
) -> str:
    """
    Render the given Jinja template with the given arguments.
    """
    template = env.get_template(template_name)
    return template.render(**kwargs)