首页 > 解决方案 > DynamoDB 表未在 serverless.yml 文件中创建

问题描述

cannot do operation on a non-existent table运行sls offline start并尝试访问用户端点后出现错误。serverless.yml 文件如下:

service: 
  name: digital-secret

plugins:
  - serverless-dynamodb-local
  - serverless-offline # must be last in the list

custom:
  userTableName: 'users-table-${self:provider.stage}'
  dynamoDb:
    start:
      migrate: true

provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: us-east-2
  iamRoleStatements:
    - Effect: Allow
      Action:
        - 'dynamodb:Query'
        - 'dynamodb:Scan'
        - 'dynamodb:GetItem'
        - 'dynamodb:PutItem'
        - 'dynamodb:UpdateItem'
        - 'dynamodb:DeleteItem'
      Resource:
        - { "Fn::GetAtt": ["usersTable", "Arn"] }
  environment:
    USERS_TABLE: ${self:custom.userTableName}

functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'
  user:
    handler: index.handler
    events:
      - http: 'GET /users/{proxy+}'
      - http: 'POST /users'

resources:
  Resources:
    usersTable:
      Type: 'AWS::DynamoDB::Table'
      Properties:
        TableName: ${self:custom.userTableName}
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

谁能帮忙指出这里有什么问题?我浏览了文档和在线提供的许多不同示例,但我所看到的与上面没有什么不同。

标签: amazon-dynamodbserverless-framework

解决方案


serverless-dynamodb-local文档说该块的custom结构应该是这样的:

custom:
  dynamodb:
    start:
      migrate: true

你有dynamoDb而不是dynamodb


推荐阅读