【问题标题】:Get KeyValue from Cloudformation Output with Python Boto3使用 Python Boto3 从 Cloudformation 输出中获取 KeyValue
【发布时间】:2020-02-28 06:59:57
【问题描述】:

我正在尝试使用 Boto3 从 Cloudformation 堆栈输出中打印出实例私有 IP 地址。这应该是一个相当简单的过程。但是我的代码只是拒绝工作。

describe_stacks 响应的输出部分如下:

{'OutputKey': 'EC2IP', 'OutputValue': '192.168.10.10', 'Description': 'Web Server IP Address'},
{'OutputKey': 'ImageID', 'OutputValue': 'ami-0888888888888', 'Description': 'Web Server Image ID'}

我在下面测试了我的代码。它什么也没打印出来。

import boto3
cf_client = boto3.client('cloudformation')
stackname = 'test-instance-stack'

response = cf_client.describe_stacks(StackName=stackname)
outputs = response["Stacks"][0]["Outputs"]
   for output in outputs:
        keyName = output["OutputKey"]
        if keyName is "EC2IP":
            print(output["OutputValue"])

如果我尝试

print(keyName)

它会打印出EC2IPImageID 所以在这种情况下,keyName 应该匹配 EC2IP,然后打印出 IP。但不知怎的,我什么也没得到......

【问题讨论】:

  • 这里解释了 ==is 之间的区别。 == 运算符比较两个操作数的值并检查值是否相等。而 is 运算符检查两个操作数是否引用同一个对象。

标签: python-3.x amazon-web-services amazon-cloudformation boto3


【解决方案1】:

代替:

if keyName is "EC2IP":

用途:

if keyName == "EC2IP":

【讨论】:

    猜你喜欢
    • 2020-11-27
    • 2017-05-28
    • 2022-01-18
    • 2020-04-03
    • 2018-03-06
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多