Boto3 Cloudformation漂移状态

问题描述 投票:1回答:1

我试图遍历每个区域,检查堆栈是否漂移,然后打印已漂移堆栈的列表。

# !/usr/bin/env python
import boto3
import time

## Create a AWS Session
session = boto3.Session(profile_name='default', region_name='us-east-1')

if __name__ == '__main__':
    ## Connect to the EC2 Service
    client = session.client('ec2')

    ## Make a list of all the regions
    response = client.describe_regions()

    for region in response['Regions']:
        name = region['RegionName']

        print("Drifted CFn in region: " + name)
        ## Connect to the CFn service in the region
        cloudformationClient = boto3.client("cloudformation")
        stacks = cloudformationClient.describe_stacks()
        detection_id = cloudformationClient.detect_stack_drift(StackName=stacks)
        for stack in stacks['Stacks']:
            while True:
                time.sleep(3)
                # sleep between api calls to prevent lockout
                response = cloudformationClient.describe_stack_drift_detection_status(
                    StackDriftDetectionId=detection_id
                )
                if response['DetectionStatus'] == 'DETECTION_IN_PROGRESS':
                    continue
                else:
                    print("Stack" + stack + " has a drift status:" + response)

我还是Python的新手,当我知道那是我要解析的“ detect_stack_drift”中变量的名称时,不确定为什么它在第22行的StackName上失败。一些帮助,将不胜感激!

amazon-web-services amazon-cloudformation boto3
1个回答
0
投票

请参见这些行:

stacks = cloudformationClient.describe_stacks()
detection_id = cloudformationClient.detect_stack_drift(StackName=stacks)

describe_stacks()调用返回:

{
    'Stacks': [
        {
            'StackId': 'string',
            'StackName': 'string',
            ...
        },
    ],
    'NextToken': 'string'
}

但是,detect_stack_drift()函数期望StackName中有单个字符串。

© www.soinside.com 2019 - 2024. All rights reserved.