-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yml
More file actions
133 lines (124 loc) · 3.96 KB
/
Copy pathtemplate.yml
File metadata and controls
133 lines (124 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Resources:
OriginalBooksTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
TableName: "dynamodb-migration-example-OriginalBooksTable"
AttributeDefinitions:
- AttributeName: isbn
AttributeType: S
KeySchema:
- AttributeName: isbn
KeyType: HASH
BillingMode: PAY_PER_REQUEST
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
TargetBooksTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
TableName: "dynamodb-migration-example-TargetBooksTable"
AttributeDefinitions:
- AttributeName: author
AttributeType: S
- AttributeName: title
AttributeType: S
KeySchema:
- AttributeName: author
KeyType: HASH
- AttributeName: title
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
MigrateLambda:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
Role: !GetAtt MigrateLambdaRole.Arn
Environment:
Variables:
TARGET_TABLE: !Ref TargetBooksTable
Events:
DDBEvent:
Type: DynamoDB
Properties:
Stream: !GetAtt OriginalBooksTable.StreamArn
StartingPosition: TRIM_HORIZON
BatchSize: 10
InlineCode: |
var AWS = require("aws-sdk");
var dynamodb = new AWS.DynamoDB();
module.exports.handler = (event, context, callback) => {
pushItems(event, context, callback);
};
function pushItems (event, context, callback) {
let promises = [];
event.Records.forEach(function(record) {
if (record.eventName === "REMOVE") {
let oldItem = record.dynamodb.OldImage;
let params = {
Key: {
author: {
S: oldItem.author.S
},
title: {
S: oldItem.title.S
}
},
TableName: process.env.TARGET_TABLE
};
promises.push(dynamodb.deleteItem(params).promise());
} else {
// CREATE or UPDATE event -> Upsert item in target table
let newItem = record.dynamodb.NewImage;
// Trim off field that was only there to trigger stream
delete newItem["migrate"];
let params = {
Item: newItem,
TableName: process.env.TARGET_TABLE
};
promises.push(dynamodb.putItem(params).promise());
}
});
Promise.all(promises)
.then(() => callback())
.catch((err) => callback(err));
}
MigrateLambdaRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
Policies:
- PolicyName: ReadDynamoDbStream
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Resource:
- !GetAtt OriginalBooksTable.StreamArn
Action:
- dynamodb:GetRecords
- dynamodb:GetShardIterator
- dynamodb:DescribeStream
- dynamodb:ListShards
- dynamodb:ListStreams
- Effect: Allow
Resource:
- !GetAtt TargetBooksTable.Arn
Action:
- dynamodb:PutItem
- dynamodb:DeleteItem