0

我正在使用无服务器框架来编写 AWS lambda 函数。我需要从 HTML 页面获取表单数据并使用 AWS lambda 将其保存到 Dynamodb。所以我也在 nodejs 和 API 端点中编写了代码。最后,我将应用程序部署到 AWS。因此,当我尝试同时使用 CURL 和 Postman 发布数据时,它会显示“内部服务器错误”

以下是相关的代码片段。

handler.js

const params = {
    TableName: process.env.DYNAMODB_TABLE,
    Item: {
      id: uuid.v1(),
      name: data.name,
      phone: data.phone,
      checked: false,
      createdAt: timestamp,
      updatedAt: timestamp,
    },
  };

无服务器.yml

provider:
  name: aws
  runtime: nodejs6.10
  environment:
    DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
  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}"

我不确定在哪里定义 Dynamo 表名以及它是否是在自动运行代码时创建的?我关注了这个 github repo - https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb

4

1 回答 1

0

您当前serverless.yml没有为您定义和创建 DynamoDB 表。

resources您可以通过在配置部分中定义它来做到这一点serverless

provider:
  name: aws
  runtime: nodejs6.10
  environment:
    DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}-phones
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: arn:aws:dynamodb:*:*:*


resources:
  Resources:
    phonesTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:service}-${opt:stage, self:provider.stage}-phones
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

参考: https ://serverless.com/framework/docs/providers/aws/guide/resources/

于 2017-12-12T11:40:29.537 回答