mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
feat(AWS EventBridge): Support disabling a rule (#9865)
* Support being able to disable an eventbridge rule * Pass state to custom resource handler * Add docs * Add unit tests for enabling or disabling an eventBridge rule
This commit is contained in:
parent
7e9bfd63fc
commit
6193d3867e
@ -31,6 +31,22 @@ functions:
|
||||
key1: value1
|
||||
```
|
||||
|
||||
## Enabling / Disabling
|
||||
|
||||
**Note:** `eventBridge` events are enabled by default. Use `enabled: false` to disable the rule.
|
||||
|
||||
```yml
|
||||
functions:
|
||||
myFunction:
|
||||
handler: index.handler
|
||||
events:
|
||||
- eventBridge:
|
||||
enabled: false
|
||||
schedule: rate(10 minutes)
|
||||
input:
|
||||
key1: value1
|
||||
```
|
||||
|
||||
## Setting up event pattern matching
|
||||
|
||||
```yml
|
||||
|
||||
@ -49,6 +49,7 @@ async function create(event, context) {
|
||||
eventBus: EventBridgeConfig.EventBus,
|
||||
pattern: EventBridgeConfig.Pattern,
|
||||
schedule: EventBridgeConfig.Schedule,
|
||||
state: EventBridgeConfig.State,
|
||||
})
|
||||
)
|
||||
.then(() =>
|
||||
@ -76,6 +77,7 @@ async function update(event, context) {
|
||||
eventBus: EventBridgeConfig.EventBus,
|
||||
pattern: EventBridgeConfig.Pattern,
|
||||
schedule: EventBridgeConfig.Schedule,
|
||||
state: EventBridgeConfig.State,
|
||||
}).then(() =>
|
||||
updateTargetConfiguration({
|
||||
lambdaArn,
|
||||
|
||||
@ -33,7 +33,7 @@ async function deleteEventBus(config) {
|
||||
}
|
||||
|
||||
async function updateRuleConfiguration(config) {
|
||||
const { ruleName, eventBus, pattern, schedule, region } = config;
|
||||
const { ruleName, eventBus, pattern, schedule, region, state } = config;
|
||||
|
||||
const EventBusName = getEventBusName(eventBus);
|
||||
|
||||
@ -42,7 +42,7 @@ async function updateRuleConfiguration(config) {
|
||||
EventBusName,
|
||||
EventPattern: JSON.stringify(pattern),
|
||||
ScheduleExpression: schedule,
|
||||
State: 'ENABLED',
|
||||
State: state,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -57,6 +57,7 @@ class AwsCompileEventBridgeEvents {
|
||||
],
|
||||
},
|
||||
schedule: { pattern: '^(?:cron|rate)\\(.+\\)$' },
|
||||
enabled: { type: 'boolean' },
|
||||
pattern: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
@ -121,6 +122,11 @@ class AwsCompileEventBridgeEvents {
|
||||
index: idx,
|
||||
});
|
||||
|
||||
let State = 'ENABLED';
|
||||
if (event.eventBridge.enabled === false) {
|
||||
State = 'DISABLED';
|
||||
}
|
||||
|
||||
if ([Input, InputPath, InputTransformer].filter(Boolean).length > 1) {
|
||||
throw new ServerlessError(
|
||||
[
|
||||
@ -147,6 +153,7 @@ class AwsCompileEventBridgeEvents {
|
||||
compiledCloudFormationTemplate,
|
||||
functionName,
|
||||
RuleName,
|
||||
State,
|
||||
Input,
|
||||
InputPath,
|
||||
InputTransformer,
|
||||
@ -167,6 +174,7 @@ class AwsCompileEventBridgeEvents {
|
||||
compiledCloudFormationTemplate,
|
||||
functionName,
|
||||
RuleName,
|
||||
State,
|
||||
Input,
|
||||
InputPath,
|
||||
InputTransformer,
|
||||
@ -196,6 +204,7 @@ class AwsCompileEventBridgeEvents {
|
||||
compiledCloudFormationTemplate,
|
||||
functionName,
|
||||
RuleName,
|
||||
State,
|
||||
Input,
|
||||
InputPath,
|
||||
InputTransformer,
|
||||
@ -235,6 +244,7 @@ class AwsCompileEventBridgeEvents {
|
||||
FunctionName,
|
||||
EventBridgeConfig: {
|
||||
RuleName,
|
||||
State,
|
||||
EventBus,
|
||||
Schedule,
|
||||
Pattern,
|
||||
@ -280,6 +290,7 @@ class AwsCompileEventBridgeEvents {
|
||||
compiledCloudFormationTemplate,
|
||||
functionName,
|
||||
RuleName,
|
||||
State,
|
||||
Input,
|
||||
InputPath,
|
||||
InputTransformer,
|
||||
@ -341,7 +352,7 @@ class AwsCompileEventBridgeEvents {
|
||||
EventPattern: JSON.stringify(Pattern),
|
||||
Name: RuleName,
|
||||
ScheduleExpression: Schedule,
|
||||
State: 'ENABLED',
|
||||
State,
|
||||
Targets: [target],
|
||||
},
|
||||
};
|
||||
|
||||
@ -127,6 +127,30 @@ const serverlessConfigurationExtension = {
|
||||
},
|
||||
],
|
||||
},
|
||||
disabled: {
|
||||
handler: 'index.handler',
|
||||
events: [
|
||||
{
|
||||
eventBridge: {
|
||||
eventBus: 'arn:aws:events:us-east-1:12345:event-bus/default',
|
||||
schedule: 'rate(10 minutes)',
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
enabled: {
|
||||
handler: 'index.handler',
|
||||
events: [
|
||||
{
|
||||
eventBridge: {
|
||||
eventBus: 'arn:aws:events:us-east-1:12345:event-bus/default',
|
||||
schedule: 'rate(10 minutes)',
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@ -193,6 +217,21 @@ describe('EventBridgeEvents', () => {
|
||||
expect(eventBridgeConfig.RuleName).to.include('dev-default-rule-1');
|
||||
});
|
||||
|
||||
it('should ensure state is enabled by default', () => {
|
||||
const eventBridgeConfig = getEventBridgeConfigById('default');
|
||||
expect(eventBridgeConfig.State).to.be.eq('ENABLED');
|
||||
});
|
||||
|
||||
it('should ensure state is enabled when explicity set', () => {
|
||||
const eventBridgeConfig = getEventBridgeConfigById('enabled');
|
||||
expect(eventBridgeConfig.State).to.be.eq('ENABLED');
|
||||
});
|
||||
|
||||
it('should ensure state is disabled when explicity set', () => {
|
||||
const eventBridgeConfig = getEventBridgeConfigById('disabled');
|
||||
expect(eventBridgeConfig.State).to.be.eq('DISABLED');
|
||||
});
|
||||
|
||||
it("should ensure rule name doesn't exceed 64 chars", () => {
|
||||
const eventBridgeConfig = getEventBridgeConfigById(NAME_OVER_64_CHARS);
|
||||
expect(eventBridgeConfig.RuleName.endsWith('rule-1')).to.be.true;
|
||||
@ -273,6 +312,8 @@ describe('EventBridgeEvents', () => {
|
||||
let ruleTarget;
|
||||
let inputPathRuleTarget;
|
||||
let inputTransformerRuleTarget;
|
||||
let disabledRuleResource;
|
||||
let enabledRuleResource;
|
||||
const schedule = 'rate(10 minutes)';
|
||||
const eventBusName = 'nondefault';
|
||||
const pattern = {
|
||||
@ -328,6 +369,22 @@ describe('EventBridgeEvents', () => {
|
||||
inputTransformer,
|
||||
},
|
||||
},
|
||||
{
|
||||
eventBridge: {
|
||||
eventBus: eventBusName,
|
||||
schedule,
|
||||
enabled: false,
|
||||
pattern,
|
||||
},
|
||||
},
|
||||
{
|
||||
eventBridge: {
|
||||
eventBus: eventBusName,
|
||||
schedule,
|
||||
enabled: true,
|
||||
pattern,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
@ -352,6 +409,16 @@ describe('EventBridgeEvents', () => {
|
||||
resource.Type === 'AWS::Events::Rule' && resource.Properties.Name.endsWith('3')
|
||||
);
|
||||
inputTransformerRuleTarget = inputTransformerRuleResource.Properties.Targets[0];
|
||||
|
||||
disabledRuleResource = Object.values(cfResources).find(
|
||||
(resource) =>
|
||||
resource.Type === 'AWS::Events::Rule' && resource.Properties.Name.endsWith('4')
|
||||
);
|
||||
|
||||
enabledRuleResource = Object.values(cfResources).find(
|
||||
(resource) =>
|
||||
resource.Type === 'AWS::Events::Rule' && resource.Properties.Name.endsWith('5')
|
||||
);
|
||||
});
|
||||
|
||||
it('should create an EventBus resource', () => {
|
||||
@ -362,6 +429,18 @@ describe('EventBridgeEvents', () => {
|
||||
expect(ruleResource.Properties.ScheduleExpression).to.equal('rate(10 minutes)');
|
||||
});
|
||||
|
||||
it('should correctly set State by default on a created rule', () => {
|
||||
expect(ruleResource.Properties.State).to.equal('ENABLED');
|
||||
});
|
||||
|
||||
it('should correctly set State when disabled on a created rule', () => {
|
||||
expect(disabledRuleResource.Properties.State).to.equal('DISABLED');
|
||||
});
|
||||
|
||||
it('should correctly set State when enabled on a created rule', () => {
|
||||
expect(enabledRuleResource.Properties.State).to.equal('ENABLED');
|
||||
});
|
||||
|
||||
it('should correctly set EventPattern on a created rule', () => {
|
||||
expect(ruleResource.Properties.EventPattern).to.deep.equal(JSON.stringify(pattern));
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user