Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made init() function create dynamo table instead of sqs client. #61

Open
wants to merge 1 commit into
base: feat/eventbridge-dynamo-refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 87 additions & 15 deletions src/helpers/eventBridge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
import { AWSError, EventBridge as AWSEventBridge, SQS } from "aws-sdk";
import { AWSError, EventBridge as AWSEventBridge, DynamoDB, Lambda } from "aws-sdk";
import { PromiseResult } from "aws-sdk/lib/request";
import { AWSClient, region } from "./general";
import { removeUndefinedMessages } from "./utils/removeUndefinedMessages";
Expand All @@ -10,7 +10,7 @@ export default class EventBridge {
eventBridgeName: string | undefined;
keep: boolean | undefined;
ruleName: string | undefined;
sqsClient: SQS | undefined;
dynamoClient: DynamoDB | undefined;
targetId: string | undefined;

async init(eventBridgeName: string): Promise<void> {
Expand All @@ -25,29 +25,98 @@ export default class EventBridge {
this.keep = keepArgEnabled || keepEnvVarEnabled;
const ruleNameArg = process.argv.filter((x) => x.startsWith("--event-rule-name="))[0];
this.ruleName = ruleNameArg ? ruleNameArg.split("=")[1] : `test-${eventBridgeName}-rule`;
const queueNameArg = process.argv.filter((x) => x.startsWith("--queue-name="))[0];
const queueName = queueNameArg ? queueNameArg.split("=")[1] : `${eventBridgeName}-testing-queue`;
const tableNameArg = process.argv.filter((x) => x.startsWith("--table-name="))[0];
const tableName : string = tableNameArg ? tableNameArg.split("=")[1] : `${eventBridgeName}-testing-table`;
let tableArn : string;

this.sqsClient = new AWSClient.SQS();
if (!this.keep) {
console.info(
"If running repeatedly add '--keep=true' to keep testing resources up to avoid creation throttles"
);
}

const queueResult = await this.sqsClient
.createQueue({
QueueName: queueName,
this.dynamoClient = new DynamoDB();
const tableResult: DynamoDB.CreateTableOutput = await this.dynamoClient
.createTable({
AttributeDefinitions: [
{
AttributeName: "pk",
AttributeType: "S",
},
{
AttributeName: "sk",
AttributeType: "S",
},
],
KeySchema: [
{
AttributeName: "pk",
KeyType: "HASH",
},
{
AttributeName: "sk",
KeyType: "RANGE",
},
],
TableName: tableName,
GlobalSecondaryIndexes: [
{
IndexName: "GSI1",
KeySchema: [
{
AttributeName: "gsi1pk",
KeyType: "HASH",
},
{
AttributeName: "gsi1sk",
KeyType: "RANGE",
},
],
Projection: {
ProjectionType: ALL,
},
},
{
IndexName: "GSI2",
KeySchema: [
{
AttributeName: "gsi2pk",
KeyType: "HASH",
},
{
AttributeName: "gsi2sk",
KeyType: "RANGE",
},
],
Projection: {
ProjectionType: ALL,
},
},
{
IndexName: "GSI3",
KeySchema: [
{
AttributeName: "gsi3pk",
KeyType: "HASH",
},
{
AttributeName: "gsi3sk",
KeyType: "RANGE",
},
],
Projection: {
ProjectionType: ALL,
},
},
],
})
.promise();

this.QueueUrl = queueResult.QueueUrl;
// this.QueueUrl = queueResult.QueueUrl;

if (this.QueueUrl === undefined) {
throw new Error("QueueUrl is undefined");
}
const accountId = this.QueueUrl.split("/")[3];
const sqsArn = `arn:aws:sqs:${region}:${accountId}:${queueName}`;
tableArn = tableResult.TableDescription?.TableArn;

const accountId : string = this.tableArn.split(":")[4];
const pattern = {
account: [`${accountId}`],
};
Expand All @@ -61,13 +130,16 @@ export default class EventBridge {
})
.promise();

// const lambdaClient = new Lambda({region: region});
// lambdaClient.createFunction({})

await this.eventBridgeClient
.putTargets({
EventBusName: eventBridgeName,
Rule: this.ruleName,
Targets: [
{
Arn: sqsArn,
Arn: lambdaArn,
Id: this.targetId,
},
],
Expand Down
Loading