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

2024-06-14 03:50:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Pulumi和Python创建一个GCP存储桶和聚合日志接收器。要创建接收器,我需要Pulumi提供的bucket 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-concat方法,如Pulumi docs中所述

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

print(destination)

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

如有任何建议,将不胜感激


Tags: namecomidoutputobjectbucketstoragedestination
1条回答
网友
1楼 · 发布于 2024-06-14 03:50:15

无法打印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实际上更简单

相关问题 更多 >