Skip to content

Commit

Permalink
updates by lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Park committed Apr 19, 2024
1 parent 13c3a52 commit b8a0d75
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist

# Lock files
package-lock.json
24 changes: 24 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest"
},
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
}
}
60 changes: 28 additions & 32 deletions packages/cloudwatch-rds-alert/app/bin/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,46 @@ const WEBHOOK_URL_PARAMETER = process.env.WEBHOOK_URL_PARAMETER as string;
const ALERT_USERNAME = process.env.ALERT_USERNAME as string;
const ALERT_CHANNEL = process.env.ALERT_CHANNEL as string;

if (typeof RDSINSTANCES === "string") {
var instances = RDSINSTANCES.split(",");
}
const instances = RDSINSTANCES.split(",");

export class CloudwatchRDSAlertStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const topic = new aws_sns.Topic(this, "SNS");

for (var i in instances) {
const db = aws_rds.DatabaseInstance.fromDatabaseInstanceAttributes(
this,
instances[i],
{
instanceEndpointAddress: "garbage value", // Can be an arbitrary value
instanceIdentifier: instances[i], // CloudWatch looks out for this value
port: 3306, // Can be an arbitrary value
securityGroups: [
aws_ec2.SecurityGroup.fromLookupById(
this,
instances[i] + "-sg",
`${SECURITYGROUP}`
),
], // SG ID has to be valid
}
);
// Only "Average over 5 minutes" is available, hence one evaluation only before firing the alarm off:
// https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_rds.DatabaseInstance.html#metricwbrcpuutilizationprops
const alarm_cpu = new aws_cloudwatch.Alarm(
this,
instances[i] + "-alarm",
{
alarmName: instances[i] + " database CPU usage alert", // Notification title if sent to Slack
instances.forEach(instance => {
{
const db = aws_rds.DatabaseInstance.fromDatabaseInstanceAttributes(
this,
instance,
{
instanceEndpointAddress: "garbage value", // Can be an arbitrary value
instanceIdentifier: instance, // CloudWatch looks out for this value
port: 3306, // Can be an arbitrary value
securityGroups: [
aws_ec2.SecurityGroup.fromLookupById(
this,
instance + "-sg",
`${SECURITYGROUP}`
),
], // SG ID has to be valid
}
);
// Only "Average over 5 minutes" is available, hence one evaluation only before firing the alarm off:
// https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_rds.DatabaseInstance.html#metricwbrcpuutilizationprops
const alarm_cpu = new aws_cloudwatch.Alarm(this, instance + "-alarm", {
alarmName: instance + " database CPU usage alert", // Notification title if sent to Slack
evaluationPeriods: 1, // The number of periods over which data is compared to the specified threshold. ( 5 minute )
datapointsToAlarm: 1, // The number of datapoints that must be breaching to trigger the alarm.
threshold: 30, // over x %
metric: db.metricCPUUtilization(),
treatMissingData: aws_cloudwatch.TreatMissingData.BREACHING,
}
);
alarm_cpu.addAlarmAction(new aws_cloudwatch_actions.SnsAction(topic));
alarm_cpu.addOkAction(new aws_cloudwatch_actions.SnsAction(topic));
}
});
alarm_cpu.addAlarmAction(new aws_cloudwatch_actions.SnsAction(topic));
alarm_cpu.addOkAction(new aws_cloudwatch_actions.SnsAction(topic));
}
});

const notifySlack = new aws_lambda_nodejs.NodejsFunction(
this,
Expand Down
14 changes: 8 additions & 6 deletions packages/cloudwatch-rds-alert/app/handlers/notifySlack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const WEBHOOK_URL_PARAMETER = process.env.WEBHOOK_URL_PARAMETER as string;
const ALERT_USERNAME = process.env.ALERT_USERNAME as string;
const ALERT_CHANNEL = process.env.ALERT_CHANNEL as string;

const AWS = require("aws-sdk");
import * as AWS from "aws-sdk";
const SSM = new AWS.SSM();
// var responseFromSSM = null;
var parameter = {
const parameter = {
Name: WEBHOOK_URL_PARAMETER,
WithDecryption: true,
};
Expand Down Expand Up @@ -39,8 +39,8 @@ export const getFormattedSlackMessage = (snsMessage: string) => {
};

interface NotifySlackOutput {
event: string;
ALERT_CHANNEL: string;
event?: string;
ALERT_CHANNEL?: string;
status_code: number;
}

Expand All @@ -67,12 +67,14 @@ export const handler = async (
event: SNSEvent,
_context: Context
): Promise<NotifySlackOutput> => {
const functionConfig = await initPromise; // just wait until
await initPromise; // just wait until
const message = getFormattedMessage(ALERT_CHANNEL, event);
console.log("Message formatted from event: ", message);
if (!ALERT_CHANNEL) {
console.log("No alert channel specified - skipping...");
return null as any;
return {
status_code: 400,
};
}
const response = await sendAlertToChannel(ALERT_CHANNEL, message);
console.log(
Expand Down
2 changes: 0 additions & 2 deletions packages/serverless-deploy-iam/test/deploy-role.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as cdk from "@aws-cdk/core";
import {
expect as expectCDK,
matchTemplate,
MatchStyle,
haveResource,
haveResourceLike,
countResources,
Expand Down

0 comments on commit b8a0d75

Please sign in to comment.