Skip to content

Commit

Permalink
feat: Add extractor (#3)
Browse files Browse the repository at this point in the history
* Added CloudFormation template extratctor

* Added `yaml-cfn`package

* Added `app.ts`

* Added CloudFormation extractor tests

* Added filter tests

* fix: Added fixed

* Linted files

* update implementation

* fix typo
  • Loading branch information
soflass1293 authored Jan 24, 2024
1 parent 44ecbff commit 52ea5d9
Show file tree
Hide file tree
Showing 20 changed files with 2,293 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .projen/deps.json

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

4 changes: 2 additions & 2 deletions .projen/tasks.json

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

5 changes: 4 additions & 1 deletion .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const project = new typescript.TypeScriptProject({
name: "sqs-lambda-assistant",
projenrcTs: true,

deps: ["cloudform-types"] /* Runtime dependencies of this module. */,
deps: [
"cloudform-types",
"yaml-cfn",
] /* Runtime dependencies of this module. */,
// description: undefined, /* The description is just a string that helps people understand the purpose of the package. */
// devDeps: [], /* Build dependencies for this module. */
// packageName: undefined, /* The "name" in package.json. */
Expand Down
114 changes: 114 additions & 0 deletions a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

############### - 7 - ################

--- SQSs of 7 ---
[
{
"LogicalId": "jobsDlqD18CF374",
"Properties": {
"MessageRetentionPeriod": 1209600,
"QueueName": "aws-node-sqs-worker-project-dev-jobs-dlq"
}
},
{
"LogicalId": "jobsQueueCEDBAE3E",
"Properties": {
"DelaySeconds": 60,
"QueueName": "aws-node-sqs-worker-project-dev-jobs",
"RedrivePolicy": {
"deadLetterTargetArn": {
"Fn::GetAtt": [
"jobsDlqD18CF374",
"Arn"
]
},
"maxReceiveCount": 3
},
"VisibilityTimeout": 69
}
}
]
--- Lambdas of 7 ---
[
{
"LogicalId": "ProducerLambdaFunction",
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "ServerlessDeploymentBucket"
},
"S3Key": "serverless/aws-node-sqs-worker-project/dev/1705833879496-2024-01-21T10:44:39.496Z/aws-node-sqs-worker-project.zip"
},
"Handler": "index.producer",
"Runtime": "nodejs18.x",
"FunctionName": "aws-node-sqs-worker-project-dev-producer",
"MemorySize": 1024,
"Timeout": 6,
"Environment": {
"Variables": {
"QUEUE_URL": {
"Ref": "jobsQueueCEDBAE3E"
}
}
},
"Role": {
"Fn::GetAtt": [
"IamRoleLambdaExecution",
"Arn"
]
}
}
},
{
"LogicalId": "JobsWorkerLambdaFunction",
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "ServerlessDeploymentBucket"
},
"S3Key": "serverless/aws-node-sqs-worker-project/dev/1705833879496-2024-01-21T10:44:39.496Z/aws-node-sqs-worker-project.zip"
},
"Handler": "index.consumer",
"Runtime": "nodejs18.x",
"FunctionName": "aws-node-sqs-worker-project-dev-jobsWorker",
"MemorySize": 1024,
"Timeout": 6,
"Role": {
"Fn::GetAtt": [
"IamRoleLambdaExecution",
"Arn"
]
},
"ReservedConcurrentExecutions": 200
}
}
]
--- EventSourceMappings of 7 ---
[
{
"LogicalId": "JobsWorkerEventSourceMappingSQSJobsQueueCEDBAE3E",
"Properties": {
"BatchSize": 5,
"MaximumBatchingWindowInSeconds": 33,
"EventSourceArn": {
"Fn::GetAtt": [
"jobsQueueCEDBAE3E",
"Arn"
]
},
"FunctionName": {
"Fn::GetAtt": [
"JobsWorkerLambdaFunction",
"Arn"
]
},
"Enabled": true,
"FunctionResponseTypes": [
"ReportBatchItemFailures"
],
"ScalingConfig": {
"MaximumConcurrency": 1000
}
}
}
]
82 changes: 82 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Lambda, SQS } from "cloudform-types";
import { extract } from "./src/helpers/cf-extractor";
import { filter } from "./src/helpers/filter";
import { Worker } from "./src/worker";

const fs = require("fs");

