user1562991
user1562991

Reputation: 123

AWS Boto3 "TypeError: list indices must be integers or slices, not str"

I am trying to get the list of CF Stacks with preferred items "Stack Name", "StackStatus" and "DriftInformation".

I tried the following and it throws an error

"TypeError: list indices must be integers or slices, not str"


import boto3
client = boto3.client('cloudformation')

response = client.list_stacks(
    )

list = response['StackSummaries']['StackName']

print(list)

Here is the response syntax.

   'StackSummaries': [
        {
            'StackId': 'string',
            'StackName': 'string',
            'TemplateDescription': 'string',
            'CreationTime': datetime(2015, 1, 1),
            'LastUpdatedTime': datetime(2015, 1, 1),
            'DeletionTime': datetime(2015, 1, 1),
            'StackStatus': 'CREATE_IN_PROGRESS'|'CREATE_FAILED'|'CREATE_COMPLETE'|'ROLLBACK_IN_PROGRESS'|'ROLLBACK_FAILED'|'ROLLBACK_COMPLETE'|'DELETE_IN_PROGRESS'|'DELETE_FAILED'|'DELETE_COMPLETE'|'UPDATE_IN_PROGRESS'|'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS'|'UPDATE_COMPLETE'|'UPDATE_ROLLBACK_IN_PROGRESS'|'UPDATE_ROLLBACK_FAILED'|'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS'|'UPDATE_ROLLBACK_COMPLETE'|'REVIEW_IN_PROGRESS',
            'StackStatusReason': 'string',
            'ParentId': 'string',
            'RootId': 'string'
        },
    ],
    'NextToken': 'string'
}

I just want to list the stacks with below items. "Stack Name", "StackStatus" and "DriftInformation". Please guide on this.

Thanks!

Update:

import boto3
client = boto3.client('cloudformation')

response = client.list_stacks(
    )

for stack in response['StackSummaries']:

    stack_summary = stack['StackName']

    print(stack_summary)

Upvotes: 0

Views: 1015

Answers (3)

John Rotenstein
John Rotenstein

Reputation: 269171

import boto3
client = boto3.client('cloudformation')

response = client.list_stacks()

for stack in response['StackSummaries']:

    stack_summary = stack['StackName']
    stack_status = stack['StackStatus']
    stack_drift_status = stack['DriftInformation']['StackDriftStatus']
    stack_drift_timestamp = stack['DriftInformation']['LastCheckTimestamp']

Upvotes: 2

Huseyin
Huseyin

Reputation: 96

Returns the description for the specified stack; if no stack name was specified, then it returns the description(use "describe_stacks").

import boto3
client = boto3.client('cloudformation')

response = client.describe_stacks(
  StackName='string',
  StackStatus= 'string',
  DriftInformation': {
            'StackDriftStatus': 'DRIFTED'|'IN_SYNC'|'UNKNOWN'|'NOT_CHECKED',
            'LastCheckTimestamp': datetime(2015, 1, 1)
        } 
)

Upvotes: 0

Marcin
Marcin

Reputation: 238101

It should be the following:

list = response['StackSummaries'][0]['StackName']

StackSummaries is an array.

For full records, how to do it in the loop:

for stack in response['StackSummaries']:

    stack_summary = stack['StackName']

p.s. Don't use list as a variable name.

Upvotes: 1

Related Questions