Skip to content

Commit

Permalink
Merge pull request #5 from stevemorland/main
Browse files Browse the repository at this point in the history
Various changes to port over from internal
  • Loading branch information
stevemorland authored Dec 5, 2023
2 parents ce0bdd8 + 522bec5 commit 496f535
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 104 deletions.
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ cdk.out
src
client
clisrc
!lib/construct/src
!lib/construct/src
*.githhub
cli
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ The Test Client validates the event you will send to ensure it matches the Schem

Test that your captured events match a published JSON Schema specification.

#### Event Monitoring Client - Collect Events to a WebUI in realtime
#### COMING SOON: Event Monitoring Client - Collect Events to a WebUI in realtime

A web application that can connect to your WebSocket, allowing you to inspect, save and copy the published events. Useful when developing for instant feedback without adding lots of console logs to your Jest tests or logging out through compute.

---

## Using the EventNet CDK Construct

Install and import the construct.
Install:

```bash

yarn add @leighton-digitial/eventnet

```

& Import the construct:

```Typescript
import { EventNet } from "@leighton-digital/event-net/lib/construct/";
Expand All @@ -41,12 +49,12 @@ Set up the EventNet instance with the EventBridge instance.
```Typescript
const eventNet = new EventNet(
this,
"event-net",
"EventNet",
{
prefix: stackName,
eventBusName: eventBusName,
includeOutput: true, // optional, default is false
invludeLogs: true // optional, default is false
includeLogs: true // optional, default is false
}
);
```
Expand All @@ -68,7 +76,7 @@ const eventNet = new EventNet(
- The Test Client collects the events from the WebSocket.

```Typescript
import { EventNetClient } from "@leighton-digital/event-net";
import { EventNetClient } from "@leighton-digital/eventnet";
```

The EventNet client expects your `prefix` from the CDK construct to be passed in as `'--stack=my-service-dev'`. This corresponds to the Cloud Formation stack name produced by CDK. i.e.:
Expand All @@ -83,7 +91,7 @@ If you are using multiple Event Buses, we strongly recommend basing the names fr

```Typescript

import { EventNetClient, stackName } from "@leighton-digital/event-net";
import { EventNetClient, stackName } from "@leighton-digital/eventnet";