const run = () => {
const workers: Worker[] = [];
for (let index = 1; index <= 7; index++) {
const path = `./mock/template${index}.json`;
const template = JSON.parse(fs.readFileSync(path));
const filtered = filter(template);
filtered.forEach(
(
options: Record<
string,
| Partial<SQS.Queue>
| Partial<Lambda.Function>
| Partial<Lambda.EventSourceMapping>
>,
) => {
const worker = new Worker({
id: `${index}_worker-${Date.now()}`,
// @ts-ignore
lambda: options.lambda,
// @ts-ignore
integration: options.integration,
// @ts-ignore
sqs: options.sqs,
});
workers.push(worker);
},
);
}
workers.forEach((worker: Worker) => {
worker.analyze();
});
};

const run1 = () => {
fs.writeFileSync("b.json", "");
for (let index = 7; index <= 7; index++) {
const path = `./mock/template${index}.json`;
const template = JSON.parse(fs.readFileSync(path));
const options = filter(template);
if (options.length > 0) {
fs.appendFileSync("b.json", `\n --- Options of ${index} --- \n`);
fs.appendFileSync("b.json", JSON.stringify(options, null, 2));
}
}
};
const run2 = () => {
fs.writeFileSync("a.json", "");
for (let index = 7; index <= 7; index++) {
const path = `./mock/template${index}.json`;
const template = JSON.parse(fs.readFileSync(path));
const sqs = extract(template, "AWS::SQS::Queue");
const lambdas = extract(template, "AWS::Lambda::Function");
const mappings = extract(template, "AWS::Lambda::EventSourceMapping");
if (sqs.length > 0 && lambdas.length > 0 && mappings.length > 0) {
fs.appendFileSync(
"a.json",
`\n############### - ${index} - ################\n`,
);
fs.appendFileSync("a.json", `\n --- SQSs of ${index} --- \n`);
fs.appendFileSync("a.json", JSON.stringify(sqs, null, 2));
fs.appendFileSync("a.json", `\n --- Lambdas of ${index} --- \n`);
fs.appendFileSync("a.json", JSON.stringify(lambdas, null, 2));
fs.appendFileSync(
"a.json",
`\n --- EventSourceMappings of ${index} --- \n`,
);
fs.appendFileSync("a.json", JSON.stringify(mappings, null, 2));
}
}
console.log("Done");
};

run();
run1();
run2();
72 changes: 72 additions & 0 deletions b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

--- Options of 7 ---
[
{
"integration": {
"LogicalId": "JobsWorkerEventSourceMappingSQSJobsQueueCEDBAE3E",
"Properties": {
"BatchSize": 5,
"MaximumBatchingWindowInSeconds": 33,
"EventSourceArn": {
"Fn::GetAtt": [
"jobsQueueCEDBAE3E",
"Arn"
]
},
"FunctionName": {
"Fn::GetAtt": [
"JobsWorkerLambdaFunction",
"Arn"
]
},
"Enabled": true,
"FunctionResponseTypes": [
"ReportBatchItemFailures"
],
"ScalingConfig": {
"MaximumConcurrency": 1000
}
}
},
"lambda": {
"LogicalId": "JobsWorkerLambdaFunction",
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "ServerlessDeploymentBucket"
},
"S3Key": "serverless/aws-node-sqs-worker-project/dev/1705833879496-2024-01-21T10:44:39.496Z/aws-node-sqs-worker-project.zip"
},
"Handler": "index.consumer",
"Runtime": "nodejs18.x",
"FunctionName": "aws-node-sqs-worker-project-dev-jobsWorker",
"MemorySize": 1024,
"Timeout": 6,
"Role": {
"Fn::GetAtt": [
"IamRoleLambdaExecution",
"Arn"
]
},
"ReservedConcurrentExecutions": 200
}
},
"sqs": {
"LogicalId": "jobsQueueCEDBAE3E",
"Properties": {
"DelaySeconds": 60,
"QueueName": "aws-node-sqs-worker-project-dev-jobs",
"RedrivePolicy": {
"deadLetterTargetArn": {
"Fn::GetAtt": [
"jobsDlqD18CF374",
"Arn"
]
},
"maxReceiveCount": 3
},
"VisibilityTimeout": 69
}
}
}
]
Loading

0 comments on commit 52ea5d9

Please sign in to comment.