Skip to content

add the function log group id as a dependency to the subscription filter #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class LogForwardingPlugin {
const filterPattern = service.custom.logForwarding.filterPattern || '';
// Get options and parameters to make resources object
const serviceName = service.service;
const awsProvider = this.serverless.getProvider('aws');
const arn = service.custom.logForwarding.destinationARN;
const stage = options.stage && options.stage.length > 0
? options.stage
Expand All @@ -74,8 +75,9 @@ class LogForwardingPlugin {
};
for (let i = 0; i < functions.length; i += 1) {
/* merge new SubscriptionFilter with current resources object */
const functionLogGroupId = awsProvider.naming.getLogGroupLogicalId(functions[i]);
const subscriptionFilter = LogForwardingPlugin.makeSubscriptionFilter(serviceName,
stage, arn, functions[i], filterPattern);
stage, arn, functions[i], filterPattern, functionLogGroupId);
_.extend(resourceObj, subscriptionFilter);
}
return resourceObj;
Expand All @@ -89,9 +91,11 @@ class LogForwardingPlugin {
* @param {String} arn arn of the lambda to forward to
* @param {String} functionName name of function to make SubscriptionFilter for
* @param {String} filterPattern filter pattern for the Subscription
* @param {Object} functionLogGroupId name of the function Log Group to add as a dependency
* @return {Object} SubscriptionFilter
*/
static makeSubscriptionFilter(serviceName, stage, arn, functionName, filterPattern) {
static makeSubscriptionFilter(serviceName, stage, arn, functionName, filterPattern,
functionLogGroupId) {
const logGroupName = `/aws/lambda/${serviceName}-${stage}-${functionName}`;
const filter = {};
filter[`SubscriptionFilter${functionName}`] = {
Expand All @@ -103,6 +107,7 @@ class LogForwardingPlugin {
},
DependsOn: [
'LogForwardingLambdaPermission',
functionLogGroupId,
],
};
return filter;
Expand Down
23 changes: 23 additions & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ const correctConfigWithStageFilter = {
filterPattern: 'Test Pattern',
stages: ['production'],
};

const awsProvider = (provider) => {
if (provider !== 'aws') {
throw new Error('provider must be aws');
}
return {
naming: {
getLogGroupLogicalId: functionName => `${functionName.charAt(0).toUpperCase()}${functionName.slice(1)}LogGroup`,
},
};
};

const constructPluginResources = (logForwarding) => {
const serverless = {
service: {
Expand Down Expand Up @@ -46,6 +58,7 @@ const constructPluginResources = (logForwarding) => {
},
service: 'test-service',
},
getProvider: awsProvider,
cli: {
log() {
},
Expand Down Expand Up @@ -74,6 +87,7 @@ const constructPluginNoResources = (logForwarding) => {
},
service: 'test-service',
},
getProvider: awsProvider,
cli: {
log() {
},
Expand Down Expand Up @@ -110,6 +124,7 @@ const constructPluginResourcesWithParam = (logForwarding) => {
},
service: 'test-service',
},
getProvider: awsProvider,
cli: {
log() {
},
Expand Down Expand Up @@ -143,6 +158,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionOneLogGroup',
],
},
SubscriptionFiltertestFunctionTwo: {
Expand All @@ -154,6 +170,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionTwoLogGroup',
],
},
},
Expand Down Expand Up @@ -185,6 +202,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionOneLogGroup',
],
},
SubscriptionFiltertestFunctionTwo: {
Expand All @@ -196,6 +214,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionTwoLogGroup',
],
},
},
Expand Down Expand Up @@ -224,6 +243,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionOneLogGroup',
],
},
SubscriptionFiltertestFunctionTwo: {
Expand All @@ -235,6 +255,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionTwoLogGroup',
],
},
},
Expand Down Expand Up @@ -266,6 +287,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionOneLogGroup',
],
},
SubscriptionFiltertestFunctionTwo: {
Expand All @@ -277,6 +299,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionTwoLogGroup',
],
},
},
Expand Down