diff --git a/__tests__/add-log-subscriptions.js b/__tests__/add-log-subscriptions.js index 0f089e4..89e1f14 100644 --- a/__tests__/add-log-subscriptions.js +++ b/__tests__/add-log-subscriptions.js @@ -829,3 +829,101 @@ test('configures api gateway log subscriptions for access logs only when executi t.is(resources.ApiGatewayExecutionLogGroupLambdaPermission, undefined); t.is(resources.ApiGatewayExecutionLogGroupSubscriptionFilter, undefined); }); + +test('configures api gateway log subscriptions for access and execution logs with RoleArn correctly', async t => { + const getNormalizedFunctionName = sinon.stub().returns('A'); + const getRestApiLogicalId = sinon.stub().returns('abcd1234'); + const getApiGatewayLogGroupLogicalId = sinon.stub().returns('ApiGatewayLogGroup'); + const generateApiGatewayDeploymentLogicalId = sinon.stub().returns('ApiGatewayDeployment1234'); + const getStackName = sinon.stub().returns('testing-cfn-stack'); + + const provider = { + naming: { + getNormalizedFunctionName, + getRestApiLogicalId, + getApiGatewayLogGroupLogicalId, + generateApiGatewayDeploymentLogicalId, + getStackName, + }, + }; + const config = { + enabled: true, + destinationArn: 'arn:aws:lambda:blah-blah-blah', + filterPattern: '{ $.level = 42 }', + apiGatewayLogs: true, + addLambdaPermission: true, + roleArn: 'arn:foo:bar:baz', + }; + const serverless = { + instanceId: '1234', + configSchemaHandler: { + defineFunctionProperties: Function.prototype, + }, + getProvider: sinon.stub().withArgs('aws').returns(provider), + service: { + provider: { + stage: 'test', + compiledCloudFormationTemplate: { + Resources: { + ApiGatewayRestApi: { + name: 'abcd', + }, + }, + }, + logs: { + restApi: true, + }, + }, + custom: { + logSubscription: [ + config + ] + }, + functions: { + A: { + name: 'a', + }, + }, + }, + }; + + const plugin = new Plugin(serverless); + + sinon.stub(plugin, 'getConfig').returns(config); + + // Return true to emulate the CFN stack already existing + sinon.stub(plugin, 'isDeployed').returns(true); + sinon.stub(plugin, 'getLogGroupName').returns('/aws/lambda/a'); + + await plugin.addLogSubscriptions(); + + const resources = serverless.service.provider.compiledCloudFormationTemplate.Resources; + + t.deepEqual(resources.ApiGatewayAccessLogGroupSubscriptionFilter0, { + Type: 'AWS::Logs::SubscriptionFilter', + Properties: { + DestinationArn: 'arn:aws:lambda:blah-blah-blah', + FilterPattern: '{ $.level = 42 }', + RoleArn: 'arn:foo:bar:baz', + LogGroupName: { + Ref: getApiGatewayLogGroupLogicalId(), + }, + }, + DependsOn: ['ApiGatewayLogGroupLambdaPermission0'], + }); + + t.deepEqual(resources.ApiGatewayExecutionLogGroupSubscriptionFilter0, { + Type: 'AWS::Logs::SubscriptionFilter', + Properties: { + DestinationArn: 'arn:aws:lambda:blah-blah-blah', + FilterPattern: '{ $.level = 42 }', + RoleArn: 'arn:foo:bar:baz', + LogGroupName: { + 'Fn::Sub': `API-Gateway-Execution-Logs_$\{${getRestApiLogicalId()}}/${ + serverless.service.provider.stage + }`, + }, + }, + DependsOn: ['ApiGatewayDeployment1234'], + }); +}); diff --git a/serverless-plugin-log-subscription.js b/serverless-plugin-log-subscription.js index 55d92c3..a76232f 100644 --- a/serverless-plugin-log-subscription.js +++ b/serverless-plugin-log-subscription.js @@ -185,6 +185,7 @@ module.exports = class LogSubscriptionsPlugin { }, }, }; + template.Resources[`ApiGatewayExecutionLogGroupLambdaPermission${suffix}`] = executionLogLambdaPermission; } @@ -204,6 +205,11 @@ module.exports = class LogSubscriptionsPlugin { }, DependsOn: template.Resources[accessLogLambdaPermResourceName] ? [accessLogLambdaPermResourceName] : [], }; + + if (config.roleArn !== undefined) { + accessLogsubscriptionFilter.Properties.RoleArn = config.roleArn; + } + template.Resources[`ApiGatewayAccessLogGroupSubscriptionFilter${suffix}`] = accessLogsubscriptionFilter; } @@ -226,6 +232,11 @@ module.exports = class LogSubscriptionsPlugin { aws.naming.generateApiGatewayDeploymentLogicalId(this.serverless.instanceId), ], }; + + if (config.roleArn !== undefined) { + executionLogsubscriptionFilter.Properties.RoleArn = config.roleArn; + } + template.Resources[`ApiGatewayExecutionLogGroupSubscriptionFilter${suffix}`] = executionLogsubscriptionFilter; }