Skip to content

Commit

Permalink
Feat/35 publish to eventbus (#12)
Browse files Browse the repository at this point in the history
* feat: edit to block CI with linting errors

* feat: creating eventStack

* feat: adding events stack deployment to ci

* feat: adding client-eventbridge

* feat: WIP editing event class

* feat: add event publishing

* feat: adding eventbridge package

* feat: adding iam package

* feat: creating an event bus and outputting ARN

* feat: importing EventBusARN and creating lambda policy

* feat: updating Event publish function

* feat: calling event.publish in producer handler

* feat: creating handler test

* feat: installing client-eventbridge

* feat: returning output of publish in handler

* fix: fixing eventBridge.send parameters

* fix: updating types integration test

* fix: updating handler test

* feat: updating integration tests

* feat: adding to docs

* fix: removing unecessary packages

* test: writing Event class tests

* fix: updating events lockfile

* fix: cr comment- clearer role naming

* fix: removing unecessary vitest test file

* fix: consistent naming convention on AWS resources

* feat: adding AWSLambdaBasicExecutionRole to lambda

* feat: adding error handling for publish

* fix: fixing small error

* fix: editing error handling

* fix: fixing naming inconsistency

* fix: extra error fix

* fix: testing new error logic

* fix: ci change to allow CfnOutput to work

* fix: reordering ci

---------

Co-authored-by: Luke Yianni <[email protected]>
  • Loading branch information
April-Bates-Dev and lukey-aleios authored Aug 10, 2023
1 parent 21e0273 commit 5136d87
Show file tree
Hide file tree
Showing 18 changed files with 2,896 additions and 121 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,17 @@ jobs:
with:
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
- name: "🧪 Run tests"
run: pnpm test --verbose
- name: '🧪 Test linter'
run: pnpm lint-test
- name: 🚀 Install Events Dependencies and Deploy
run: |
cd example-architecture/events
pnpm install --frozen-lockfile
pnpm deploy-dev
- name: 🚀 Install Producer Dependencies and Deploy
run: |
cd example-architecture/producer
pnpm install --frozen-lockfile
pnpm deploy-dev
- name: "🧪 Test unit and integration"
run: pnpm test --verbose
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@ export type PersonRegisteredContract = {

## Creating an Event

The Event class bakes in a lot of best practices, you can import and use it like so:
The Event class bakes in a lot of best practices.
To instantiate an eventbridge-toolbox event, create a new Event and pass in your event data. You can then publish your event by calling the `publish` function, passing in the ARN of the EventBus which you want to publish your event to.

You can see an example below:

```typescript
import Event from "@eventbridge-toolbox";

const MyEvent = new Event(data);
const data: LoggedInContract = {
firstName: "Lucy",
lastName: "Example",
timeLoggedIn: "2023-01-01T13:00:00.000Z",
};

const myEvent = new Event(data);

const EVENT_BUS_ARN = getEnvVariable("EVENT_BUS_ARN");

await myEvent.publish(EVENT_BUS_ARN);
```

## Key Features
Expand Down
30 changes: 30 additions & 0 deletions example-architecture/__tests__/importEvent.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { type PutEventsResponse } from "@aws-sdk/client-eventbridge";
import { Lambda } from "@aws-sdk/client-lambda";
import { describe, expect, it } from "vitest";

const lambda = new Lambda({ region: "eu-west-2" });

describe("Given a producer lambda that returns a Contract", () => {
const params = {
FunctionName: "producerLambda",
};
describe("When that lambda is invoked", async () => {
const invokedLambda = await lambda.invoke(params);

it("Response with an eventId is returned without an error", () => {
const body: PutEventsResponse = JSON.parse(
Buffer.from(invokedLambda.Payload ?? "").toString(),
);
expect(body.Entries).toEqual([
expect.objectContaining({ EventId: expect.any(String) }),
]);
expect(body.Entries).toEqual([
expect.not.objectContaining({
ErrorCode: expect.any(String),
ErrorMessage: expect.any(String),
}),
]);
});
});
});
6 changes: 6 additions & 0 deletions example-architecture/events/bin/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { App } from "aws-cdk-lib";

import { EventsStack } from "../lib/eventsStack";

const app = new App();
new EventsStack(app, "EventsStack");
49 changes: 49 additions & 0 deletions example-architecture/events/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"app": "npx ts-node --prefer-ts-exts bin/events.ts",
"requireApproval": "never",
"watch": {
"include": ["**"],
"exclude": [
"README.md",
"cdk*.json",
"tsconfig.json",
"node_modules",
"test"
]
},
"context": {
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
"@aws-cdk/core:checkSecretUsage": true,
"@aws-cdk/core:target-partitions": ["aws", "aws-cn"],
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
"@aws-cdk/aws-iam:minimizePolicies": true,
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
"@aws-cdk/core:enablePartitionLiterals": true,
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
"@aws-cdk/aws-route53-patters:useCertificate": true,
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
"@aws-cdk/aws-redshift:columnId": true,
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
"@aws-cdk/aws-kms:aliasNameRef": true,
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true
}
}
17 changes: 17 additions & 0 deletions example-architecture/events/lib/eventsStack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CfnOutput, Stack, StackProps } from "aws-cdk-lib";
import { EventBus } from "aws-cdk-lib/aws-events";
import { Construct } from "constructs";

export class EventsStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const eventBus = new EventBus(this, "ExampleArchitectureEventBus", {
eventBusName: "exampleArchitectureEventBus",
});

new CfnOutput(this, "EventBusARN", {
value: eventBus.eventBusArn,
exportName: "EventBusARN",
});
}
}
26 changes: 26 additions & 0 deletions example-architecture/events/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "events",
"version": "0.1.0",
"bin": {
"events": "bin/events.js"
},
"scripts": {
"bootstrap": "cdk bootstrap",
"cdk": "cdk",
"deploy-dev": "cdk deploy --verbose --all",
"lint-fix": "eslint . --ext=js,ts --fix"
},
"devDependencies": {
"@types/node": "^20.4.5",
"aws-cdk": "2.89.0",
"aws-cdk-lib": "2.89.0",
"constructs": "^10.0.0",
"esbuild": "^0.18.17",
"ts-node": "^10.9.1",
"typescript": "~5.1.6"
},
"dependencies": {
"@aws-sdk/client-eventbridge": "^3.387.0",
"aws-sdk": "^2.1431.0"
}
}
Loading

0 comments on commit 5136d87

Please sign in to comment.