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

时间:2018-08-30 02:09:02

标签: aws-lambda amazon-dynamodb serverless

我是AWS的新手。据我所知,要在开始时创建表,我们需要在Yaml文件中1:42 proccess:1 2:56 proccess:2 4:15 proccess:3 4:32 proccess:4 14:15 proccess:5 24:32 proccess:6 42:56 proccess:7 下的migrate: true。我已经编写了如下的serverless.yml文件。

custom.dynamodb

但是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} 不会从资源部分创建表。 请提出上述配置有什么问题。

2 个答案:

答案 0 :(得分:0)

这是因为您的所有键,自定义,插件,资源都嵌套在提供程序下。所有这些都应该在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}

答案 1 :(得分:0)

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

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

看看https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html,特别是“具有DependsOn属性的DynamoDB表”部分