如何使用python检索Pulumi资源的属性a?

问题描述

我正在使用Pulumi和Python创建GCP存储桶和聚合日志接收器。要创建接收器,我需要来自Pulumi的存储区ID的值。

bucket = storage.Bucket(resource_name=bucket_name,location="us-central1",storage_class="REGIONAL",uniform_bucket_level_access=True)

# destination value is needed to create the logging sink.
destination = Output.all(bucket.id).apply(lambda l: f"storage.googleapis.com/{l[0]}")

print(destination)

我希望获得类似于"storage.googleapis.com/bucket_id"的目标变量的打印输出。相反,我越来越 <pulumi.output.Output object at 0x10c39ae80>。我也尝试过使用Pulumi docs中所述的Pulumi concat方法

destination = Output.concat("storage.googleapis.com/",bucket.id)

print(destination)

这将返回相同的<pulumi.output.Output object at 0x10c39ae80>而不是预期的字符串。

任何建议将不胜感激。

解决方法

您无法打印Output,因为输出是延迟值的容器,当调用print时,该容器尚不可用。相反,尝试将值导出为堆栈输出

pulumi.export("destination",destination)

如果您真的要打印,请尝试在apply中进行打印:

destination.apply(lambda v: print(v))

顺便说一句,您的第一个代码段可以简化为

destination = bucket.id.apply(lambda id: f"storage.googleapis.com/{id}")

但是concat确实更简单。