Skip to content

Commit 6de624b

Browse files
authored
Merge pull request #121 from SumoLogic/hpal_auto_tag
SUMO-140669: Added tag filtering
2 parents cf70bcc + 62c6daf commit 6de624b

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

loggroup-lambda-connector/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Made with ❤️ by Sumo Logic. Available on the [AWS Serverless Application Rep
1212
LambdaARN: "Enter ARN for target lambda function" All loggroups matching the pattern are subscribed to this function
1313
LogGroupPattern: "Enter regex for matching logGroups"
1414
UseExistingLogs: "Select true for subscribing existing logs"
15+
LogGroupTags: "Enter comma separated keyvalue pairs for filtering logGroups using tags. Ex KeyName1=string,KeyName2=string. Supported only when UseExistingLogs is set to false."
1516
6. Click Deploy.
1617

1718

@@ -43,6 +44,8 @@ It has two environment variables
4344

4445
**USE_EXISTING_LOGS**: This is used for subscribing existing log groups. By setting this parameter to true and invoking the function manually, all the existing log groups matching the pattern will be subscribed to lambda function with `LAMBDA_ARN` as arn
4546

47+
**LogGroupTags**: This is used for filtering out loggroups based on tags.Only loggroups which match any one of the key value pairs will be subscribed to the lambda function. This works only for new loggroups not existing loggroups.
48+
4649
### For Developers
4750

4851
Installing Dependencies

loggroup-lambda-connector/sam/packaged.yaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ Metadata:
2121
- serverless
2222
- loggroups
2323
- cloudwatch
24-
LicenseUrl: s3://appdevstore/LoggroupConnector/v1.0.2/5122657d5b9a0d3713e24d3a33eae431
24+
LicenseUrl: s3://appdevstore/LoggroupConnector/v1.0.3/5122657d5b9a0d3713e24d3a33eae431
2525
Name: sumologic-loggroup-connector
26-
ReadmeUrl: s3://appdevstore/LoggroupConnector/v1.0.2/0490ecc943c35a5d04b00813554c06ad
27-
SemanticVersion: 1.0.2
26+
ReadmeUrl: s3://appdevstore/LoggroupConnector/v1.0.3/c98f2c0d986a3f55fbea539688ee6a2e
27+
SemanticVersion: 1.0.3
2828
SourceCodeUrl: https://github.com/SumoLogic/sumologic-aws-lambda/loggroup-lambda-connector
2929
SpdxLicenseId: Apache-2.0
3030
Parameters:
@@ -43,11 +43,15 @@ Parameters:
4343
- 'true'
4444
- 'false'
4545
Description: Select true for subscribing existing logs
46+
LogGroupTags:
47+
Type: CommaDelimitedList
48+
Description: 'Enter comma separated keyvalue pairs for filtering logGroups using
49+
tags. Ex KeyName1=string,KeyName2=string '
4650
Resources:
4751
SumoLogGroupLambdaConnector:
4852
Type: AWS::Serverless::Function
4953
Properties:
50-
CodeUri: s3://appdevstore/LoggroupConnector/v1.0.2/06605e5d4a1e561027da885250b6d539
54+
CodeUri: s3://appdevstore/LoggroupConnector/v1.0.3/fef3de66919bef9861be16f25aec0343
5155
Handler: loggroup-lambda-connector.handler
5256
Runtime: nodejs10.x
5357
Environment:
@@ -58,6 +62,10 @@ Resources:
5862
Ref: LogGroupPattern
5963
USE_EXISTING_LOG_GROUPS:
6064
Ref: UseExistingLogs
65+
LOG_GROUP_TAGS:
66+
Fn::Join:
67+
- ','
68+
- Ref: LogGroupTags
6169
Policies:
6270
- Statement:
6371
- Sid: ReadWriteFilterPolicy

loggroup-lambda-connector/sam/sam_package.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ else
88
SAM_S3_BUCKET="cf-templates-5d0x5unchag-us-east-2"
99
AWS_REGION="us-east-2"
1010
fi
11-
sam package --template-file template.yaml --s3-bucket $SAM_S3_BUCKET --output-template-file packaged.yaml --s3-prefix LoggroupConnector/v1.0.2
1211

13-
# sam deploy --template-file packaged.yaml --stack-name testingloggrpconnector --capabilities CAPABILITY_IAM --region $AWS_REGION --parameter-overrides LambdaARN="arn:aws:lambda:us-east-1:956882708938:function:AccessVPCResourcesLambda"
12+
version="1.0.3"
1413

15-
sam publish --template packaged.yaml --region us-east-1
14+
sam package --template-file template.yaml --s3-bucket $SAM_S3_BUCKET --output-template-file packaged.yaml --s3-prefix "LoggroupConnector/v$version"
15+
16+
sam deploy --template-file packaged.yaml --stack-name testingloggrpconnector --capabilities CAPABILITY_IAM --region $AWS_REGION --parameter-overrides LambdaARN="arn:aws:lambda:us-east-1:956882708938:function:SumoCWLogsLambda" LogGroupTags="env=prod,name=apiassembly" LogGroupPattern="test"
17+
18+
# sam publish --template packaged.yaml --region $AWS_REGION --semantic-version $version
1619
# aws cloudformation describe-stack-events --stack-name testingloggrpconnector --region $AWS_REGION
1720
# aws cloudformation get-template --stack-name testingloggrpconnector --region $AWS_REGION
1821

loggroup-lambda-connector/sam/template.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Metadata:
2424
LicenseUrl: ./LICENSE
2525
Name: sumologic-loggroup-connector
2626
ReadmeUrl: ../README.md
27-
SemanticVersion: 1.0.2
27+
SemanticVersion: 1.0.3
2828
SourceCodeUrl: https://github.com/SumoLogic/sumologic-aws-lambda/loggroup-lambda-connector
2929
SpdxLicenseId: Apache-2.0
3030

@@ -45,6 +45,10 @@ Parameters:
4545
AllowedValues : ["true", "false"]
4646
Description: "Select true for subscribing existing logs"
4747

48+
LogGroupTags:
49+
Type: CommaDelimitedList
50+
Description: "Enter comma separated keyvalue pairs for filtering logGroups using tags. Ex KeyName1=string,KeyName2=string "
51+
4852
Resources:
4953

5054
SumoLogGroupLambdaConnector:
@@ -58,6 +62,7 @@ Resources:
5862
LAMBDA_ARN: !Ref "LambdaARN"
5963
LOG_GROUP_PATTERN: !Ref "LogGroupPattern"
6064
USE_EXISTING_LOG_GROUPS: !Ref "UseExistingLogs"
65+
LOG_GROUP_TAGS: !Join [",", {"Ref": "LogGroupTags"} ]
6166
Policies:
6267
- Statement:
6368
- Sid: ReadWriteFilterPolicy

loggroup-lambda-connector/src/loggroup-lambda-connector.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,25 @@ function subscribeToLambda(lambdaLogGroupName, lambdaArn, errorHandler) {
1818

1919
function filterLogGroups(event, logGroupRegex) {
2020
logGroupRegex = new RegExp(logGroupRegex, "i");
21-
var logGroupName = event.detail.requestParameters.logGroupName;
21+
let logGroupName = event.detail.requestParameters.logGroupName;
2222
if (logGroupName.match(logGroupRegex) && event.detail.eventName === "CreateLogGroup") {
2323
return true;
24-
} else {
25-
return false;
2624
}
25+
let lg_tags = event.detail.requestParameters.tags;
26+
if (process.env.LOG_GROUP_TAGS && lg_tags) {
27+
console.log("tags in loggroup: ", lg_tags);
28+
var tags_array = process.env.LOG_GROUP_TAGS.split(",");
29+
let tag, key, value;
30+
for (let i = 0; i < tags_array.length; i++) {
31+
tag = tags_array[i].split("=");
32+
key = tag[0].trim();
33+
value = tag[1].trim();
34+
if (lg_tags[key] && lg_tags[key]==value) {
35+
return true;
36+
}
37+
}
38+
}
39+
return false;
2740
}
2841

2942
async function subscribeExistingLogGroups(logGroups) {

0 commit comments

Comments
 (0)