首页 > 解决方案 > AWS Lambda Python 处理程序 - dynamodb:NoVersionFound

问题描述

我尝试使用我的 Python 处理程序 (processQuerySearch) 在 DynamoDB 中插入一个简单的数据:

START RequestId: 8b913dd4-b13c-407a-a1e3-bddeacc1b066 Version: $LATEST
dynamodb: NoVersionFound
Traceback (most recent call last):
  File "/var/task/searchHandler.py", line 9, in processQuerySearch
    dynamodb = boto3.resource('dynamodb', region_name = 'eu-west-1')
  File "/opt/python/boto3/__init__.py", line 87, in resource
    return _get_default_session().resource(*args, **kwargs)
  File "/opt/python/boto3/session.py", line 278, in resource
    version = self._find_latest_version(service_name)
  File "/opt/python/boto3/session.py", line 110, in _find_latest_version
    raise NoVersionFound(service_name)
boto3.exceptions.NoVersionFound: dynamodb

这是我的处理程序:

import json
import os
import boto3
import uuid

def processSearchLogger (event, context):

    generated_uuid = 'gg3352-6546-6546d'
    dynamodb = boto3.resource('dynamodb', region_name = 'eu-west-1')

    table = dynamodb.Table('DataSearchLogger')
    
    response = table.put_item(
        Item = {
            'uuid': generated_uuid,
            'search_term': 'Kitchen book',
        }
    )
    
    return response

和 serverless.yml (这似乎对这个错误没有影响,因为我改变了很多东西,它仍然是同样的错误):

service: processors

frameworkVersion: '2'

provider:
  name: aws
  runtime: python3.6
  region: eu-west-1
  profile: default
  environment:
    DYNAMODB_TABLE: DataSearchLogger
  iamRoleStatements:
    - Effect: Allow
      Action:
        - sqs:SendMessage
        - sqs:GetQueueUrl
        - sqs:ListQueues
      Resource: 
        Fn::GetAtt: [SQSQueue, Arn]
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable 
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: 
        Fn::GetAtt: [DataSearchLoggerDynamoDbTable, Arn]

#"arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
# AWS Lambda Layers
layers:
  Boto3:
    path: layers/boto3 # required, path to layer contents on disk
    name: ${self:service}-Python36-Boto3-layer # optional, Deployed Lambda layer name
    description: Boto3 Layer # optional, Description to publish to AWS
    compatibleRuntimes: # optional, a list of runtimes this layer is compatible with
      - python3.6
    licenseInfo: MIT License # optional, a string specifying license information.
    allowedAccounts: # optional, a list of AWS account IDs allowed to access this layer.
      - "*"

package:
  individually: true

# AWS Lambda Functions
functions:
  ProcessSearchLogger:
    handler: searchHandler.processSearchLogger
    layers:
      - { Ref: Boto3LambdaLayer }
    environment:
        SQS_URL:
            Ref: SQSQueue
    events:
        - sqs:
            arn:
              Fn::GetAtt: [SQSQueue, Arn]
# Note the reference name is always the CamelCase version of the layer name with LambdaLayer suffix.

# Custom configuration of all resources including Lambda Function, IAM Role, S3 Bucket, ...
resources:
  Resources:
    ProcessSearchLoggerLambdaFunction: # name is always the CamelCase version of the layer with LambdaLayer suffix.
      Type: AWS::Lambda::Function
      Properties:
        MemorySize: 128
        Timeout: 5
    SQSQueue:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: "processor-data-search-logger-queue"
    DataSearchLoggerDynamoDbTable:
      Type: AWS::DynamoDB::Table
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: uuid
            AttributeType: S
          - AttributeName: search_term
            AttributeType: S
        KeySchema:
          - AttributeName: uuid
            KeyType: HASH
          - AttributeName: search_term
            KeyType: RANGE
        ProvisionedThroughput:
            ReadCapacityUnits: 1
            WriteCapacityUnits: 1
        TableName: ${self:provider.environment.DYNAMODB_TABLE}

有关信息,我从 SQS 句柄开始,它运行良好,因此我的凭证正确链接到 AWS 服务。

更新 :

似乎问题来自 Lambda Role,一个权限问题。但是,我已经花了几个小时寻找这些权限,但我看不出这会如何引发问题。这是相关的角色政策:

{
    "RoleName": "processors-dev-eu-west-1-lambdaRole",
    "PolicyName": "processors-dev-lambda",
    "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Action": [
                    "logs:CreateLogStream",
                    "logs:CreateLogGroup"
                ],
                "Resource": [
                    "arn:aws:logs:eu-west-1:*:log-group:/aws/lambda/processors-dev*:*"
                ],
                "Effect": "Allow"
            },
            {
                "Action": [
                    "logs:PutLogEvents"
                ],
                "Resource": [
                    "arn:aws:logs:eu-west-1:*:log-group:/aws/lambda/processors-dev*:*:*"
                ],
                "Effect": "Allow"
            },
            {
                "Action": [
                    "sqs:SendMessage",
                    "sqs:GetQueueUrl",
                    "sqs:ListQueues"
                ],
                "Resource": "arn:aws:sqs:eu-west-1:*:processor-data-search-logger-queue",
                "Effect": "Allow"
            },
            {
                "Action": [
                    "dynamodb:Query",
                    "dynamodb:Scan",
                    "dynamodb:GetItem",
                    "dynamodb:PutItem",
                    "dynamodb:UpdateItem",
                    "dynamodb:DeleteItem"
                ],
                "Resource": "arn:aws:dynamodb:eu-west-1:*:table/DataSearchLogger",
                "Effect": "Allow"
            },
            {
                "Action": [
                    "sqs:ReceiveMessage",
                    "sqs:DeleteMessage",
                    "sqs:GetQueueAttributes"
                ],
                "Resource": [
                    "arn:aws:sqs:eu-west-1:*:processor-data-search-logger-queue"
                ],
                "Effect": "Allow"
            }
        ]
    }
}

标签: pythonaws-lambdaamazon-dynamodb

解决方案


推荐阅读