-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim-lambda-dynamodb-stream-event-source.example.ts
More file actions
146 lines (132 loc) · 4.09 KB
/
Copy pathsim-lambda-dynamodb-stream-event-source.example.ts
File metadata and controls
146 lines (132 loc) · 4.09 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
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Delivering a simulated table's changes to a simulated function.
*/
import {
CreateTableCommand,
GetItemCommand,
PutItemCommand,
} from "@aws-sdk/client-dynamodb";
import { CreateRoleCommand, PutRolePolicyCommand } from "@aws-sdk/client-iam";
import {
CreateEventSourceMappingCommand,
CreateFunctionCommand,
} from "@aws-sdk/client-lambda";
import { SimAws } from "@kensio/yulin";
import {
makeLambdaZipFileInput,
type SimLambdaDynamoDbStreamEvent,
} from "@kensio/yulin/lambda";
const simAws = new SimAws();
const { TableDescription } = await simAws.dynamoDb().createTable(
new CreateTableCommand({
TableName: "orders",
KeySchema: [{ AttributeName: "orderId", KeyType: "HASH" }],
AttributeDefinitions: [{ AttributeName: "orderId", AttributeType: "S" }],
BillingMode: "PAY_PER_REQUEST",
StreamSpecification: {
StreamEnabled: true,
StreamViewType: "NEW_AND_OLD_IMAGES",
},
}),
);
const streamArn = TableDescription?.LatestStreamArn;
// The projection goes into a second table. A function writing back into the
// table whose stream invoked it would be delivered its own writes, which the
// simulator refuses rather than looping on.
await simAws.dynamoDb().createTable(
new CreateTableCommand({
TableName: "order-totals",
KeySchema: [{ AttributeName: "orderId", KeyType: "HASH" }],
AttributeDefinitions: [{ AttributeName: "orderId", AttributeType: "S" }],
BillingMode: "PAY_PER_REQUEST",
}),
);
// The execution role needs the three stream actions Lambda reads a stream
// with, plus ListStreams, which is on every stream rather than on one, and
// whatever the function itself does.
const role = await simAws.iam().createRole(
new CreateRoleCommand({
RoleName: "OrderProjectorRole",
AssumeRolePolicyDocument: JSON.stringify({
Version: "2012-10-17",
Statement: {
Effect: "Allow",
Principal: { Service: "lambda.amazonaws.com" },
Action: "sts:AssumeRole",
},
}),
}),
);
await simAws.iam().putRolePolicy(
new PutRolePolicyCommand({
RoleName: "OrderProjectorRole",
PolicyName: "ProjectOrders",
PolicyDocument: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: [
"dynamodb:DescribeStream",
"dynamodb:GetRecords",
"dynamodb:GetShardIterator",
],
Resource: streamArn,
},
{ Effect: "Allow", Action: "dynamodb:ListStreams", Resource: "*" },
{
Effect: "Allow",
Action: "dynamodb:PutItem",
Resource: `arn:aws:dynamodb:${simAws.defaultRegionName}:${simAws.defaultAccountId}:table/order-totals`,
},
],
}),
}),
);
await simAws.lambda().createFunction(
new CreateFunctionCommand({
FunctionName: "order-projector",
Role: role.Role.Arn,
Code: {
ZipFile: makeLambdaZipFileInput(
async (event: SimLambdaDynamoDbStreamEvent) => {
await Promise.all(
event.Records.map(async (record) =>
simAws.dynamoDb().putItem(
new PutItemCommand({
TableName: "order-totals",
Item: {
orderId: { S: record.dynamodb.Keys?.["orderId"]?.S ?? "" },
total: { N: record.dynamodb.NewImage?.["total"]?.N ?? "0" },
},
}),
),
),
);
},
),
},
}),
);
await simAws.lambda().createEventSourceMapping(
new CreateEventSourceMappingCommand({
EventSourceArn: streamArn,
FunctionName: "order-projector",
StartingPosition: "TRIM_HORIZON",
}),
);
await simAws.dynamoDb().putItem(
new PutItemCommand({
TableName: "orders",
Item: { orderId: { S: "order-1" }, total: { N: "42" } },
}),
);
// Delivery happens in the background, so wait for the simulation to settle.
await simAws.backgroundTasksComplete();
const projected = await simAws.dynamoDb().getItem(
new GetItemCommand({
TableName: "order-totals",
Key: { orderId: { S: "order-1" } },
}),
);
console.log(projected.Item?.["total"]?.N); // "42"