不幸的是,aws不支持按名称过滤输出。但是很容易做一个过滤器:
#!/usr/bin/python
import sys
import boto3
import rest_client
OUTPUT_KEY = 'InstanceIp' # <-- Use the proper output name here
if len(sys.argv) < 2:
print "Bad usage: missing stack name"
exit(1)
stack_name = sys.argv[1]
session = boto3.Session(profile_name='profile name')
cf_resource = session.resource('cloudformation')
stack = cf_resource.Stack(stack_name)
try:
ip = filter(lambda x: x['OutputKey'] == OUTPUT_KEY, stack.outputs)[0]['OutputValue']
print "Extracted instance IP address ({0})".format(ip)
except IndexError:
print OUTPUT_KEY + " not found in " + stack_name
exit(1)
另外,我可以向您保证,这是未来的证明,因为一旦api正式发布,他们(据我所知)永远不会更新其响应有效负载的语法。