Skip to content

Commit

Permalink
feat: replay support for evb-local 💥
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Nov 11, 2020
1 parent 568ce99 commit f0f5757
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 190 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
31 changes: 15 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const arnListener = require("./src/evb-local/listeners/arnListener");
const package = require('./package.json');
const eventTester = require("./src/event-tester");
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 All @@ -20,7 +21,7 @@ program
.alias("p")
.option("-f, --format <json|yaml>", "Select output format", "json")
.option("-p, --profile [profile]", "AWS profile to use")
.option("-t, --template [template]", "Path to template file", "template.yml")
.option("-t, --template [template]", "Path to template file", "template.yaml")
.option("--region [region]", "The AWS region to use. Falls back on AWS_REGION environment variable if not specified")
.description("Starts an EventBridge pattern builder")
.action(async (cmd) => {
Expand Down Expand Up @@ -76,7 +77,7 @@ program
program
.command("extract-sam-event")
.alias("e")
.option("-t, --template [template]", "Path to template file", "template.yml")
.option("-t, --template [template]", "Path to template file", "template.yaml")
.description("Extracts an EventBusRule event from an AWS::Serverless::Function resource to an AWS::Events::Rule for more advanced use cases")
.action(async (cmd) => {
templateParser.load(cmd.template);
Expand Down Expand Up @@ -109,18 +110,6 @@ program
await archiveUtil.replay(cmd.eventbus, cmd.rulePrefix);
});

program
.command("insights")
.alias("in")
.option("-b, --eventbus [eventbus]", "The eventbus the archive is stored against", "default")
.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")
.description("Starts an interactive insights browser for events in a specific archive and timerange")
.action(async (cmd) => {
initAuth(cmd);
await archiveUtil.insights(cmd.eventbus, cmd.rulePrefix);
});

const ruleDefault = "choose from template";
program
.command("local")
Expand All @@ -140,13 +129,22 @@ program
.option(
"-t, --template-file [sam]",
"Path to template file. Only used together with --rule option",
"template.yml"
"template.yaml"
)
.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 @@ -160,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
16 changes: 15 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 @@ -48,12 +50,24 @@ async function handler(event, context) {
.postToConnection({
ConnectionId: event.requestContext.connectionId,
Data: JSON.stringify({
Status: "Connected!",
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
4 changes: 2 additions & 2 deletions sam/src/builders/ruleArnCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ async function create(event) {
.promise();
const newRuleName = eventBridgeClient.getRuleName(busName);
ruleNames.push(newRuleName);
if (body.replayName && ruleResponse.EventPattern) {
if (body.replaySettings && ruleResponse.EventPattern) {
const pattern = JSON.parse(ruleResponse.EventPattern);
pattern["replay-name"] = [body.replayName];
pattern["replay-name"] = [body.replaySettings.ReplayName];
ruleResponse.EventPattern = JSON.stringify(pattern);
}
await eventBridgeClient.putRule(busName, ruleResponse, newRuleName);
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 f0f5757

Please sign in to comment.