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

broad-hairdresser-1495

04/30/2021, 3:57 PM
Hi, When using
pulumi.export()
is it possible to use that output to a variable and use this data later in the code?
Copy code
my_volumeID = pulumi.export('volume_ID', volume.id)
or is it just for output?
b

better-actor-92669

04/30/2021, 4:05 PM
you can use
volume.id
directly. Or, if you need to use
apply()
, something like
Copy code
member=service_account.email.apply(
        lambda x: 'serviceAccount:' + x
    )
b

broad-hairdresser-1495

04/30/2021, 4:11 PM
I use
volume.id
in Pulumi :
Copy code
ebs_att = aws.ec2.VolumeAttachment(volume_["tags"]["Name"],
                                               device_name=volume_["device_name"],
                                               volume_id=volume.id,
                                               instance_id=server.id)
but that is for aws Pulumi functions. i want to write that volume.id to file:
Copy code
fw = open("myFile.txt", "w")
fw.write(volume.id)
fw.close()
b

better-actor-92669

04/30/2021, 4:30 PM
In this case, you need to wrap it in a function, so that values are interpolated in runtime
Copy code
def pulumi_output_renderer_kps(args_to_render) -> None:

    from service_scripts.template_files import template_config

    jinja2_variables_dict_kps = {
        # TODO: define varialbes for KPS values
        'project_name': project_name,
        'project_zones': gcp_region_zones,
    }

    template_config(
        args_to_render[0],
        args_to_render[1],
        jinja2_variables_dict_kps,
        render_in_place=False,
        write_to_stdout=False,
    )

Output.all(
    kps_additional_scrape_configs_out_file_location,
    kps_additional_scrape_configs_j2_location,
).apply(pulumi_output_renderer_kps)
b

broad-hairdresser-1495

04/30/2021, 4:37 PM
thanks, will look into that, found similar approach in here: https://github.com/pulumi/pulumi/issues/3722#issuecomment-650654006
r

red-match-15116

04/30/2021, 4:44 PM
Copy code
def write_id_to_file(id):
    with open("myFile.txt", "w") as fw:
        fw.write(id)

volume.id.apply(write_id_to_file)
❤️ 1
b

broad-hairdresser-1495

05/04/2021, 7:45 AM
@red-match-15116 thanks for the elegant code. This looks good. But how can I send additional parameter into this function?
Copy code
def write_id_to_file(id, instanceName): 
    with open("myFile.txt", "w") as fw:
        fw.write(id + instanceName)
volume.id.apply(write_id_to_file, instanceName)
I tested out with global variable, but that did not work when there is multiple instances.
Now, I got this. Thanks both of you @red-match-15116 and @better-actor-92669
Copy code
def pulumi_output_renderer(args_to_render):
    with open("myFile.txt", "a") as fw:
        fw.write(args_to_render[0] + " - " + str(args_to_render[1]["Name"]) + "\n")

pulumi.Output.all(volume.id, host.tags).apply(pulumi_output_renderer)
also more info can be found here: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/
partypus 8bit 1
👍🏽 1
4 Views