Milister
Milister

Reputation: 658

list indices must be integers, not str -Python

I'm trying to modify this python function to monitor Lambda functions in Zabbix (automatically create items/triggers for every lambda function)

https://github.com/omni-lchen/zabbix-cloudwatch/blob/master/awsLLD.py#L182

import re
import json
from optparse import OptionParser
from awsAccount import awsAccount
from awsConnection import awsConnection

def config_parser():
    parser = OptionParser(usage="usage: %prog [options]", version="%prog 1.0")
    parser.add_option("-a", "--account", dest="accountname", help="account name", metavar="ACCOUNT")
    parser.add_option("-r", "--region", dest="region", help="region", metavar="REGION")
    parser.add_option("-q", "--query", dest="query", help="specify a query", metavar="QUERY")
    parser.add_option("-c", "--component", dest="component", help="component name", metavar="COMPONENT")
    return parser


def get(a, r):
    account = a
    aws_account = awsAccount(account)
    aws_access_key_id = aws_account._aws_access_key_id
    aws_secret_access_key = aws_account._aws_secret_access_key
    aws_region = r
    #component = c
    # Init LLD Data
    lldlist = []
    llddata = {"data":lldlist}

    # Connect to Lambda service

    conn = awsConnection()
    conn.lambdaConnect(aws_region, aws_access_key_id, aws_secret_access_key)
    lambdaConn = conn._aws_connection

    # Save Lambda function results in a list

    functionResultsList = []
    # Save topic names in a list
    tdata = []

    # Get a list of Lambda Functions

    functionResults = lambdaConn.list_functions()
    functionResultsList.append(functionResults)
    print functionResults
    nextmarker = functionResults['Functions']['NextMarker']


if __name__ == '__main__':
    parser = config_parser()
    (options, args) = parser.parse_args()

    account = options.accountname
    region = options.region
    query = options.query


get(account,region)

print functionResults gives these results (2 functions)

{u'Functions': [{u'Description': u'',
                 u'LastModified': u'2018-08-01T18:50:04.214+0000',
                 u'ConfigurationId': u'e97b805a-c947-4c56-9a2e-5bef3c4cc6c5',
                 u'CodeSize': 222,
                 u'FunctionARN': u'arn:aws:lambda:eu-west-1:233135199200:function:test',
                 u'MemorySize': 128,
                 u'Handler': u'lambda_function.lambda_handler',
                 u'Role': u'arn:aws:iam::233135199200:role/lambda_basic_execution',
                 u'Mode': u'event',
                 u'Timeout': 3, u'Runtime': u'python2.7',
                 u'FunctionName': u'test'},
                {u'Description': u'',
                 u'LastModified': u'2018-06-18T12:17:34.362+0000',
                 u'ConfigurationId': u'b3c59ce0-f028-43b2-8c34-a73d2bb41782',
                 u'CodeSize': 1436,
                 u'FunctionARN': u'arn:aws:lambda:eu-west-1:233135199200:function:email',
                 u'MemorySize': 128,
                 u'Handler': u'lambda_function.lambda_handler',
                 u'Role': u'arn:aws:iam::233135199200:role/lambda_basic_execution',
                 u'Mode': u'event',
                 u'Timeout': 183,
                 u'Runtime': u'python2.7',
                 u'FunctionName': u'email'}],
               u'NextMarker': None}

Next line creates a problem for me:

Get next Marker from current results, which will be used to get the next one

nextmarker = functionResults['Functions']['NextMarker']

File "./sns.py", line 51, in get
    nextmarker = functionResults['Functions']['Description']['NextMarker']
TypeError: list indices must be integers, not str

Desired result should be:None

Last word in functionResults output variable

Upvotes: 0

Views: 89

Answers (2)

Henry
Henry

Reputation: 15732

The problem is that functionResults['Functions'] is a list, not a dictionary, so you need to pass integer indices in. But also the 'NextMarker' key looks like it's within functionResults proper.

I think what you want is:

nextmarker = functionResults['NextMarker']

Upvotes: 2

Prune
Prune

Reputation: 77900

Your value is quite clear: functionResults["Functions"] is a large list. You cannot index it with a string. Perhaps functionResults["Functions"][0], or remove the list container from the value.

{u'Functions': [      # <=== here's the problem !
    {u'Description': u'',
     u'LastModified': u'2018-08-01T18:50:04.214+0000',
     u'ConfigurationId': u'e97b805a-c947-4c56-9a2e-5bef3c4cc6c5', 
     ...}],
 u'NextMarker': None}

However, note that NextMarker is not within the Functions entry; it's an entry in its own right. This should do what you expect.

nextmarker = functionResults['NextMarker']

Upvotes: 1

Related Questions