无法使用python将数据插入dynamodb

无法使用python将数据插入dynamodb,python,amazon-web-services,amazon-dynamodb,serverless,Python,Amazon Web Services,Amazon Dynamodb,Serverless,我是无服务器框架的新手,我正在尝试使用python将数据放入dynamodb。但无法插入数据。即使是我也没有得到任何错误 我尝试了.yml文件的所有设置,但没有任何进展 yml先生 .py文件 我不知道为什么插入不起作用,代码看起来是正确的。如果您从put\u item获得响应,您可以获得更多信息。例如: # write the todo to the database put_response = table.put_item(Item=item) # create

我是无服务器框架的新手,我正在尝试使用python将数据放入dynamodb。但无法插入数据。即使是我也没有得到任何错误

我尝试了.yml文件的所有设置,但没有任何进展

yml先生 .py文件
我不知道为什么插入不起作用,代码看起来是正确的。如果您从
put\u item
获得响应,您可以获得更多信息。例如:

    # write the todo to the database
    put_response = table.put_item(Item=item)

    # create a response
    response = {
        "statusCode": 200,
        "body": json.dumps(put_response)
    }

我已经部署了您的项目,只需稍作修改,它就可以工作了。这意味着您的代码基本上正常工作,但由于可能存在诸如iAM角色之类的其他问题,如果您能够提供POST请求的结果,则会更好

我的情况如下:

  • 文件结构-您使用的是
    函数/create.create
    ,因此它可能不同于您的文件,但对于给定的文件,但为了使其正常工作,我需要将其作为一个简单的无服务器项目
  • serverless.yml-添加了“服务”属性,并删除了您的授权人,以便在没有任何授权的情况下调用它
  • handler.py-与.py文件相同

  • 结果

  • 建议

    看起来你的代码没有任何严重的缺陷,但是仍然有很多错误。这两项建议可能会有所帮助:

    1) 捕获任何异常并打印出来 如果您使用下面这样的函数,您可以轻松捕获那里发生的事情和调试提示

    导入系统 导入回溯 导入操作系统 def printException(异常,写入程序=无): 如果writer为None: 作者=印刷品 top=traceback.extract_tb(sys.exc_info()[2])[-1] writer(“,”.join([type(exception)。\uuuuu name\uuuuu,str(sys.exc\u info()[1]),os.path.basename(top[0]),str(top[1]))``` 如果名称=“\uuuuu main\uuuuuuuu”: 尝试: 1/0#只是为了引发一个异常,看看这个函数是如何工作的 例外情况除外,如e: 打印例外(e) 将打印异常,以便您可以在Cloudwatch日志中看到它

    2) 在Cloudwatch中启用访问日志并查看日志-从无服务器框架v1.42.0开始,您可以在框架中启用访问日志。只需设置以下属性:

    ...
    provider:
      ...
      logs:
        restApi:
          level: INFO
    ...
    
    然后,您将很容易找到任何与api端点相关的错误,如身份验证、验证或lambda响应格式错误

        # write the todo to the database
        put_response = table.put_item(Item=item)
    
        # create a response
        response = {
            "statusCode": 200,
            "body": json.dumps(put_response)
        }
    
    .
    ├── serverless.yml
    └── handler.py
    
    service: stack1
    provider:
      name: aws
      runtime: python3.7
    
      environment:
        DYNAMODB_TABLE: employee
      iamRoleStatements:
        - Effect: Allow
          Action:
            - dynamodb:Query
            - dynamodb:Scan
            - dynamodb:GetItem
            - dynamodb:PutItem
            - dynamodb:UpdateItem
            - dynamodb:DeleteItem
          Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
    
      stage: dev
      region: us-east-1
    
    functions:
      create:
        handler: handler.create
        events:
          - http:
            path: employee
            method: post
            cors: true
      get:
        handler: functions/get.get
        events:
          - http:
            path: employee/{id}
            method: get
            cors: true
    
    resources:
      Resources:
        employee:
          Type: 'AWS::DynamoDB::Table'
          DeletionPolicy: Retain
            Properties:
              AttributeDefinitions:
                - AttributeName: employeeId
                  AttributeType: S
              KeySchema:
                - AttributeName: employeeId
                  KeyType: HASH
              ProvisionedThroughput:
                ReadCapacityUnits: 1
                WriteCapacityUnits: 1
              TableName: ${self:provider.environment.DYNAMODB_TABLE}
    
    ~/prj/temp/stack> curl -X POST -H "Content-Type: application/json" -d "{}" https://u85k6*****.execute-api.us-east-1.amazonaws.com/dev/employee
    {"employeeId": "2", "employeeName": "Singhs"}%
    
    ...
    provider:
      ...
      logs:
        restApi:
          level: INFO
    ...