使用DeletionPolicy保留创建的无服务器服务更新Dynamodb表

Ser*_*res 3 amazon-web-services aws-cloudformation amazon-dynamodb serverless-framework serverless

我在使用无服务器框架时遇到一些问题,因为我不小心在另一个服务上使用了相同名称的服务。

An error occurred: tableX - TableX already exists.

假设我有两个“ serverless.yml ”文件,两个文件的服务名称相同。其中一个(我们称其为“ test1 ”)具有资源(DynamoDB表),而另一个则没有(“ test2 ”)。像以下片段一样:

测试1

service: sandbox-core
provider:
  name: aws
  stage: core
  runtime: nodejs6.10
  region: sa-east-1
  memorySize: 128
  timeout: 300

resources:
  Resources:

    table3:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        TableName: SandboxTable3
        AttributeDefinitions:
          -
            AttributeName: provider
            AttributeType: S
          -
            AttributeName: appId
            AttributeType: S
        KeySchema:
          -
            AttributeName: provider
            KeyType: HASH
          -
            AttributeName: appId
            KeyType: RANGE

        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

    table4:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        TableName: SandboxTable4
        AttributeDefinitions:
          -
            AttributeName: session
            AttributeType: S
        KeySchema:
          -
            AttributeName: session
            KeyType: HASH

        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 1

functions:
  auth:
    handler: handler.auth
    events:
      - http:
          path: auth/{session}/{provider}/{appId}
          method: get
          cors: true
Run Code Online (Sandbox Code Playgroud)

测试2

service: sandbox-core

provider:
  name: aws
  stage: core
  runtime: nodejs6.10
  region: sa-east-1
  memorySize: 128
  timeout: 300

functions:
  createCustomData:
    handler: handler.createCustomData
    events:
      - http:
          path: teste2
          method: post
          cors: true
Run Code Online (Sandbox Code Playgroud)

当我sls deploy为“ test1 ”时,他使用DeletionPolicy: Retain,为具有非常敏感数据的表创建了我想要的表。然后,我具有其他功能但没有任何资源的sls deploytest2 ”(DynamoDB表),他做了预期的事情:跳过表的删除。

但是,当我再次sls部署“ test1 ”时,他无法识别这些表,他开始“创建”现有表而不是更新它们,并且无法部署。

我需要未删除的表,并且需要服务上的功能。看起来Cloud Formation在第一次部署中就失去了对创建表的跟踪。

我不希望像在github线程上所说的那样分离服务(仅用于资源)。我需要正在运行的表,它具有大量数据,并且将其备份并还原到另一个表的开销太大,很多用户可能会受到影响。

那么,如何告诉Cloud Formation Stack,我正在更新该表,而不是尝试创建该表?如何跟踪云形成堆栈上的服务?而且,如何防止没有资源的情况下部署带有资源的服务?

这种情况下最好的解决方案是什么?希望我的问题很清楚可以理解。

BMW*_*BMW 6

没有相关的问题test2

对于test1,您可以sls deploy很多次。

但是,如果运行sls remove,则将dynamodb设置为 Retain in时serverless.yml,不会删除dynamodb表。因此,您无法使用再次创建它sls deploy,因为存在具有相同名称的资源。这是aws cloudformation中的设计。

您已经找到了用于新功能的开放票证,以跳过资源。我们必须等待功能被开发和合并。我也在等待相同的解决方案。去那里投票吧!

在当前情况下,sls deploy如果确实很重要,则必须备份dynamodb,销毁它并运行,然后将其还原。

我通常用变量来管理,例如

DeletionPolicy: ${self:custom.${self:custom.stage}.deletion_policy}
Run Code Online (Sandbox Code Playgroud)

在针对不同环境的定制中:

custom
  dev:
    deletion_policy: Delete
  prod:
    deletion_policy: Retain
Run Code Online (Sandbox Code Playgroud)