4

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...

1
  • Here is a explanation for the difference bewteen == and is. The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not. Commented Nov 2, 2019 at 6:05

1 Answer 1

4

Instead of:

if keyName is "EC2IP":

Use:

if keyName == "EC2IP":
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.