首页 > 解决方案 > Get KeyValue from Cloudformation Output with Python Boto3

问题描述

I am trying to use Boto3 to print out the instance private IP address from a Cloudformation stack Output. It should be a fairly straightforward process. However my code just refuse to work.

The Outputs section of the describe_stacks response is below:

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

I have tested my code below. It prints out nothing.

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"])

Though if I try

print(keyName)

It does printout EC2IP and ImageID So keyName in this case should match EC2IP and then print out the IP. But somehow I get nothing...

标签: python-3.xamazon-web-servicesamazon-cloudformationboto3

解决方案


代替:

if keyName is "EC2IP":

利用:

if keyName == "EC2IP":

推荐阅读