From ec3ad4c37053c44db9c171456f3ae5ed580b2918 Mon Sep 17 00:00:00 2001 From: Dan German Date: Mon, 2 Oct 2023 17:09:28 +0300 Subject: [PATCH] migrate to aws sdk for js v3 --- alerter/index.js | 12 +++++++----- alerter/package-lock.json | 13 ------------- alerter/package.json | 4 +--- subscriber/index.js | 13 ++++++------- subscriber/package.json | 3 ++- template.yaml | 6 +++--- 6 files changed, 19 insertions(+), 32 deletions(-) delete mode 100644 alerter/package-lock.json diff --git a/alerter/index.js b/alerter/index.js index 4159ed8..941d3e4 100644 --- a/alerter/index.js +++ b/alerter/index.js @@ -1,6 +1,8 @@ -const https = require('https'); -const Promise = require('bluebird'); -const zlib = Promise.promisifyAll(require('zlib')); +import https from 'https'; +import zlib from 'zlib'; +import { promisify } from 'util'; +const gunzipAsync = promisify(zlib.gunzip); + /** * * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format @@ -13,9 +15,9 @@ const zlib = Promise.promisifyAll(require('zlib')); * @returns {Object} object - API Gateway Lambda Proxy Output Format * */ -exports.lambdaHandler = async (event, context) => { +export const lambdaHandler = async (event, context) => { const payload = Buffer.from(event.awslogs.data, 'base64'); - const gunzipped = (await zlib.gunzipAsync(payload)).toString('utf8'); + const gunzipped = (await gunzipAsync(payload)).toString('utf8'); const eventDetails = JSON.parse(gunzipped); let messageArray = eventDetails.logEvents[0].message.split('\t'); let errorJSON, errorType, errorMessage; diff --git a/alerter/package-lock.json b/alerter/package-lock.json deleted file mode 100644 index 94ed991..0000000 --- a/alerter/package-lock.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "alerter", - "version": "1.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "bluebird": { - "version": "3.7.2", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", - "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" - } - } -} diff --git a/alerter/package.json b/alerter/package.json index 833142e..9f6925b 100644 --- a/alerter/package.json +++ b/alerter/package.json @@ -8,7 +8,5 @@ }, "author": "", "license": "ISC", - "dependencies": { - "bluebird": "^3.7.2" - } + "type": "module" } diff --git a/subscriber/index.js b/subscriber/index.js index d325cba..54d5af2 100644 --- a/subscriber/index.js +++ b/subscriber/index.js @@ -1,7 +1,6 @@ // Load the SDK for JavaScript -var AWS = require('aws-sdk'); - -const cloudwatchlogs = new AWS.CloudWatchLogs(); +import { CloudWatchLogsClient, DescribeSubscriptionFiltersCommand, DeleteSubscriptionFilterCommand, PutSubscriptionFilterCommand } from '@aws-sdk/client-cloudwatch-logs'; +const cloudWatchLogsClient = new CloudWatchLogsClient({}); /** * * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format @@ -14,7 +13,7 @@ const cloudwatchlogs = new AWS.CloudWatchLogs(); * @returns {Object} object - API Gateway Lambda Proxy Output Format * */ -exports.lambdaHandler = async (event, context) => { +export const lambdaHandler = async (event, context) => { console.log(JSON.stringify(event)); const detail = event.detail; const resourceSplit = event.resources[0].split(':'); @@ -38,7 +37,7 @@ exports.lambdaHandler = async (event, context) => { }; let describeSubscriptionFiltersResult; try { - describeSubscriptionFiltersResult = await cloudwatchlogs.describeSubscriptionFilters(params).promise(); + describeSubscriptionFiltersResult = await cloudWatchLogsClient.send(new DescribeSubscriptionFiltersCommand(params)); } catch (err) { console.log('Unable to describe subscription filters'); //dont throw so we can try removing the log subscription just in case it is there @@ -57,7 +56,7 @@ exports.lambdaHandler = async (event, context) => { }); if (removeParams) { try{ - await cloudwatchlogs.deleteSubscriptionFilter(removeParams).promise(); + await cloudWatchLogsClient.send(new DeleteSubscriptionFilterCommand(removeParams)); }catch(err){ console.log(err); throw err; @@ -80,7 +79,7 @@ exports.lambdaHandler = async (event, context) => { distribution: 'ByLogStream' }; try { - await cloudwatchlogs.putSubscriptionFilter(params).promise(); + await cloudWatchLogsClient.send(new PutSubscriptionFilterCommand(params)); } catch (err) { console.log(err); throw err; diff --git a/subscriber/package.json b/subscriber/package.json index fe11375..6156ffa 100644 --- a/subscriber/package.json +++ b/subscriber/package.json @@ -7,5 +7,6 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", - "license": "ISC" + "license": "ISC", + "type": "module" } diff --git a/template.yaml b/template.yaml index bd5ed8a..952bec1 100644 --- a/template.yaml +++ b/template.yaml @@ -1,7 +1,7 @@ AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: > - lambda-errors-to-slack + lambda-errors-to-slack-sdk-v3 Subscribes to tagged Lambdas in your account and forwards their errors to a specified Slack channel @@ -18,7 +18,7 @@ Resources: Properties: CodeUri: alerter/ Handler: index.lambdaHandler - Runtime: nodejs12.x + Runtime: nodejs18.x Environment: Variables: SLACK_WEBHOOK_URL: !Ref SlackWebhookURL @@ -27,7 +27,7 @@ Resources: Properties: CodeUri: subscriber/ Handler: index.lambdaHandler - Runtime: nodejs12.x + Runtime: nodejs18.x Policies: - Version: '2012-10-17' # Policy Document Statement: