我在使用无服务器框架时遇到了一些问题,因为我不小心在另一个服务上使用了相同的服务名称。
发生错误:tableX - TableX 已存在。
假设我有两个“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
测试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
当我sls部署
“test1”时,他根据我的需要使用DeletionPolicy: Retain
创建表,对于具有非常明智的数据。然后我sls部署
“test2”具有其他功能但没有任何资源(DynamoDB表),他执行了预期的操作:跳过表的删除。
但是,当我再次部署“test1”时,他无法识别这些表,他开始“创建”现有表而不是更新它们,并且部署失败。
我需要未删除的表,并且需要服务上的功能。看起来 Cloud Formation 失去了对第一次部署时创建的表的跟踪。
我不想像 github thread 中所说的那样分离服务(仅用于资源) 。 我需要正在运行的表,它有大量数据,备份和恢复到另一个表的成本太高,很多用户可能会受到影响。
那么,我如何告诉 Cloud Formation Stack 我正在更新该表,而不是尝试创建它?如何跟踪 Cloud Formation Stack 上的服务?而且,我如何防止在没有资源的情况下部署服务?
对于这种情况,最好的解决方案是什么?希望我的问题足够清楚,能够理解。
最佳答案
不存在与test2
相关的问题。
对于test1
,您可以多次sls部署
。
但是,如果您运行 sls remove
,当 dynamodb 在 serverless.yml
中设置为 Retain
时,dynamodb 表不会被删除。所以你不能用slsdeploy
再次创建它,因为同名资源已经存在。这是aws cloudformation中的设计。
您发现跳过资源的新功能已开放。我们必须等待该功能的开发和合并。我也在等待同样的解决方案。去那里投票吧!
就目前的情况,你必须备份dynamodb,销毁它,然后运行sls部署
,如果确实有问题,再恢复它。
我通常使用变量进行管理,例如
DeletionPolicy: ${self:custom.${self:custom.stage}.deletion_policy}
针对不同环境定制:
custom
dev:
deletion_policy: Delete
prod:
deletion_policy: Retain
关于amazon-web-services - 无服务器服务更新使用 DeletionPolicy 保留创建的 Dynamodb 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47536542/