Skip to content

Commit

Permalink
Merge pull request #172 from SumoLogic/python-js-update
Browse files Browse the repository at this point in the history
Updating runtimes of python and nodeJS
  • Loading branch information
himsharma01 authored Oct 25, 2023
2 parents 7412937 + 57d5272 commit 23cbb63
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 266 deletions.
14 changes: 7 additions & 7 deletions awsautoenableS3Logging/packaged.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ Metadata:
- s3logging
- flowlogs
Name: sumologic-s3-logging-auto-enable
SemanticVersion: 1.0.5
SemanticVersion: 1.0.6
SourceCodeUrl: https://github.com/SumoLogic/sumologic-aws-lambda/tree/master/awsautoenableS3Logging
LicenseUrl: s3://appdevstore/AutoEnableS3Logs/v1.0.5/978602b5b9ec16f8bab0e38fd6b3998f
ReadmeUrl: s3://appdevstore/AutoEnableS3Logs/v1.0.5/d05d411471e0bb4db3389f2523f515f0
LicenseUrl: s3://appdevstore/AutoEnableS3Logs/v1.0.6/978602b5b9ec16f8bab0e38fd6b3998f
ReadmeUrl: s3://appdevstore/AutoEnableS3Logs/v1.0.6/d05d411471e0bb4db3389f2523f515f0
SpdxLicenseId: Apache-2.0
Mappings:
Region2ELBAccountId:
Expand Down Expand Up @@ -221,9 +221,9 @@ Resources:
Type: AWS::Serverless::Function
Condition: auto_enable_new
Properties:
CodeUri: s3://appdevstore/sumo_app_utils/v2.0.10/sumo_app_utils.zip
CodeUri: s3://appdevstore/sumo_app_utils/v2.0.11/sumo_app_utils.zip
Handler: awsresource.enable_s3_logs
Runtime: python3.7
Runtime: python3.11
Role:
Fn::GetAtt:
- SumoLambdaRole
Expand Down Expand Up @@ -458,8 +458,8 @@ Resources:
Condition: auto_enable_existing
Properties:
Handler: main.handler
Runtime: python3.7
CodeUri: s3://appdevstore/sumo_app_utils/v2.0.10/sumo_app_utils.zip
Runtime: python3.11
CodeUri: s3://appdevstore/sumo_app_utils/v2.0.11/sumo_app_utils.zip
MemorySize: 128
Timeout: 900
Role:
Expand Down
10 changes: 5 additions & 5 deletions awsautoenableS3Logging/sumologic-s3-logging-auto-enable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Metadata:
- s3logging
- flowlogs
Name: sumologic-s3-logging-auto-enable
SemanticVersion: 1.0.5
SemanticVersion: 1.0.6
SourceCodeUrl: https://github.com/SumoLogic/sumologic-aws-lambda/tree/master/awsautoenableS3Logging
LicenseUrl: ./LICENSE
ReadmeUrl: ./README.md
Expand Down Expand Up @@ -204,9 +204,9 @@ Resources:
Type: 'AWS::Serverless::Function'
Condition: auto_enable_new
Properties:
CodeUri: s3://appdevstore/sumo_app_utils/v2.0.10/sumo_app_utils.zip
CodeUri: s3://appdevstore/sumo_app_utils/v2.0.11/sumo_app_utils.zip
Handler: "awsresource.enable_s3_logs"
Runtime: python3.7
Runtime: python3.11
Role: !GetAtt SumoLambdaRole.Arn
Description: "Lambda Function for auto enable s3 logs for AWS Resources."
MemorySize: 128
Expand Down Expand Up @@ -380,8 +380,8 @@ Resources:
Condition: auto_enable_existing
Properties:
Handler: main.handler
Runtime: python3.7
CodeUri: s3://appdevstore/sumo_app_utils/v2.0.10/sumo_app_utils.zip
Runtime: python3.11
CodeUri: s3://appdevstore/sumo_app_utils/v2.0.11/sumo_app_utils.zip
MemorySize: 128
Timeout: 900
Role:
Expand Down
8 changes: 4 additions & 4 deletions cloudwatchlogs-with-dlq/DLQLambdaCloudFormation.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
"FunctionName": { "Fn::Join": [ "-", [ "SumoCWLogsLambda", { "Fn::Select" : [ "2", {"Fn::Split" : [ "/" , { "Ref": "AWS::StackId" } ]}] } ] ] },
"Code": {
"S3Bucket": { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "bucketname"]},
"S3Key": "cloudwatchlogs-with-dlq.zip"
"S3Key": "cloudwatchLogsDLQ/v1.2.0/cloudwatchlogs-with-dlq.zip"
},
"Role": {
"Fn::GetAtt": [
Expand All @@ -228,7 +228,7 @@
}
},
"Handler": "cloudwatchlogs_lambda.handler",
"Runtime": "nodejs16.x",
"Runtime": "nodejs18.x",
"MemorySize": 128,
"Environment": {
"Variables": {
Expand Down Expand Up @@ -272,7 +272,7 @@
"FunctionName": { "Fn::Join": [ "-", [ "SumoCWProcessDLQLambda", { "Fn::Select" : [ "2", {"Fn::Split" : [ "/" , { "Ref": "AWS::StackId" } ]}] } ] ] },
"Code": {
"S3Bucket": { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "bucketname"]},
"S3Key": "cloudwatchlogs-with-dlq.zip"
"S3Key": "cloudwatchLogsDLQ/v1.2.0/cloudwatchlogs-with-dlq.zip"
},
"Role": {
"Fn::GetAtt": [
Expand All @@ -290,7 +290,7 @@
]
}
},
"Runtime": "nodejs16.x",
"Runtime": "nodejs18.x",
"MemorySize": 128,
"Environment": {
"Variables": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
"FunctionName": { "Fn::Join": [ "-", [ "SumoCWLogsLambda", { "Fn::Select" : [ "2", {"Fn::Split" : [ "/" , { "Ref": "AWS::StackId" } ]}] } ] ] },
"Code": {
"S3Bucket": { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "bucketname"]},
"S3Key": "cloudwatchlogs-with-dlq.zip"
"S3Key": "cloudwatchLogsDLQ/v1.2.0/cloudwatchlogs-with-dlq.zip"
},
"Role": {
"Fn::GetAtt": [
Expand All @@ -270,7 +270,7 @@
}
},
"Handler": "cloudwatchlogs_lambda.handler",
"Runtime": "nodejs16.x",
"Runtime": "nodejs18.x",
"MemorySize": 128,
"Environment": {
"Variables": {
Expand Down Expand Up @@ -313,7 +313,7 @@
"FunctionName": { "Fn::Join": [ "-", [ "SumoCWProcessDLQLambda", { "Fn::Select" : [ "2", {"Fn::Split" : [ "/" , { "Ref": "AWS::StackId" } ]}] } ] ] },
"Code": {
"S3Bucket": { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "bucketname"]},
"S3Key": "cloudwatchlogs-with-dlq.zip"
"S3Key": "cloudwatchLogsDLQ/v1.2.0/cloudwatchlogs-with-dlq.zip"
},
"Role": {
"Fn::GetAtt": [
Expand All @@ -331,7 +331,7 @@
]
}
},
"Runtime": "nodejs16.x",
"Runtime": "nodejs18.x",
"MemorySize": 128,
"Environment": {
"Variables": {
Expand Down
93 changes: 49 additions & 44 deletions cloudwatchlogs-with-dlq/DLQProcessor.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,82 @@
var AWS = require("aws-sdk");
var processLogsHandler = require('./cloudwatchlogs_lambda').processLogs;
var getEndpointURL = require('./cloudwatchlogs_lambda').getEndpointURL;
var DLQUtils = require("./sumo-dlq-function-utils").DLQUtils;
var Messages = DLQUtils.Messages;
var invokeLambdas = DLQUtils.invokeLambdas;
const { processLogs: processLogsHandler, getEndpointURL } = require('./cloudwatchlogs_lambda');
const { DLQUtils } = require("./sumo-dlq-function-utils");

const { Messages, invokeLambdas } = DLQUtils;

exports.consumeMessages = async function (env, context, callback) {
var sqs = new AWS.SQS({region: env.AWS_REGION});
var MessagesObj = new Messages(env);
env.SUMO_CLIENT_HEADER="dlq-aws-lambda";
const MessagesObj = new Messages(env);
env.SUMO_CLIENT_HEADER = "dlq-aws-lambda";

if (!env.SUMO_ENDPOINT) {
let SUMO_ENDPOINT = await getEndpointURL();
if (SUMO_ENDPOINT instanceof Error) {
console.log("Error in getEndpointURL: ", SUMO_ENDPOINT);
callback(SUMO_ENDPOINT, null);
try {
let SUMO_ENDPOINT = await getEndpointURL();
env.SUMO_ENDPOINT = SUMO_ENDPOINT;
} catch (error) {
console.log("Error in getEndpointURL: ", error);
callback(error, null);
return;
}
env.SUMO_ENDPOINT = SUMO_ENDPOINT;
} else {
console.log("consumeMessages: Getting SUMO_ENDPOINT from env");
}
MessagesObj.receiveMessages(10, function (err, data) {
var messages = (data)? data.Messages: null;
if (err) {
callback(err);
} else if (messages && messages.length > 0) {
var fail_cnt = 0, msgCount = 0;

try {
const messages = await MessagesObj.receiveMessages(10);


if (messages && messages.length > 0) {
let fail_cnt = 0, msgCount = 0;
console.log("Messages Received", messages.length);
for (var i = 0; i < messages.length; i++) {
(function(idx) {
var payload = JSON.parse(messages[idx].Body);
var receiptHandle = messages[idx].ReceiptHandle;

for (let i = 0; i < messages.length; i++) {
(function (idx) {
const payload = JSON.parse(messages[idx].Body);
const receiptHandle = messages[idx].ReceiptHandle;

if (!(payload.awslogs && payload.awslogs.data)) {
console.log("Message does not contain awslogs or awslogs.data attributes", payload);
//deleting msg in DLQ after injesting in sumo
MessagesObj.deleteMessage(receiptHandle, function (err, data) {
if (err) console.log(err, err.stack);
});

MessagesObj.deleteMessage(receiptHandle)
.catch((err) => console.log(err, err.stack));

return;
}
var logdata = payload.awslogs.data;

const logdata = payload.awslogs.data;

processLogsHandler(env, logdata, function (err, msg) {
msgCount++;

if (err) {
console.log(err, msg);
fail_cnt++;
} else {
//deleting msg in DLQ after injesting in sumo
MessagesObj.deleteMessage(receiptHandle, function (err, data) {
if (err) console.log(err, err.stack);
});
MessagesObj.deleteMessage(receiptHandle)
.catch((err) => console.log(err, err.stack));
}
if (msgCount == messages.length) {
if (fail_cnt == 0 && (parseInt(env.is_worker) === 0)) {

if (msgCount === messages.length) {
if (fail_cnt === 0 && parseInt(env.is_worker) === 0) {
invokeLambdas(env.AWS_REGION, parseInt(env.NUM_OF_WORKERS),
context.functionName, '{"is_worker": "1"}', context);
context.functionName, '{"is_worker": "1"}', context);
}
callback(null, (messages.length-fail_cnt) + ' success');

callback(null, `${messages.length - fail_cnt} success`);
}
});
})(i);
}

} else {

callback(null, 'success');
}
});
} catch (error) {
callback(error);
}
};

exports.handler = function (event, context, callback) {

var env = Object.assign({}, process.env);
env['is_worker'] = event.is_worker || 0;
const env = Object.assign({}, process.env);
env.is_worker = event.is_worker || 0;
exports.consumeMessages(env, context, callback);
};

};
Binary file not shown.
40 changes: 18 additions & 22 deletions cloudwatchlogs-with-dlq/cloudwatchlogs_lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,25 @@ var url = require('url');
var vpcutils = require('./vpcutils');
var SumoLogsClient = require('./sumo-dlq-function-utils').SumoLogsClient;
var Utils = require('./sumo-dlq-function-utils').Utils;
const AWS = require('aws-sdk');
const ssm = new AWS.SSM();

const { SSMClient, GetParameterCommand } = require("@aws-sdk/client-ssm");

exports.getEndpointURL = async function() {
console.log('Getting SUMO_ENDPOINT from AWS SSM Parameter Store');
return new Promise((resolve, reject) => {
ssm.getParameter(
{
Name: 'SUMO_ENDPOINT',
WithDecryption: true
},
(err, data) => {
if (err) {
console.log(err, err.stack);
reject(new Error('Unable to get EndpointURL from SSM: ' + err));
} else {
// console.log(data);
resolve(data.Parameter.Value);
}
}
);
});
}
console.log('Getting SUMO_ENDPOINT from AWS SSM Parameter Store');
const ssmClient = new SSMClient();
try {
const data = await ssmClient.send(
new GetParameterCommand({
Name: 'SUMO_ENDPOINT',
WithDecryption: true
})
);
return data.Parameter.Value;
} catch (error) {
console.error('Unable to get EndpointURL from SSM:', error);
throw new Error('Unable to get EndpointURL from SSM: ' + error);
}
}

function createRecords(config, events, awslogsData) {
var records = [];
Expand Down Expand Up @@ -210,4 +206,4 @@ exports.handler = function (event, context, callback) {

exports.processLogs(process.env, event.awslogs.data, callback);

};
};
Loading

0 comments on commit 23cbb63

Please sign in to comment.