Skip to content

Commit

Permalink
Merge pull request #12 from mhlabs/local-replays
Browse files Browse the repository at this point in the history
Local replays
  • Loading branch information
ljacobsson authored Nov 11, 2020
2 parents be49772 + f0f5757 commit 5b8bfb1
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 148 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,8 @@ If the rule's logical ID is omitted such as `evb local --rule` the tool will par
*Use cases*:
* You want to test the behaviour of an already deployed rule where you don't know the stack's name or where it doesn't belong to a stack.

## Replaying archived events
Add `--replay` option to command. This will guide you through a wizard to find the archive and set the time range to replay.

## Forward events to sam-local
All `evb local` commands support a `--sam-local` flag. When used, events will be passed on to sam-local for more advanced debugging
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const localPatternListener = require("./src/evb-local/listeners/localPatternList
const arnListener = require("./src/evb-local/listeners/arnListener");
const package = require('./package.json');
const eventTester = require("./src/event-tester");
const replayBuilder = require("./src/replay-builder");
const archiveUtil = require("./src/archive-util");
const inputUtil = require("./src/input-util");
require("@mhlabs/aws-sdk-sso");

program.version(package.version, "-v, --vers", "output the current version");
Expand Down Expand Up @@ -106,7 +107,7 @@ program
.description("Starts a replay of events against a specific destination")
.action(async (cmd) => {
initAuth(cmd);
await replayBuilder.replay(cmd.eventbus, cmd.rulePrefix);
await archiveUtil.replay(cmd.eventbus, cmd.rulePrefix);
});

const ruleDefault = "choose from template";
Expand All @@ -132,9 +133,18 @@ program
)
.option("-p, --profile [profile]", "AWS profile to use")
.option("--region [region]", "The AWS region to use. Falls back on AWS_REGION environment variable if not specified")
.option("--replay", "Presents a UI for selecting archive and time range to replay")
.description("Initiates local consumption of a stack's EventBridge rules")
.action(async (cmd) => {
initAuth(cmd);
let replayConfig;
if(cmd.replay) {
const eventbus = await inputUtil.getEventBusName(new AWS.EventBridge());
replayConfig = await archiveUtil.getReplayConfig(eventbus);
replayConfig.EventBusName = eventbus;
}


if (cmd.stackName) {
await stackListener.init(cmd.stackName, cmd.compact, cmd.samLocal);
return;
Expand All @@ -148,7 +158,8 @@ program
cmd.rule === ruleDefault ? null : cmd.rule,
cmd.templateFile,
cmd.compact,
cmd.samLocal
cmd.samLocal,
replayConfig
);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mhlabs/evb-cli",
"version": "1.1.19",
"version": "1.1.20",
"description": "A package for building EventBridge/CloudWatch Events patterns",
"main": "index.js",
"scripts": {
Expand Down
105 changes: 98 additions & 7 deletions sam/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sam/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
]
},
"dependencies": {
"aws-sdk": "^2.790.0",
"node-cache": "^5.1.0",
"uuid": "^8.0.0",
"ws": "^7.2.5"
Expand Down
20 changes: 19 additions & 1 deletion sam/src/Registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ const apigateway = new AWS.ApiGatewayManagementApi({
});

const dynamoDb = new AWS.DynamoDB.DocumentClient();
const eventBridge = new AWS.EventBridge();

async function handler(event, context) {
console.log(event);
const body = JSON.parse(event.body);
const token = body.token;
const localRule = body.localRule;
const ruleArn = body.ruleArn;
const replaySettings = body.replaySettings;
console.log("Rulearn", ruleArn);
let ruleNames;
if (localRule) {
Expand Down Expand Up @@ -47,9 +49,25 @@ async function handler(event, context) {
await apigateway
.postToConnection({
ConnectionId: event.requestContext.connectionId,
Data: "Connected!",
Data: JSON.stringify({
Status:
"Connected!" +
(replaySettings
? `\n\nReplay starting. This can take a few minutes. You can follow the progress here: https://${process.env.AWS_REGION}.console.aws.amazon.com/events/home?region=${process.env.AWS_REGION}#/replay/${replaySettings.ReplayName}`
: ""),
Rules: ruleNames,
EvbLocalRegistration: true,
}),
})
.promise();
if (replaySettings) {
replaySettings.Destination.FilterArns = ruleNames.map(
(p) =>
`arn:aws:events:${process.env.AWS_REGION}:${process.env.AccountId}:rule/${replaySettings.EventBusName}/${p}`
);
delete replaySettings.EventBusName;
const resp = await eventBridge.startReplay(replaySettings).promise();
}

return { statusCode: 200 };
}
Expand Down
5 changes: 5 additions & 0 deletions sam/src/builders/ruleArnCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ async function create(event) {
.promise();
const newRuleName = eventBridgeClient.getRuleName(busName);
ruleNames.push(newRuleName);
if (body.replaySettings && ruleResponse.EventPattern) {
const pattern = JSON.parse(ruleResponse.EventPattern);
pattern["replay-name"] = [body.replaySettings.ReplayName];
ruleResponse.EventPattern = JSON.stringify(pattern);
}
await eventBridgeClient.putRule(busName, ruleResponse, newRuleName);
const targets = [];
for (const target of ruleTargets.Targets) {
Expand Down
4 changes: 4 additions & 0 deletions sam/src/builders/stackRuleCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ async function create(event) {
.promise();
const newRuleName = eventBridgeClient.getRuleName(busName);
ruleNames.push(newRuleName);
if (body.replayName) {
ruleResponse.EventPattern["replay-name"] =
body.replaySettings.ReplayName;
}
await eventBridgeClient.putRule(busName, ruleResponse, newRuleName);
const targets = [];
for (const target of ruleTargets.Targets) {
Expand Down
3 changes: 2 additions & 1 deletion sam/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Metadata:
ReadmeUrl: README.md
Labels: ['eventbridge', 'evb-cli', 'debugging']
HomePageUrl: https://github.com/mhlabs/evb-cli#readme
SemanticVersion: 0.0.5
SemanticVersion: 0.0.6
SourceCodeUrl: https://github.com/mhlabs/evb-cli
Resources:
OnConnectFunction:
Expand Down Expand Up @@ -117,6 +117,7 @@ Resources:
- events:ListTargetsByRule
- events:PutRule
- events:PutTargets
- events:StartReplay
Resource:
- "*"
- Version: '2012-10-17'
Expand Down
Loading

0 comments on commit 5b8bfb1

Please sign in to comment.