无服务器开始不从 yaml 文件创建本地 DynamoDB 数据库

Serverless start not creating local DynamoDB database from yaml file

我是 AWS 新手。据我所知,要在开始时创建表,我们需要在 yaml 文件的 custom.dynamodbmigrate: true。我写了serverless.yml文件如下。

provider:
  name: aws
  runtime: nodejs8.10
  profile: default
  region: us-east-1
  memorySize: 512
  target: 'node' # Below defined environment variables wont be accessible in lambda functions.
  stage: ${opt:stage, 'dev'}
  environment:
    USERS_TABLE: Users_${self:provider.stage}
    DAILYACTIVITY_TABLE: DailyActivity_${self:provider.stage}
  plugins:
    - serverless-dynamodb-local
    - serverless-offline
  custom:
    dynamodb:
      start:
        migrate: true
  resources:
    Resources:
      usersTable:
        Type: 'AWS::DynamoDB::Table'
        DeletionPolicy: Retain
        Properties:
          AttributeDefinitions:
            - AttributeName: emailid
              AttributeType: S
            - AttributeName: id
              AttributeType: S
          KeySchema:
            - AttributeName: emailid
              KeyType: HASH
          GlobalSecondaryIndexes:
            - IndexName: gsi_id
              KeySchema:
                - AttributeName: id
                  KeyType: HASH
              ProvisionedThroughput:
                ReadCapacityUnits: 5
                WriteCapacityUnits: 5
              Projection:
                ProjectionType: ALL
          ProvisionedThroughput:
            ReadCapacityUnits: 5
            WriteCapacityUnits: 5
          TableName: ${self:provider.environment.USERS_TABLE}
      activityTable:
        Type: 'AWS::DynamoDB::Table'
        DeletionPolicy: Retain
        Properties:
          AttributeDefinitions:
            - AttributeName: id
              AttributeType: S
            - AttributeName: date
              AttributeType: S
          KeySchema:
            - AttributeName: id
              KeyType: HASH
            - AttributeName: date
              KeyType: RANGE
          ProvisionedThroughput:
            ReadCapacityUnits: 5
            WriteCapacityUnits: 5
          TableName: ${self:provider.environment.DAILYACTIVITY_TABLE}

但是 sls offline start 不会从资源部分创建表。 请指出上面的配置有什么问题。

这是因为您所有的密钥、自定义、插件、资源都嵌套在提供程序下。所有这些都应该在 yaml 的顶层(缩进级别 0)。我 运行 遇到了同样的问题,这就是它不起作用的原因。另外,请使用 serverless-local-dynamodb 版本 0.2.30。最新的 (0.2.36) 有问题。

我已经为您重新格式化了 yaml,以便您明白我的意思

provider:
  name: aws
  runtime: nodejs8.10
  profile: default
  region: us-east-1
  memorySize: 512
  target: 'node' # Below defined environment variables wont be accessible in lambda functions.
  stage: ${opt:stage, 'dev'}
  environment:
    USERS_TABLE: Users_${self:provider.stage}
    DAILYACTIVITY_TABLE: DailyActivity_${self:provider.stage}

plugins:
  - serverless-dynamodb-local
  - serverless-offline

custom:
  dynamodb:
    start:
      migrate: true

resources:
  Resources:
    usersTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: emailid
            AttributeType: S
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: emailid
            KeyType: HASH
        GlobalSecondaryIndexes:
          - IndexName: gsi_id
            KeySchema:
              - AttributeName: id
                KeyType: HASH
            ProvisionedThroughput:
              ReadCapacityUnits: 5
              WriteCapacityUnits: 5
            Projection:
              ProjectionType: ALL
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5
        TableName: ${self:provider.environment.USERS_TABLE}
    activityTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: date
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
          - AttributeName: date
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5
        TableName: ${self:provider.environment.DAILYACTIVITY_TABLE}

Dynamodb 无法同时创建两个表,因此您需要将一个表声明为依赖于另一个表,以便它们是按顺序创建的,而不是并行创建的。

"mySecondDDBTable" : {
  "Type" : "AWS::DynamoDB::Table",
  "DependsOn" : "myFirstDDBTable" ,
  "Properties" : {
   ... etc

看看 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html 具体 "DynamoDB Table with a DependsOn Attribute"

部分