Skip to content

Commit

Permalink
fix subscribe url
Browse files Browse the repository at this point in the history
  • Loading branch information
nitrag committed Nov 17, 2023
1 parent b910bbb commit 6a5028d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ This automation will provide a Webhook (HTTPS URL) which you can configure a SNS
with, allowing Cloudwatch Alarms to be posted to your Slack Channel in a human
readable format.

*Please note that the features in this project requires that the workspace be part of
[a Slack paid plan](https://slack.com/pricing).*
_Please note that the features in this project requires that the workspace be part of
[a Slack paid plan](https://slack.com/pricing)._

---

Expand Down
12 changes: 9 additions & 3 deletions functions/sns_transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const SNSTransformFunctionDefinition = DefineFunction({
subscribeUrl: {
type: Schema.types.string,
description: "URL to acknowledge a newly created SNS Subscription",
},
}
},
required: ["message"],
},
Expand All @@ -38,11 +38,17 @@ export const SNSTransformFunctionDefinition = DefineFunction({
export default SlackFunction(
SNSTransformFunctionDefinition,
async ({ inputs }) => {
console.log(inputs);
const { message, subscribeUrl } = inputs;
if (subscribeUrl) {
console.log(`Activating SNS Subscription: ${subscribeUrl}`);
await fetch(subscribeUrl);
return { outputs: { channelResponse: "SNS Subscription Activated!" } };
try {
await fetch(subscribeUrl);
return { outputs: { channelResponse: "SNS Subscription Activated!" } };
} catch (error) {
console.log(`Error activating SNS Subscription: ${error}`);
return { outputs: { channelResponse: `Error activating SNS Subscription, here is the URL: ${subscribeUrl}` } };
}
}
const payload = typeof message === "string" ? JSON.parse(message) : message;
const { AlarmName, OldStateValue, NewStateValue, NewStateReason } = payload;
Expand Down
10 changes: 9 additions & 1 deletion functions/sns_transform_function_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import SNSTransform from "./sns_transform.ts";

const { createContext } = SlackFunctionTester("sns_transform_function");

Deno.test("Greeting function test", async () => {

Deno.test("Test Subscribe", async () => {
const inputs = { message: "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message", subscribeUrl: "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription" };
const { outputs } = await SNSTransform(createContext({ inputs }));
assert(outputs?.channelResponse.includes("SNS Subscription"));
});


Deno.test("Test Alarm", async () => {
const inputs = { message: "{\"AlarmName\":\"RDS Prod - CPU Util\",\"AlarmDescription\":\"## RDS Prod - CPU Util %\",\"AWSAccountId\":\"682846136134\",\"AlarmConfigurationUpdatedTimestamp\":\"2023-08-31T13:00:37.714+0000\",\"NewStateValue\":\"OK\",\"NewStateReason\":\"Thresholds Crossed: 1 out of the last 3 datapoints [40.609605060341245 (08/09/23 15:36:00)] was not greater than the upper thresholds [44.47595391472755] (minimum 1 datapoint for ALARM -> OK transition).\",\"StateChangeTime\":\"2023-09-08T15:47:04.677+0000\",\"Region\":\"US West (N. California)\",\"AlarmArn\":\"arn:aws:cloudwatch:us-west-1:12341234:alarm:RDS Prod - CPU Util\",\"OldStateValue\":\"ALARM\",\"OKActions\":[\"arn:aws:sns:us-west-1:12341234:ops-us-west-1\"],\"AlarmActions\":[\"arn:aws:sns:us-west-1:12341234:ops-us-west-1\"],\"InsufficientDataActions\":[],\"Trigger\":{\"Period\":300,\"EvaluationPeriods\":3,\"DatapointsToAlarm\":3,\"ComparisonOperator\":\"GreaterThanUpperThreshold\",\"ThresholdMetricId\":\"ad1\",\"TreatMissingData\":\"missing\",\"EvaluateLowSampleCountPercentile\":\"\",\"Metrics\":[{\"Id\":\"m1\",\"MetricStat\":{\"Metric\":{\"Dimensions\":[{\"value\":\"trailsoffroad-prod-v3\",\"name\":\"DBInstanceIdentifier\"}],\"MetricName\":\"CPUUtilization\",\"Namespace\":\"AWS/RDS\"},\"Period\":300,\"Stat\":\"Average\"},\"ReturnData\":true},{\"Expression\":\"ANOMALY_DETECTION_BAND(m1, 12)\",\"Id\":\"ad1\",\"Label\":\"CPUUtilization (expected)\",\"ReturnData\":true}]}}" };
const { outputs } = await SNSTransform(createContext({ inputs }));
assert(outputs?.channelResponse.includes("RDS Prod - CPU Util"));
Expand Down
4 changes: 2 additions & 2 deletions import_map.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"imports": {
"deno-slack-sdk/": "https://deno.land/x/deno_slack_sdk@2.2.0/",
"deno-slack-api/": "https://deno.land/x/deno_slack_api@2.1.1/"
"deno-slack-sdk/": "https://deno.land/x/deno_slack_sdk@2.5.0/",
"deno-slack-api/": "https://deno.land/x/deno_slack_api@2.1.2/"
}
}
2 changes: 1 addition & 1 deletion slack.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hooks": {
"get-hooks": "deno run -q --allow-read --allow-net https://deno.land/x/deno_slack_hooks@1.1.0/mod.ts"
"get-hooks": "deno run -q --allow-read --allow-net https://deno.land/x/deno_slack_hooks@1.2.2/mod.ts"
}
}
4 changes: 2 additions & 2 deletions workflows/sns_alarm_workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const SNSAlarmWorkflow = DefineWorkflow({
properties: {
message: {
type: Schema.types.string,
description: "The raw message (JSON) payload received from AWS Cloudwatch Alarm via SNS"
description: "The Message payload received from AWS Cloudwatch Alarm via SNS"
},
subscribeUrl: {
type: Schema.types.string,
Expand All @@ -34,7 +34,7 @@ const transformFunctionStep = SNSAlarmWorkflow.addStep(
SNSTransformFunctionDefinition,
{
message: SNSAlarmWorkflow.inputs.message,
subscribeUrl: SNSAlarmWorkflow.inputs.subscribeUrl,
subscribeUrl: SNSAlarmWorkflow.inputs.subscribeUrl
},
);

Expand Down

0 comments on commit 6a5028d

Please sign in to comment.