【问题标题】:How do I retrieve property a of Pulumi resource using python?如何使用python检索Pulumi资源的属性a?
【发布时间】:2021-02-14 03:08:49
【问题描述】:

我正在使用 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> 而不是预期的字符串。

任何建议将不胜感激。

【问题讨论】:

    标签: python google-cloud-platform pulumi


    【解决方案1】:

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

    pulumi.export("destination", destination)
    

    如果您真的想打印它,请尝试在apply 中这样做:

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

    顺便说一下,你的第一个 sn-p 可以简化为

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

    concat 确实更简单。

    【讨论】:

    • 谢谢,这很有道理。我无法确定的是如何将该目标字符串值构建为组织接收器资源的输入。目标值是字符串 storage.googleapis.com/ 沿着我想要的存储桶连接。
    • 例如,在创建存储桶后,我正在使用 sink = logging.OrganizationSink("resource-ops-sink", destination=f"storage.googleapis.com/{bucket.id}", filter=filter, org_id=org) 我在目标变量 ``` gcp:logging:OrganizationSink (resource-ops-sink) 上收到错误:错误:发生 1 个错误:* googleapi: 错误 404: Bucket 不存在,未找到```
    • 使用destintion=Output.concat("storage.googleapis.com/", bucket.id)而不是直接格式化字符串
    猜你喜欢
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 2022-11-11
    • 1970-01-01
    • 2011-07-06
    • 2021-10-31
    • 1970-01-01
    相关资源
    最近更新 更多