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

Upgrade Cloudwatch to Slack Lambda for Node 18 #154

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion cloud-watch-to-slack-testing/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
This lambda handles relaying messages from AWS CloudWatch Alarms and Alerts into Slack. This lambda thus understands both events and alert SNS messages and with testing may replace serverlessrepo-cloudwatch-a-cloudwatchalarmtoslack-60UO1LIXCS1Y
This lambda handles relaying messages from AWS CloudWatch Alarms and Alerts into Slack. This lambda thus understands
both events and alert SNS messages.
34 changes: 0 additions & 34 deletions cloud-watch-to-slack-testing/cloud-watch-to-slack-testing.yaml

This file was deleted.

58 changes: 25 additions & 33 deletions cloud-watch-to-slack-testing/deployment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const url = require("url");
const https = require("https");
const AWS = require("aws-sdk");
const { EC2Client, DescribeInstancesCommand } = require("@aws-sdk/client-ec2");

// The Slack URL to send the message to
const hookUrl = process.env.hookUrl;
Expand All @@ -40,46 +40,38 @@ function getInstanceNameAndSendMsgToSlack(
processEventCallback,
callback
) {
const ec2 = new AWS.EC2();

ec2.describeInstances(function (err, result) {
if (err) console.log(err);
// Log the error message.
else {
for (var i = 0; i < result.Reservations.length; i++) {
var res = result.Reservations[i];
var instances = res.Instances;

// Try to get the user friendly name of the EC2 target instance
var instance = instances.find(
(instance) => instance.InstanceId === targetInstanceId
);
var tagInstanceNameKey =
instance && instance.Tags.find((tag) => "Name" === tag.Key);
if (tagInstanceNameKey) {
var tagInstanceName = tagInstanceNameKey.Value || null;
return callback(
slackChannel,
messageText,
tagInstanceName,
targetInstanceId,
processEventCallback
);
}
}
}
// If there was an error or the user friendly name was not found just send the
// message to Slack with a default name for the target
return callback(
instanceName(targetInstanceId).then((friendlyName) => {
callback(
slackChannel,
messageText,
null,
friendlyName,
targetInstanceId,
processEventCallback
);
});
}

/**
* If the instanceId exists and has a tag whose key is "Name", returns that tag's value, otherwise returs null.
coverbeck marked this conversation as resolved.
Show resolved Hide resolved
* @param instanceId
* @returns {Promise<void>}
*/
async function instanceName(instanceId) {
const client = new EC2Client();
const command = new DescribeInstancesCommand({
InstanceIds: [instanceId],
});
try {
const result = await client.send(command); // Throws if the instance is not found
const instance = result.Reservations[0].Instances; // Safe to assume, as previous line would have thrown
const tagInstanceNameKey = instance.Tags.find((tag) => "Name" === tag.Key);
return tagInstanceNameKey && tagInstanceNameKey.Value;
coverbeck marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
console.error("Error describing instance", e);
}
return null;
}

function constructMsgAndSendToSlack(
slackChannel,
messageText,
Expand Down
37 changes: 0 additions & 37 deletions cloud-watch-to-slack-testing/deployment/output-template.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions cloud-watch-to-slack-testing/deployment/package.json

This file was deleted.

37 changes: 0 additions & 37 deletions cloud-watch-to-slack-testing/deployment/template.yaml

This file was deleted.

Loading