-
Notifications
You must be signed in to change notification settings - Fork 21
/
tail-dynamodb.js
169 lines (138 loc) · 4.15 KB
/
tail-dynamodb.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const _ = require("lodash");
const { getAWSSDK } = require("../lib/aws");
const { Command, flags } = require("@oclif/command");
const { checkVersion } = require("../lib/version-check");
const { track } = require("../lib/analytics");
require("colors");
class TailDynamodbCommand extends Command {
async run() {
const { flags } = this.parse(TailDynamodbCommand);
const { tableName, region, profile, endpoint } = flags;
global.region = region;
global.profile = profile;
global.endpoint = endpoint;
checkVersion();
track("tail-dynamodb", { region, hasEndpoint: !_.isEmpty(endpoint) });
this.log(`checking DynamoDB table [${tableName}] in [${region}]`);
const streamArn = await this.getStreamArn(tableName);
if (!streamArn) {
this.log("table doesn't have a stream, exiting...");
this.exit();
}
this.log(`stream arn is: ${streamArn}`);
this.log(`checking DynamoDB stream [${streamArn}] in [${region}]`);
const stream = await this.describeStream(streamArn);
this.log(
`polling DynamoDB stream for table [${tableName}] (${stream.shardIds.length} shards)...`
);
this.log("press <any key> to stop");
await this.pollDynamoDBStreams(streamArn, stream.shardIds);
this.exit(0);
}
getDynamoDBClient() {
const AWS = getAWSSDK();
if (global.endpoint) {
return new AWS.DynamoDB({ endpoint: global.endpoint });
} else {
return new AWS.DynamoDB();
}
}
getDynamoDBStreamsClient() {
const AWS = getAWSSDK();
if (global.endpoint) {
return new AWS.DynamoDBStreams({ endpoint: global.endpoint });
} else {
return new AWS.DynamoDBStreams();
}
}
async getStreamArn(tableName) {
const DynamoDB = this.getDynamoDBClient();
const resp = await DynamoDB.describeTable({
TableName: tableName
}).promise();
return resp.Table.LatestStreamArn;
}
async describeStream(streamArn) {
const DynamoDBStreams = this.getDynamoDBStreamsClient();
const resp = await DynamoDBStreams.describeStream({
StreamArn: streamArn
}).promise();
return {
arn: resp.StreamDescription.StreamArn,
status: resp.StreamDescription.StreamStatus,
viewType: resp.StreamDescription.StreamViewType,
shardIds: resp.StreamDescription.Shards.map(x => x.ShardId)
};
}
async pollDynamoDBStreams(streamArn, shardIds) {
const DynamoDBStreams = this.getDynamoDBStreamsClient();
let polling = true;
const readline = require("readline");
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
const stdin = process.openStdin();
stdin.once("keypress", () => {
polling = false;
this.log("stopping...");
});
const promises = shardIds.map(async shardId => {
const iteratorResp = await DynamoDBStreams.getShardIterator({
ShardId: shardId,
StreamArn: streamArn,
ShardIteratorType: "LATEST"
}).promise();
let shardIterator = iteratorResp.ShardIterator;
// eslint-disable-next-line no-constant-condition
while (polling) {
let resp;
if (!shardIterator) {
break;
}
try {
resp = await DynamoDBStreams.getRecords({
ShardIterator: shardIterator,
Limit: 10
}).promise();
} catch (e) {
this.error(
`Error while getting records for shard (${shardIterator.yellow}): ${e.message.red}`
);
break;
}
if (resp && !_.isEmpty(resp.Records)) {
resp.Records.forEach(record => {
const timestamp = new Date().toJSON().grey.bold.bgWhite;
this.log(timestamp, "\n", JSON.stringify(record, undefined, 2));
});
}
shardIterator = resp.NextShardIterator;
}
});
await Promise.all(promises);
this.log("stopped");
}
}
TailDynamodbCommand.description = "Tails the records going into a DynamoDB stream";
TailDynamodbCommand.flags = {
tableName: flags.string({
char: "n",
description: "name of the DynamoDB table, e.g. users-dev",
required: true
}),
region: flags.string({
char: "r",
description: "AWS region, e.g. us-east-1",
required: true
}),
profile: flags.string({
char: "p",
description: "AWS CLI profile name",
required: false
}),
endpoint: flags.string({
char: "e",
description: "DynamoDB endpoint (for when using dynamodb-local)",
required: false
})
};
module.exports = TailDynamodbCommand;