Skip to content

Commit

Permalink
fix(AWS EventBridge): Fix attaching lambdas to "default" stage (#7995)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdgeOfCat committed Jul 30, 2020
1 parent 15fae3b commit b53f080
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/plugins/aws/package/compile/events/eventBridge/index.js
Expand Up @@ -147,6 +147,10 @@ class AwsCompileEventBridgeEvents {
],
],
};
const fullRuleName =
eventBusName === 'default'
? `rule/${RuleName}`
: `rule/${eventBusName}/${RuleName}`;
ruleResource = {
'Fn::Join': [
':',
Expand All @@ -156,12 +160,12 @@ class AwsCompileEventBridgeEvents {
'events',
{ Ref: 'AWS::Region' },
{ Ref: 'AWS::AccountId' },
`rule/${eventBusName}/${RuleName}`,
fullRuleName,
],
],
};

if (!eventBusIamRoleStatements.has(eventBusName)) {
if (!eventBusIamRoleStatements.has(eventBusName) && eventBusName !== 'default') {
eventBusIamRoleStatements.set(eventBusName, {
Effect: 'Allow',
Resource: eventBusResource,
Expand Down
71 changes: 71 additions & 0 deletions lib/plugins/aws/package/compile/events/eventBridge/index.test.js
Expand Up @@ -4,9 +4,13 @@

const sinon = require('sinon');
const chai = require('chai');
const BbPromise = require('bluebird');
const childProcess = BbPromise.promisifyAll(require('child_process'));
const proxyquire = require('proxyquire').noCallThru();
const AwsProvider = require('../../../../provider/awsProvider');
const Serverless = require('../../../../../../Serverless');
const runServerless = require('../../../../../../../tests/utils/run-serverless');
const fixtures = require('../../../../../../../tests/fixtures');

const { expect } = chai;
chai.use(require('sinon-chai'));
Expand Down Expand Up @@ -1334,3 +1338,70 @@ describe('AwsCompileEventBridgeEvents', () => {
});
});
});

describe('EventBridgeEvents', () => {
after(() => {
sinon.restore();
return fixtures.cleanup();
});

describe('when using the default event bus arn', () => {
const functionName = 'foo';
let cfResources;
let naming;

before(() =>
fixtures
.extend('function', {
functions: {
[functionName]: {
events: [
{
eventBridge: {
eventBus: 'arn:aws:events:us-east-1:12345:event-bus/default',
schedule: 'rate(10 minutes)',
},
},
],
},
},
})
.then(fixturePath => {
// Prevent `npm install` in custom resource setup
sinon.stub(childProcess, 'execAsync');
// Stubbing removes bluebird marker, which will result in bluebird crash on repeated
// promisification attempton. Ensure blubird marker to prevent that
childProcess.execAsync.__isPromisified__ = true;
return runServerless({
cwd: fixturePath,
cliArgs: ['package'],
});
})
.then(({ cfTemplate, awsNaming }) => {
({ Resources: cfResources } = cfTemplate);
naming = awsNaming;
})
);

it('should create the correct execution role', () => {
const roleId = naming.getCustomResourcesRoleLogicalId();
const executionRolePolicyStatements =
cfResources[roleId].Properties.Policies[0].PolicyDocument.Statement;
expect(executionRolePolicyStatements[0].Action).to.include('events:PutRule');
expect(executionRolePolicyStatements[0].Resource['Fn::Join'][1]).to.deep.include(
`rule/service-dev-${functionName}-rule-1`
);
});

it('should create the necessary resources', () => {
const normalizedFunctionName = naming.getNormalizedFunctionName(functionName);
const eventBridgeId = naming.getCustomResourceEventBridgeResourceLogicalId(
normalizedFunctionName,
1
);
expect(cfResources[eventBridgeId].Properties.EventBridgeConfig.RuleName).to.equal(
`service-dev-${functionName}-rule-1`
);
});
});
});
8 changes: 7 additions & 1 deletion tests/integration-all/event-bridge/service/core.js
Expand Up @@ -10,6 +10,12 @@ function eventBusDefault(event, context, callback) {
return callback(null, event);
}

function eventBusDefaultArn(event, context, callback) {
const functionName = 'eventBusDefaultArn';
log(functionName, JSON.stringify(event));
return callback(null, event);
}

function eventBusCustom(event, context, callback) {
const functionName = 'eventBusCustom';
log(functionName, JSON.stringify(event));
Expand All @@ -22,4 +28,4 @@ function eventBusArn(event, context, callback) {
return callback(null, event);
}

module.exports = { eventBusDefault, eventBusCustom, eventBusArn };
module.exports = { eventBusDefault, eventBusDefaultArn, eventBusCustom, eventBusArn };
8 changes: 8 additions & 0 deletions tests/integration-all/event-bridge/service/serverless.yml
Expand Up @@ -13,6 +13,14 @@ functions:
pattern:
source:
- serverless.test
eventBusDefaultArn:
handler: core.eventBusDefaultArn
events:
- eventBridge:
eventBus: CHANGE_TO_UNIQUE_PER_RUN
pattern:
source:
- serverless.test
eventBusCustom:
handler: core.eventBusCustom
events:
Expand Down
12 changes: 11 additions & 1 deletion tests/integration-all/event-bridge/tests.js
Expand Up @@ -5,7 +5,12 @@ const { expect } = require('chai');

const { getTmpDirPath, readYamlFile, writeYamlFile } = require('../../utils/fs');
const { confirmCloudWatchLogs } = require('../../utils/misc');
const { createEventBus, putEvents, deleteEventBus } = require('../../utils/eventBridge');
const {
createEventBus,
putEvents,
deleteEventBus,
describeEventBus,
} = require('../../utils/eventBridge');

const { createTestService, deployService, removeService } = require('../../utils/integration');
const { getMarkers } = require('../shared/utils');
Expand All @@ -31,6 +36,10 @@ describe('AWS - Event Bridge Integration Test', function() {
before(async () => {
tmpDirPath = getTmpDirPath();
console.info(`Temporary path: ${tmpDirPath}`);

// get default event bus ARN
const defaultEventBusArn = (await describeEventBus('default')).Arn;

const serverlessConfig = await createTestService(tmpDirPath, {
templateDir: path.join(__dirname, 'service'),
filesToAdd: [path.join(__dirname, '..', 'shared')],
Expand All @@ -40,6 +49,7 @@ describe('AWS - Event Bridge Integration Test', function() {
namedEventBusName = `${config.service}-named-event-bus`;
arnEventBusName = `${config.service}-arn-event-bus`;
config.functions.eventBusCustom.events[0].eventBridge.eventBus = namedEventBusName;
config.functions.eventBusDefaultArn.events[0].eventBridge.eventBus = defaultEventBusArn;
},
});

Expand Down
5 changes: 5 additions & 0 deletions tests/utils/eventBridge/index.js
Expand Up @@ -10,6 +10,10 @@ function deleteEventBus(name) {
return awsRequest('EventBridge', 'deleteEventBus', { Name: name });
}

function describeEventBus(name) {
return awsRequest('EventBridge', 'describeEventBus', { Name: name });
}

function putEvents(EventBusName, Entries) {
Entries.map(entry => (entry.EventBusName = EventBusName));
const params = {
Expand All @@ -21,5 +25,6 @@ function putEvents(EventBusName, Entries) {
module.exports = {
createEventBus,
deleteEventBus,
describeEventBus,
putEvents,
};

0 comments on commit b53f080

Please sign in to comment.