Jag*_*row 5 amazon-dynamodb serverless-framework amazon-dynamodb-streams
我正在创建无服务器框架项目。
DynamoDB 表是由其他 CloudFormation Stack 创建的。
我如何在中引用现有 dynamodb 表的 StreamArnserverless.yml
我的配置如下
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
Run Code Online (Sandbox Code Playgroud)
编辑:
- 如果您的表是在另一个无服务器服务中创建的,您可以跳过步骤 1、4 和 8。
- 如果您的表是在标准CloudFormation Stack中创建的,请编辑此堆栈以添加步骤 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
Run Code Online (Sandbox Code Playgroud)
(可选)您可以使用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
Run Code Online (Sandbox Code Playgroud)设置堆栈以输出表名称、Arn 和 StreamArn:
Outputs:
FoosTableName:
Value:
Ref: FoosTable
FoosTableArn:
Value: {"Fn::GetAtt": ["FoosTable", "Arn"]}
FoosTableStreamArn:
Value: {"Fn::GetAtt": ["FoosTable", "StreamArn"]}
Run Code Online (Sandbox Code Playgroud)部署堆栈
将数据从旧表复制到新创建的表中。
为此,我使用了这个脚本,如果旧表和新表位于同一区域并且表不是很大,该脚本可以很好地工作。对于较大的表,您可能需要使用 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}
Run Code Online (Sandbox Code Playgroud)部署这些更改
| 归档时间: |
|
| 查看次数: |
3222 次 |
| 最近记录: |