Skip to content

Commit 8a4c1b4

Browse files
author
Charles Overbeck
authored
Upgrade Cloudwatch to Slack Lambda for Node 18 (#154)
* Upgrade Cloudwatch to Slack Lambda to node18 SEAB-5759 The Node18 lambda environment includes AWS SDK v3; Node14 had v2. Also got rid of unused files that confused me when trying to update this lambda.
1 parent c94a6a3 commit 8a4c1b4

6 files changed

Lines changed: 27 additions & 150 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
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
1+
This lambda handles relaying messages from AWS CloudWatch Alarms and Alerts into Slack. This lambda thus understands
2+
both events and alert SNS messages.

cloud-watch-to-slack-testing/cloud-watch-to-slack-testing.yaml

Lines changed: 0 additions & 34 deletions
This file was deleted.

cloud-watch-to-slack-testing/deployment/index.js

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

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

2424
// The Slack URL to send the message to
2525
const hookUrl = process.env.hookUrl;
@@ -40,46 +40,38 @@ function getInstanceNameAndSendMsgToSlack(
4040
processEventCallback,
4141
callback
4242
) {
43-
const ec2 = new AWS.EC2();
44-
45-
ec2.describeInstances(function (err, result) {
46-
if (err) console.log(err);
47-
// Log the error message.
48-
else {
49-
for (var i = 0; i < result.Reservations.length; i++) {
50-
var res = result.Reservations[i];
51-
var instances = res.Instances;
52-
53-
// Try to get the user friendly name of the EC2 target instance
54-
var instance = instances.find(
55-
(instance) => instance.InstanceId === targetInstanceId
56-
);
57-
var tagInstanceNameKey =
58-
instance && instance.Tags.find((tag) => "Name" === tag.Key);
59-
if (tagInstanceNameKey) {
60-
var tagInstanceName = tagInstanceNameKey.Value || null;
61-
return callback(
62-
slackChannel,
63-
messageText,
64-
tagInstanceName,
65-
targetInstanceId,
66-
processEventCallback
67-
);
68-
}
69-
}
70-
}
71-
// If there was an error or the user friendly name was not found just send the
72-
// message to Slack with a default name for the target
73-
return callback(
43+
instanceName(targetInstanceId).then((friendlyName) => {
44+
callback(
7445
slackChannel,
7546
messageText,
76-
null,
47+
friendlyName,
7748
targetInstanceId,
7849
processEventCallback
7950
);
8051
});
8152
}
8253

54+
/**
55+
* If the instanceId exists and has a tag whose key is "Name", returns that tag's value, otherwise returns null.
56+
* @param instanceId
57+
* @returns {Promise<void>}
58+
*/
59+
async function instanceName(instanceId) {
60+
const client = new EC2Client();
61+
const command = new DescribeInstancesCommand({
62+
InstanceIds: [instanceId],
63+
});
64+
try {
65+
const result = await client.send(command); // Throws if the instance is not found
66+
const instance = result.Reservations[0].Instances; // Safe to assume, as previous line would have thrown
67+
const tagInstanceNameKey = instance.Tags.find((tag) => "Name" === tag.Key);
68+
return tagInstanceNameKey?.Value || null;
69+
} catch (e) {
70+
console.error("Error describing instance", e);
71+
}
72+
return null;
73+
}
74+
8375
function constructMsgAndSendToSlack(
8476
slackChannel,
8577
messageText,

cloud-watch-to-slack-testing/deployment/output-template.yaml

Lines changed: 0 additions & 37 deletions
This file was deleted.

cloud-watch-to-slack-testing/deployment/package.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

cloud-watch-to-slack-testing/deployment/template.yaml

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)