describe("Test Producer > ", () => {
Expand Down Expand Up @@ -111,7 +119,7 @@ describe("Test Producer > ", () => {
await eventNet.waitForClosedSocket()

// Use the Jest assertion to check
// the event against the JSONschema,
// the event against the JSONSchema,
// more on this below
expect(events[0].detail).toMatchJsonSchema(EventSpec);

Expand All @@ -131,7 +139,7 @@ describe("Test Producer > ", () => {

```Typescript

import { EventNetClient } from "@leighton-digital/event-net";
import { EventNetClient } from "@leighton-digital/eventnet";
import * as EventSpec from "../../jsonSpecs/someSchema-v1.json";

describe("Test Consumer > ", () => {
Expand All @@ -149,9 +157,9 @@ describe("Test Consumer > ", () => {
},

// Use the Jest assertion to check
// the event against the JSONschema,
// the event against the JSONSchema,
// more on this below
expect(Event.Detail).toMatchJsonSchema(EventSpec);
expect(Event).toMatchJsonSchema(EventSpec);

// send the event to the EventBrdge instance
const sendEvents = await eventNet.sendEvent(Event, EventBusName)
Expand Down Expand Up @@ -190,12 +198,12 @@ expect(singleMessage).toMatchJsonSchema(EventSpec);

---

## Using the EventNet Web Client
## COMING SOON: Using the EventNet Web Client

First, you need to run the client:

```
eventNet start
eventnet start
```

This will open a web client on url: http://localhost:3000
Expand Down
21 changes: 8 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@leighton-digital/eventnet",
"version": "0.0.1",
"version": "0.0.3",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -28,26 +28,24 @@
"@types/uuid": "^9.0.2",
"@types/ws": "^8.5.4",
"@types/yargs": "^17.0.24",
"aws-cdk-lib": "2.79.1",
"aws-cdk-lib": "2.113.0",
"constructs": "^10.0.0",
"jest": "^29.5.0",
"release-it": "^16.1.0",
"ts-jest": "^29.0.5",
"typescript": "~4.9.5"
},
"peerDependencies": {
"aws-cdk-lib": "2.79.1",
"aws-cdk-lib": "2.113.0",
"constructs": "^10.0.0"
},
"dependencies": {
"@aws-cdk/aws-apigatewayv2-alpha": "^2.79.1-alpha.0",
"@aws-cdk/aws-apigatewayv2-integrations-alpha": "^2.79.1-alpha.0",
"@aws-sdk/client-dynamodb": "^3.348.0",
"@aws-sdk/lib-dynamodb": "^3.348.0",
"@aws-sdk/client-apigatewaymanagementapi": "^3.454.0",
"@aws-sdk/credential-providers": "^3.454.0",
"@aws-sdk/client-ssm": "^3.454.0",
"@aws-sdk/client-dynamodb": "^3.348.0",
"@aws-sdk/client-eventbridge": "^3.454.0",
"@aws-sdk/client-ssm": "^3.454.0",
"@aws-sdk/credential-providers": "^3.465.0",
"@aws-sdk/lib-dynamodb": "^3.348.0",
"@types/figlet": "^1.5.6",
"@types/inquirer": "^9.0.3",
"@types/shelljs": "^0.8.12",
Expand All @@ -61,10 +59,7 @@
"ws": "^8.13.0",
"yargs": "^17.7.2"
},
"bin": {
"eventnet": "cli/index.js"
},
"description": "<img width=\"50px\" height=\"50px\" align=\"right\" alt=\"EventNet Logo\" src=\"eventnet.png?sanitize=true\" title=\"Leighton EventNet\"/>",
"description": "Several tools for working with AWS EventBridge and the events published through it.",
"keywords": [
"CDK",
"Jest",
Expand Down
15 changes: 13 additions & 2 deletions src/construct/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct } from "constructs";
import { WebSocketLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
import * as apigwv2 from "@aws-cdk/aws-apigatewayv2-alpha";
import { WebSocketLambdaIntegration } from "aws-cdk-lib/aws-apigatewayv2-integrations";
import * as apigwv2 from "aws-cdk-lib/aws-apigatewayv2";
import * as cdk from "aws-cdk-lib";
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
import * as events from "aws-cdk-lib/aws-events";
Expand Down Expand Up @@ -83,6 +83,7 @@ export class EventNet extends Construct {
entry: path.join(path.resolve(__dirname), `/src/lambda/connected.js`),
environment: {
TABLE_NAME: table.tableName,
REGION: region,
},
memorySize: 1024,
architecture: lambda.Architecture.ARM_64,
Expand Down Expand Up @@ -113,6 +114,7 @@ export class EventNet extends Construct {
),
environment: {
TABLE_NAME: table.tableName,
REGION: region,
},
memorySize: 1024,
architecture: lambda.Architecture.ARM_64,
Expand Down Expand Up @@ -163,6 +165,7 @@ export class EventNet extends Construct {
TABLE_NAME: table.tableName,
API_ID: webSocketApi.apiId,
STAGE: wsStage.stageName,
REGION: region,
},
memorySize: 1024,
architecture: lambda.Architecture.ARM_64,
Expand All @@ -176,6 +179,14 @@ export class EventNet extends Construct {
}
);

if (props.includeLogs) {
new LogGroup(this, `nodeJsFunctionLogGroupDataHandler`, {
logGroupName: `/aws/lambda/${eventNetFunction.functionName}`,
removalPolicy: cdk.RemovalPolicy.DESTROY,
retention: RetentionDays.ONE_WEEK,
});
}

rule.addTarget(
new targets.LambdaFunction(eventNetFunction, {
retryAttempts: 3,
Expand Down
22 changes: 7 additions & 15 deletions src/construct/src/lambda/connected.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { BatchWriteCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { PutCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";

import { APIGatewayProxyEvent } from "aws-lambda";

Expand All @@ -9,30 +9,22 @@ const client = new DynamoDBClient({
const docClient = DynamoDBDocumentClient.from(client);

export const main = async (event: APIGatewayProxyEvent) => {
console.log({ event });
const tableName = process.env.TABLE_NAME;

if (!tableName) {
throw new Error("tableName not specified in process.env.TABLE_NAME");
}

const writeRequest = {
RequestItems: {
[`${process.env.TABLE_NAME}`]: [
{
PutRequest: {
Item: {
PK: event.requestContext.connectionId,
},
},
},
],
const putItem = {
TableName: process.env.TABLE_NAME,
Item: {
PK: event.requestContext.connectionId,
},
};

try {
await await docClient.send(
new BatchWriteCommand(writeRequest)
);;
await docClient.send(new PutCommand(putItem));
} catch (err) {
return {
statusCode: 500,
Expand Down
17 changes: 12 additions & 5 deletions src/construct/src/lambda/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, ScanCommand } from "@aws-sdk/lib-dynamodb";
import { ApiGatewayManagementApiClient, GetConnectionCommand, PostToConnectionCommand } from "@aws-sdk/client-apigatewaymanagementapi";
import {
ApiGatewayManagementApiClient,
GetConnectionCommand,
PostToConnectionCommand,
} from "@aws-sdk/client-apigatewaymanagementapi";

const client = new DynamoDBClient({
region: process.env.AWS_REGION,
Expand All @@ -9,7 +13,7 @@ const docClient = DynamoDBDocumentClient.from(client);

const apiClient = new ApiGatewayManagementApiClient({
region: process.env.AWS_REGION,
endpoint: `https://${process.env.API_ID}.execute-api.${process.env.AWS_REGION}.amazonaws.com/${process.env.STAGE}`
endpoint: `https://${process.env.API_ID}.execute-api.${process.env.AWS_REGION}.amazonaws.com/${process.env.STAGE}`,
});

export const main = async (event: any): Promise<any> => {
Expand All @@ -26,17 +30,20 @@ export const main = async (event: any): Promise<any> => {
}

try {
connectionData = await docClient.send(new ScanCommand({ TableName: tableName, ProjectionExpression: "PK" }));
connectionData = await docClient.send(
new ScanCommand({ TableName: tableName, ProjectionExpression: "PK" })
);
} catch (e) {
return { statusCode: 500, body: e };
}

console.log({ connectionData });

const postCalls = (connectionData.Items ?? []).map(async ({ PK }) => {
try {
const input = { ConnectionId: PK, Data: JSON.stringify(event) };

const command = new PostToConnectionCommand(input);
apiClient.send(command);
await apiClient.send(command);
} catch (e) {
console.log(e);
throw e;
Expand Down
25 changes: 22 additions & 3 deletions src/utils/AWSConfig/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { loadArg } from "../options/args";
const { getDefaultRoleAssumerWithWebIdentity } = require("@aws-sdk/client-sts");
const { defaultProvider } = require("@aws-sdk/credential-provider-node");
const { fromIni } = require("@aws-sdk/credential-providers");

export const stackName = loadArg({
cliArg: "stack",
Expand All @@ -13,8 +14,26 @@ export const region = loadArg({
defaultValue: "eu-west-2",
});

const provider = defaultProvider({
roleAssumerWithWebIdentity: getDefaultRoleAssumerWithWebIdentity({ region }),
const profile = loadArg({
cliArg: "profile",
processEnvName: "PROFILE",
});

export const AWSConfig = { credentialDefaultProvider: provider };
let AWSConfigObject: any = {
provider: defaultProvider({
roleAssumerWithWebIdentity: getDefaultRoleAssumerWithWebIdentity({
region,
}),
}),
};

if (profile) {
AWSConfigObject = {
credentials: fromIni({
profile,
}),
region,
};
}

export const AWSConfig = AWSConfigObject;
2 changes: 1 addition & 1 deletion src/utils/JSONSchemaClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default {
toMatchSchema(theEvent: any, theSchema: any) {
try {
var validate = ajv.compile(theSchema);
var valid = validate(theEvent.detail);
var valid = validate(theEvent.detail || theEvent.Detail);
if (!valid) {
let warning: string = "Event";
validate.errors.forEach((msg: any) => {
Expand Down
Loading

0 comments on commit 496f535

Please sign in to comment.