【问题标题】:Lambda - Python - "errorMessage": "list indices must be integers, not str"Lambda - Python - “errorMessage”:“列表索引必须是整数,而不是 str”
【发布时间】:2020-09-06 02:05:57
【问题描述】:

试图计算出此broken repo 的功能版本并收到有关数据类型的错误。它正在检测应该是字符串的整数。您可以在 github 存储库中看到其他模块,但基本上这应该会根据 cloudwatch 事件向 Slack 触发一些消息。

堆栈跟踪如下。


from __future__ import print_function
import json
import boto3
import time

# from test_events import TEST_EVENTS, TEST_ERROR_EVENTS
from build_info import BuildInfo, CodeBuildInfo
from slack_helper import post_build_msg, find_message_for_build
from message_builder import MessageBuilder

import re
import sys

client = boto3.client('codepipeline')

def findRevisionInfo(info):
  r = client.get_pipeline_execution(
    pipelineName=info.pipeline,
    pipelineExecutionId=info.executionId
  )['pipelineExecution']

  revs = r.get('artifactRevisions',[])
  if len(revs) > 0:
      return revs[0]
  return None


def pipelineFromBuild(codeBuildInfo):
  r = client.get_pipeline_state(name=codeBuildInfo.pipeline)

  for s in r['stageStates']:
    for a in s['actionStates']:
      executionId = a.get('latestExecution', {}).get('externalExecutionId')
      if executionId and codeBuildInfo.buildId.endswith(executionId):
        pe = s['latestExecution']['pipelineExecutionId']
        return (s['stageName'], pe, a)

  return (None, None, None)


def processCodePipeline(event):
  buildInfo = BuildInfo.fromEvent(event)
  existing_msg = find_message_for_build(buildInfo)
  builder = MessageBuilder(buildInfo, existing_msg)
  builder.updatePipelineEvent(event)

  if builder.needsRevisionInfo():
    revision = findRevisionInfo(buildInfo)
    builder.attachRevisionInfo(revision)

  post_build_msg(builder)

def processCodeBuild(event):
  cbi = CodeBuildInfo.fromEvent(event)
  (stage, pid, actionStates) = pipelineFromBuild(cbi)

  if not pid:
    return

  buildInfo = BuildInfo(pid, cbi.pipeline)

  existing_msg = find_message_for_build(buildInfo)
  builder = MessageBuilder(buildInfo, existing_msg)

  if 'phases' in event['detail']['additional-information']:
    phases = event['detail']['additional-information']['phases']
    builder.updateBuildStageInfo(stage, phases, actionStates)

  logs = event['detail'].get('additional-information', {}).get('logs')
  if logs:
    builder.attachLogs(event['detail']['additional-information']['logs'])

  post_build_msg(builder)


def process(event):
  if event['source'] == "aws.codepipeline":
    processCodePipeline(event)
  if event['source'] == "aws.codebuild":
    processCodeBuild(event)

def run(event, context):
  #print(json.dumps(event, indent=2, default=str))
  m = process(event)

if __name__ == "__main__":
  with open ('test-event.json') as f:
      events = json.load(f)
      for e in events:
        run(e, {})
        time.sleep(1)

返回错误:

File "/var/task/notifier.py", line 86, in run
    m = process(event)
  File "/var/task/notifier.py", line 79, in process
    if event['source'] == "aws.codepipeline":
TypeError: list indices must be integers, not str

正在摄取的数据如下所示:

{
    "account": "164943972409",
    "region": "us-west-2",
    "detail": {
      "execution-id": "c776b515-1810-465f-a0ab-3a30a1d4341b",
      "pipeline": "buildfish-web-ng-code-pipeline-dev",
      "version": 1,
      "state": "STARTED"
    },
    "detail-type": "CodePipeline Pipeline Execution State Change",
    "source": "aws.codepipeline",
    "version": "0",
    "time": "2018-05-20T04:11:41Z",
    "id": "ae75c080-2f81-dd60-e6cc-76ec00489305",
    "resources": [
      "arn:aws:codepipeline:us-west-2:164943972409:buildfish-web-ng-code-pipeline-dev"
    ]
  }

谢谢

【问题讨论】:

  • 检查 json.load() 返回的内容,它是一个列表而不是字典
  • 您是否尝试过验证 e 具有您期望的值?

标签: python python-2.7 aws-lambda


【解决方案1】:

在您的主函数中,您打开并加载 json 事件。

当循环这个字典时,即for e in events:,你循环的是字典中的键值,而不是一次加载的整个事件。

只需将从json.load(f) 读入的整个事件传递给您的run(event, context) 函数。

if __name__ == "__main__":
  with open ('test-event.json') as f:
      event = json.load(f)
      run(event, {})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2016-02-09
    • 2015-05-03
    相关资源
    最近更新 更多