【问题标题】:How to get only the OutputKey from boto3 cloudformation describe_stack API?如何仅从 boto3 cloudformation describe_stack API 获取 OutputKey?
【发布时间】:2020-11-27 10:45:42
【问题描述】:

需要帮助!我正在尝试使用 boto3 部署 CloudFormation 堆栈。在 cloudformation 模板中,为 Outputs 参数提供以下代码。当我运行 describe_stack 时,它会显示堆栈的完整描述和所有属性。如何从下面的 OutputKeys 中只获取其中的一些?例如,如果我只想要 PublicDNS 或 PublicIP 怎么办?

Outputs:
  InstanceId:
    Description: InstanceId of the newly created EC2 instance
    Value: !Ref EC2Instance
  AZ:
    Description: Availability Zone of the newly created EC2 instance
    Value: !GetAtt 
      - EC2Instance
      - AvailabilityZone
  PublicDNS:
    Description: Public DNSName of the newly created EC2 instance
    Value: !GetAtt 
      - EC2Instance
      - PublicDnsName
  PublicIP:
    Description: Public IP address of the newly created EC2 instance
    Value: !GetAtt 
      - EC2Instance
      - PublicIp

【问题讨论】:

    标签: python amazon-cloudformation boto3


    【解决方案1】:

    您可以在Stack 的帮助下执行以下操作(例如PublicDNS):

    import boto3
    
    session = boto3.Session(aws_access_key_id='', aws_secret_access_key=''...) 
    
    cloudformation = session.resource('cloudformation')
    
    stack = cloudformation.Stack('<your-stack-name>')
    
    print(stack.outputs)
    
    public_dns = ''
    
    for output in stack.outputs:
        if output['OutputKey'] == 'PublicDNS':
            public_dns = output['OutputValue']
            break
    
    print(public_dns)
    

    【讨论】:

    • 感谢您的快速回复。我已经有一个 boto3 会话和一个创建到 CloudFormation 的客户端。如何为上述输出操作使用相同的会话和客户端? session = boto3.Session(aws_access_key_id='', aws_secret_access_key=''...) cf_client = session.client('cloudformation')
    • @SupreetKulkarni 对不起,我不明白这个问题。只需使用您的会话致电session.resource('cloudformation')
    • 对不起,让我改一下。在同一个 python 代码中,我已经定义了一个会话和客户端来连接到上面提到的 cloudformation。这样,如果我使用cloudformation = boto3.resource('cloudformation'),我将需要再次传递会话凭据。想知道如何避免这种情况。
    • @SupreetKulkarni 我更新了答案。请立即查看。
    猜你喜欢
    • 2020-02-28
    • 1970-01-01
    • 2016-08-31
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2022-01-18
    • 2016-07-12
    相关资源
    最近更新 更多