mammoth-optician-14118
05/17/2023, 3:35 PMimport 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:
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:
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.billowy-army-68599
05/17/2023, 4:23 PMelasticsearch_output = program.small_cluster.elasticsearch.apply(lambda es: es.__dict__)
print(f'elasticsearch_output {elasticsearch_output}')
Should be like this:
elasticsearch_output = program.small_cluster.elasticsearch.apply(lambda es: print(es))
mammoth-optician-14118
05/17/2023, 4:31 PMpulumi.export("id", self.small_cluster.id)
and then accessed it in my code with:
id = up_res.outputs['id'].value
I assume this is the correct way?billowy-army-68599
05/17/2023, 4:38 PMmammoth-optician-14118
05/17/2023, 4:49 PM