Boto3 Cloudformation Drift Status - PullRequest
       17

Boto3 Cloudformation Drift Status

1 голос
/ 21 февраля 2020

Я пытаюсь l oop пройти через каждый регион и проверить, сместился ли стек, а затем распечатать список смещенных стеков.

# !/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 и я не уверен, почему происходит сбой в StackName в строке 22, когда я знаю, что это имя переменной в "detect_stack_drift", которую я пытаюсь проанализировать. Была бы признательна за помощь!

1 Ответ

0 голосов
/ 21 февраля 2020

См. Эти строки:

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.

...