我正在创建无服务器框架项目。
DynamoDB表由其他CloudFormation Stack创建。
如何在serverless.yml
中引用现有的dynamodb表格的StreamArn
我的配置如下
resources:
Resources:
MyDbTable: //'arn:aws:dynamodb:us-east-2:xxxx:table/MyTable'
provider:
name: aws
...
onDBUpdate:
handler: handler.onDBUpdate
events:
- stream:
type: dynamodb
arn:
Fn::GetAtt:
- MyDbTable
- StreamArn
答案 0 :(得分:1)
编辑:
-如果您的表格是在另一个无服务器服务中创建的,则可以跳过步骤1、4和8。
-如果您的表是在标准的 CloudFormation堆栈中创建的,请编辑该堆栈以添加步骤2的输出,并跳过步骤1、4和8
遇到同样的问题,我想出了以下解决方法:
创建一个新的无服务器服务,其中仅包含表(您希望复制现有表设置):
service: MyResourcesStack
resources:
Resources:
FoosTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${opt:stage}-${self:service}-foos
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES # This enables the table's stream
(可选)您可以使用serverless-dynamodb-autoscaling从serverless.yml
配置自动缩放:
plugins:
- serverless-dynamodb-autoscaling
custom:
capacities:
- table: FoosTable # DynamoDB Resource
read:
minimum: 5 # Minimum read capacity
maximum: 50 # Maximum read capacity
usage: 0.75 # Targeted usage percentage
write:
minimum: 5 # Minimum write capacity
maximum: 50 # Maximum write capacity
usage: 0.75 # Targeted usage percentage
设置堆栈以输出表名称Arn和StreamArn:
Outputs:
FoosTableName:
Value:
Ref: FoosTable
FoosTableArn:
Value: {"Fn::GetAtt": ["FoosTable", "Arn"]}
FoosTableStreamArn:
Value: {"Fn::GetAtt": ["FoosTable", "StreamArn"]}
部署堆栈
将数据从旧表复制到新创建的表。
为此,我使用了this script,如果旧表和新表位于同一区域并且表不是很大,则可以很好地工作。对于较大的表,您可能要使用AWS Data Pipeline。
用先前输出的变量替换对初始服务中表的硬编码引用:
provider:
environment:
stage: ${opt:stage}
region: ${self:provider.region}
dynamoDBTablesStack: "MyResourcesStack-${opt:stage}" # Your resources stack's name and the current stage
foosTable: "${cf:${self:provider.environment.dynamoDBTablesStack}.FoosTableName}"
foosTableArn: "${cf:${self:provider.environment.dynamoDBTablesStack}.FoosTableArn}"
foosTableStreamArn: "${cf:${self:provider.environment.dynamoDBTablesStack}.FoosTableStreamArn}"
functions:
myFunction:
handler: myFunction.handler
events:
- stream:
batchSize: 100
type: dynamodb
arn: ${self:provider.environment.foosStreamArn}
部署这些更改