From 7ce61dab9f0f8b8ebe8c1d18d32c4e72a4433891 Mon Sep 17 00:00:00 2001 From: Charles Overbeck Date: Wed, 26 Jul 2023 16:31:41 -0700 Subject: [PATCH 1/2] 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. --- cloud-watch-to-slack-testing/README.md | 3 +- .../cloud-watch-to-slack-testing.yaml | 34 ----------- .../deployment/index.js | 58 ++++++++----------- .../deployment/output-template.yaml | 37 ------------ .../deployment/package.json | 8 --- .../deployment/template.yaml | 37 ------------ 6 files changed, 27 insertions(+), 150 deletions(-) delete mode 100644 cloud-watch-to-slack-testing/cloud-watch-to-slack-testing.yaml delete mode 100644 cloud-watch-to-slack-testing/deployment/output-template.yaml delete mode 100644 cloud-watch-to-slack-testing/deployment/package.json delete mode 100644 cloud-watch-to-slack-testing/deployment/template.yaml diff --git a/cloud-watch-to-slack-testing/README.md b/cloud-watch-to-slack-testing/README.md index dfba5023..842766b0 100644 --- a/cloud-watch-to-slack-testing/README.md +++ b/cloud-watch-to-slack-testing/README.md @@ -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. diff --git a/cloud-watch-to-slack-testing/cloud-watch-to-slack-testing.yaml b/cloud-watch-to-slack-testing/cloud-watch-to-slack-testing.yaml deleted file mode 100644 index 76345301..00000000 --- a/cloud-watch-to-slack-testing/cloud-watch-to-slack-testing.yaml +++ /dev/null @@ -1,34 +0,0 @@ -AWSTemplateFormatVersion: '2010-09-09' -Transform: 'AWS::Serverless-2016-10-31' -Description: >- - An Amazon SNS trigger that sends CloudWatch alarm notifications to Slack - #dockstore-testing. -Resources: - cloudwatchtoslacktesting: - Type: 'AWS::Serverless::Function' - Properties: - Handler: index.handler - Runtime: nodejs8.10 - CodeUri: . - Description: >- - An Amazon SNS trigger that sends CloudWatch alarm notifications to Slack - #dockstore-testing. - MemorySize: 128 - Timeout: 3 - Role: >- - arn:aws:iam::312767926603:role/serverlessrepo-cloudwatch-cloudwatchalarmtoslackRo-1MWUBBKPY1IYG - Events: - SNS1: - Type: SNS - Properties: - Topic: - Ref: SNSTopic1 - Environment: - Variables: - kmsEncryptedHookUrl: >- - AQICAHicSBs6aaXy8Lb+IjLRg6DV8EoauPSVsU5VmBT+1qT9lQHToZoua3KzPa1BecYrbyk0AAAApzCBpAYJKoZIhvcNAQcGoIGWMIGTAgEAMIGNBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDIzxlGCJcqy30dT8kwIBEIBg0zj3RpxntbX1izzXdPa2SoJL6TbCeHBSOpsxtF679Ywlq6DHG6NWHth+Zj1lnhUdHqoqZK7JilcqJN8L6inFjcgmgZEwPsiQN3QWOr4ExwlxrKl3zKaxBn5i0f7Un+K+ - slackChannel: dockstore-testing - KmsKeyArn: >- - arn:aws:kms:us-east-1:312767926603:key/0a1d7811-1366-448c-a31d-ce61effcbf92 - SNSTopic1: - Type: 'AWS::SNS::Topic' diff --git a/cloud-watch-to-slack-testing/deployment/index.js b/cloud-watch-to-slack-testing/deployment/index.js index 88516c34..c5312fc9 100644 --- a/cloud-watch-to-slack-testing/deployment/index.js +++ b/cloud-watch-to-slack-testing/deployment/index.js @@ -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; @@ -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. + * @param instanceId + * @returns {Promise} + */ +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; + } catch (e) { + console.error("Error describing instance", e); + } + return null; +} + function constructMsgAndSendToSlack( slackChannel, messageText, diff --git a/cloud-watch-to-slack-testing/deployment/output-template.yaml b/cloud-watch-to-slack-testing/deployment/output-template.yaml deleted file mode 100644 index 7c9313f1..00000000 --- a/cloud-watch-to-slack-testing/deployment/output-template.yaml +++ /dev/null @@ -1,37 +0,0 @@ -AWSTemplateFormatVersion: '2010-09-09' -Description: An Amazon SNS trigger that sends CloudWatch alarm notifications to Slack. -Parameters: - KeyIdParameter: - Type: String - kmsEncryptedHookUrlParameter: - Type: String - slackChannelParameter: - Type: String -Resources: - SNSTopic1: - Type: AWS::SNS::Topic - cloudwatchalarmtoslack: - Properties: - CodeUri: s3://aws-sar-publishing/376e2767f8cf796f8f7a36e41cb5ae7e - Description: An Amazon SNS trigger that sends CloudWatch alarm notifications - to Slack. - Environment: - Variables: - kmsEncryptedHookUrl: kmsEncryptedHookUrlParameter - slackChannel: slackChannelParameter - Events: - SNS1: - Properties: - Topic: - Ref: SNSTopic1 - Type: SNS - Handler: index.handler - MemorySize: 128 - Policies: - - KMSDecryptPolicy: - KeyId: - Ref: KeyIdParameter - Runtime: nodejs8.10 - Timeout: 3 - Type: AWS::Serverless::Function -Transform: AWS::Serverless-2016-10-31 diff --git a/cloud-watch-to-slack-testing/deployment/package.json b/cloud-watch-to-slack-testing/deployment/package.json deleted file mode 100644 index 02d6a7da..00000000 --- a/cloud-watch-to-slack-testing/deployment/package.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "algorithmia-blueprint", - "version": "1.0.0", - "private": true, - "dependencies": { - "algorithmia": "^0.3.9" - } -} diff --git a/cloud-watch-to-slack-testing/deployment/template.yaml b/cloud-watch-to-slack-testing/deployment/template.yaml deleted file mode 100644 index dd316e1f..00000000 --- a/cloud-watch-to-slack-testing/deployment/template.yaml +++ /dev/null @@ -1,37 +0,0 @@ -AWSTemplateFormatVersion: '2010-09-09' -Transform: 'AWS::Serverless-2016-10-31' -Description: An Amazon SNS trigger that sends CloudWatch alarm notifications to Slack. -Parameters: - KeyIdParameter: - Type: String - slackChannelParameter: - Type: String - kmsEncryptedHookUrlParameter: - Type: String -Resources: - cloudwatchalarmtoslack: - Type: 'AWS::Serverless::Function' - Properties: - Handler: index.handler - Runtime: nodejs8.10 - CodeUri: . - Description: >- - An Amazon SNS trigger that sends CloudWatch alarm notifications to - Slack. - MemorySize: 128 - Timeout: 3 - Policies: - - KMSDecryptPolicy: - KeyId: !Ref KeyIdParameter - Events: - SNS1: - Type: SNS - Properties: - Topic: - Ref: SNSTopic1 - Environment: - Variables: - slackChannel: slackChannelParameter - kmsEncryptedHookUrl: kmsEncryptedHookUrlParameter - SNSTopic1: - Type: 'AWS::SNS::Topic' From ae77fdbc340cd52561d901b3e0b096312d92061d Mon Sep 17 00:00:00 2001 From: Charles Overbeck Date: Thu, 27 Jul 2023 10:46:35 -0700 Subject: [PATCH 2/2] PR feedback. --- cloud-watch-to-slack-testing/deployment/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloud-watch-to-slack-testing/deployment/index.js b/cloud-watch-to-slack-testing/deployment/index.js index c5312fc9..fbc84a69 100644 --- a/cloud-watch-to-slack-testing/deployment/index.js +++ b/cloud-watch-to-slack-testing/deployment/index.js @@ -52,7 +52,7 @@ function getInstanceNameAndSendMsgToSlack( } /** - * If the instanceId exists and has a tag whose key is "Name", returns that tag's value, otherwise returs null. + * If the instanceId exists and has a tag whose key is "Name", returns that tag's value, otherwise returns null. * @param instanceId * @returns {Promise} */ @@ -65,7 +65,7 @@ async function instanceName(instanceId) { 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; + return tagInstanceNameKey?.Value || null; } catch (e) { console.error("Error describing instance", e); }