2

我在一个无服务器项目中工作,并且在使用 dynamodb 在本地运行我的应用程序时遇到问题。它没有创建数据表,因此无法正确执行种子。我的配置有什么问题?

启动命令:$ sls offline start --stage dev

错误信息:

Serverless: Bundling with Webpack...
Serverless: Watching for changes...
Dynamodb Local Started, Visit: http://localhost:8000/shell

  Resource Not Found Exception ---------------------------

  ResourceNotFoundException: Cannot do operations on a non-existent table
      at Request.extractError (/home/mauricio/dev/project/covid-favor-api/node_modules/aws-sdk/lib/protocol/json.js:51:27)
      at Request.callListeners (/home/mauricio/dev/project/covid-favor-api/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
      at Request.emit (/home/mauricio/dev/project/covid-favor-api/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
      at Request.emit (/home/mauricio/dev/project/covid-favor-api/node_modules/aws-sdk/lib/request.js:683:14)
      at endReadableNT (_stream_readable.js:1183:12)
      at processTicksAndRejections (internal/process/task_queues.js:80:21)

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          linux
     Node Version:              12.13.0
     Framework Version:         1.64.0
     Plugin Version:            3.4.0
     SDK Version:               2.3.0
     Components Core Version:   1.1.2
     Components CLI Version:    1.4.0

无服务器文件:

org: mauriciocoder
app: covid-favor
# We are using JEST for testing: https://jestjs.io/docs/en/getting-started.html - npm test
service: covid-favor-app-api

# Create an optimized package for our functions
package:
  individually: true

# Create our resources with separate CloudFormation templates
resources:
  # API Gateway Handler
  - ${file(resources/api-gateway-handler.yml)}
  # DynamoDb Handler
  - ${file(resources/dynamodb-handler.yml)}

plugins:
  - serverless-bundle # Package our functions with Webpack
  - serverless-dynamodb-local
  - serverless-offline
  - serverless-dotenv-plugin # Load .env as environment variables

custom:
  authorizer:
    dev: 
    prod: aws_iam
  dynamodb:
    stages: dev
    start:
      port: 8000 # always se port 8000, otherwise serverless-dynamodb-client will not find
      migrate: true # creates tables from serverless config
      seed: true # determines which data to onload
    seed:
      domain:
        sources:
          - table: userAccount
            sources: [./resources/migrations/v0.json]

provider:
  name: aws
  runtime: nodejs10.x
  stage: ${opt:stage, 'dev'}
  region: us-east-1

  # These environment variables are made available to our functions
  # under process.env.
  environment:
    helpTableName: help
    userAccountTableName: userAccount

  # 'iamRoleStatements' defines the permission policy for the Lambda function.
  # In this case Lambda functions are granted with permissions to access DynamoDB.
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:us-east-1:*:*"

  # These are the usage plan for throttling
  usagePlan:
    throttle:
        burstLimit: 2
        rateLimit: 1

functions: ...

dynamodb 处理程序文件:

userAccount:
  Type: AWS::DynamoDB::Table
  DeletionPolicy : Retain
  Properties:
    TableName: userAccount
    AttributeDefinitions:
      - AttributeName: userId
        AttributeType: S
    KeySchema:
      - AttributeName: userId
        KeyType: HASH
    ProvisionedThroughput:
      ReadCapacityUnits: 1
      WriteCapacityUnits: 1

种子文件 v0.json:

{
    "Table": {
        "TableName": "userAccount",
        "KeySchema": [
            {
                "AttributeName": "userId",
                "KeyType": "S"
            }
        ],
        "LocalSecondaryIndexes": [
            {
                "IndexName": "local_index_1",
                "KeySchema": [
                    {
                        "AttributeName": "userId",
                        "KeyType": "HASH"
                    }
                ]
            }
        ],
        "ProvisionedThroughput": {
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        }
    }
}

包.json

{
  "name": "notes-app-api",
  "version": "1.1.0",
  "description": "A Node.js starter for the Serverless Framework with async/await and unit test support",
  "main": "handler.js",
  "scripts": {
    "test": "serverless-bundle test"
  },
  "author": "",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/AnomalyInnovations/serverless-nodejs-starter.git"
  },
  "devDependencies": {
    "aws-sdk": "^2.622.0",
    "jest": "^25.1.0",
    "serverless-bundle": "^1.2.5",
    "serverless-dotenv-plugin": "^2.1.1",
    "serverless-offline": "^5.3.3"
  },
  "dependencies": {
    "serverless-dynamodb-client": "0.0.2",
    "serverless-dynamodb-local": "^0.2.35",
    "stripe": "^8.20.0",
    "uuid": "^3.4.0"
  }
}

4

2 回答 2

1

我认为您缺少指定迁移文件的迁移块。把它放在你的dynamodb:钥匙下

migration:
  dir: resources/migrations/v0.json
于 2021-07-15T17:00:46.090 回答
0

错误消息正在解释您的问题所在 -dynamodb-handler.yml文件缺少密钥,Resources. 在serverless-dynamodb-local 文档中,您可以看到多余的resources&Resources键。所以你dynamodb-handler.yml应该这样开始:

Resources:
  userAccount:
    Type: AWS::DynamoDB::Table

请注意,所有其他外部资源必须以相同的键开头,即api-gateway-handler.yml在您的示例中。

此外,如果您在离线启动无服务器时难以创建表,或者您正在使用持久性 docker dynamodb,请使用以下命令进行迁移:

npx serverless dynamodb migrate
于 2021-12-24T09:50:07.937 回答