Hello, New to Pulumi because we hit some limitatio...
# python
m
Hello, New to Pulumi because we hit some limitations with TF so we are testing this out, but having a little difficulty with outputs. For instances, lets say I create a class to easily create Elastic infrastructure:
Copy code
import pulumi
import pulumi_ec as ec

class Cluster:

    def __init__(self, name, region, version, rsc_id):
        self.name = name
        self.region = region
        self.version = version
        self.rsc_id = rsc_id
        self.small_cluster = None

    def create_small_cluster(self):
        try:
            self.small_cluster = ec.Deployment(self.name,
                region=self.region,
                version=self.version,
                deployment_template_id="aws-storage-optimized-v4",
                elasticsearch=ec.DeploymentElasticsearchArgs(
                    autoscale="false",
                    topologies=[
                        ec.DeploymentElasticsearchTopologyArgs(
                            id="cold",
                            size="2g",
                            zone_count="1"
                        ),
                        ec.DeploymentElasticsearchTopologyArgs(
                            id="frozen",
                            size="4g",
                            zone_count="1"
                        ),
                        ec.DeploymentElasticsearchTopologyArgs(
                            id="hot_content",
                            size="4g",
                            zone_count="2"
                        ),
                        ec.DeploymentElasticsearchTopologyArgs(
                            id="ml",
                            size="1g"
                        ),
                        ec.DeploymentElasticsearchTopologyArgs(
                            id="warm"
                        ),
                    ],
                ),
            kibana=ec.DeploymentKibanaArgs(
                topology=ec.DeploymentKibanaTopologyArgs(
                    size="1g"
                )
            ),
            integrations_server=ec.DeploymentIntegrationsServerArgs(
                topology=ec.DeploymentIntegrationsServerTopologyArgs(
                        size="1g"
                    )
            ),
            enterprise_search=ec.DeploymentEnterpriseSearchArgs(
                topology=ec.DeploymentEnterpriseSearchTopologyArgs(
                    size="2g"
                )
            ),
            observability=ec.DeploymentObservabilityArgs(
                deployment_id=self.rsc_id
            ))
        except Exception as e:
            raise e
Then I call this class ito create a small cluster:
Copy code
project_name = "small-cluster"
stack_name = auto.fully_qualified_stack_name("acme", project_name, "dev")

version = "8.7.1"
region = "aws-ca-central-1"
name = "test-cluster"
rsc_id = "self"

program = clusters.Cluster(name, region, version, rsc_id)

stack = auto.create_or_select_stack(stack_name=stack_name,
                                    project_name=project_name,
                                    program=program.create_small_cluster)

print("Refreshing stack...")
stack.refresh(on_output=print)
print("Refresh complete")

if destroy:
    print("destroying stack...")
    stack.destroy(on_output=print)
    print("Stack destroy complete")
    sys.exit()

print("Updating stack...")
up_res = stack.up(on_output=print)
print(f"update summary: \n{json.dumps(up_res.summary.resource_changes, indent=4)}")
Now how can I reference resource properties further along in the code? I have read up on 'apply' but this never seems to give me an output, though I can see the resource and its properties (name, value) in the Pulumi Cloud stack. If I try this for instance:
Copy code
elasticsearch_output = program.small_cluster.elasticsearch.apply(lambda es: es.__dict__)
print(f'elasticsearch_output {elasticsearch_output}')
I get nothing. I have tried many variations of this, along with other stuff I have found online but I can't seem to just get the properties for the resources. This limits me significantly as I need to reference things like the Elasticsearch ID further along in my code. I know the ID is a valid property of my resource as I can find its value manually in the Pulumi Cloud GUI stack details, but I can't figure out how to call it in Python. This may be something simple, but I can't seem to figure it out.
b
@mammoth-optician-14118 when you say futher along in the code, what do you mean? Concretely, this code:
Copy code
elasticsearch_output = program.small_cluster.elasticsearch.apply(lambda es: es.__dict__)
print(f'elasticsearch_output {elasticsearch_output}')
Should be like this:
Copy code
elasticsearch_output = program.small_cluster.elasticsearch.apply(lambda es: print(es))
but it looks like you’re trying to use an output from automation API, which isn’t how it works
outputs work to create a graph inside a pulumi program. Automation API is the driver of a program. You can’t directly access outputs from automation API
m
Ya I re-read the documentation and saw that. I figured out how to solve my problem with the automation api the proper way leveraging pulumi.export and up_res.outputs.
It gives me the results I need.
For instance I appended this to the cluster:
Copy code
pulumi.export("id", self.small_cluster.id)
and then accessed it in my code with:
Copy code
id = up_res.outputs['id'].value
I assume this is the correct way?
b
yeah that looks correct!
m
Perfect! Thanks!