简体   繁体   中英

boto3: How to get the IP addresses of CloudFormation stack instances?

I need to discover the private IP address for every host in a AWS CloudFormation stack.

The CloudFormation API (see http://boto3.readthedocs.io/en/latest/reference/services/cloudformation.html ) doesn't seem to have any direct support for extracting nodes given a stack ID. Is it even possible?

Yes it is possible but not straightforward.

  1. Get list of stacks in your account/profile
  2. Loop through the list and create a list of stack names
  3. Get the stack resources (describe_stack_resources) for each stack(name)
  4. Locate the resource where resource['LogicalResourceId'] == 'Ec2Instance'
  5. Get the inst_id from that resource
  6. Once you have the inst_id, you can get its attributes including private_ip using boto3.resource('ec2')

I have coded this and use it regularly. AWS may throttle your CF calls, if it is called too often.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM