【发布时间】: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)
它会打印出EC2IP 和ImageID
所以在这种情况下,keyName 应该匹配 EC2IP,然后打印出 IP。但不知怎的,我什么也没得到......
【问题讨论】:
-
这里解释了 == 和 is 之间的区别。 == 运算符比较两个操作数的值并检查值是否相等。而 is 运算符检查两个操作数是否引用同一个对象。
标签: python-3.x amazon-web-services amazon-cloudformation boto3