Skip to content

Commit

Permalink
scheduled expression doesn't work with custom eventbus
Browse files Browse the repository at this point in the history
  • Loading branch information
bilindhajer committed Feb 6, 2024
1 parent 22570c4 commit e7cc5fb
Show file tree
Hide file tree
Showing 18 changed files with 27 additions and 105 deletions.
27 changes: 0 additions & 27 deletions lib/shortcuts/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,6 @@ a Lambda permission.
| --- | --- | --- | --- |
| options | <code>Object</code> | | Extends the options for [`Lambda`](#lambda) with the following additional attributes: |
| options.ScheduleExpression | <code>String</code> | | See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-scheduleexpression). |
| [options.EventBusName] | <code>String</code> | <code>&#x27;default&#x27;</code> | See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-eventbusname). |
| [options.State] | <code>String</code> | <code>&#x27;ENABLED&#x27;</code> | See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-state). |

**Example**
Expand All @@ -725,32 +724,6 @@ const lambda = new cf.shortcuts.ScheduledLambda({
ScheduleExpression: 'cron(45 * * * ? *)',
});

module.exports = cf.merge(myTemplate, lambda);
```
**Example**
```js
const cf = require('@mapbox/cloudfriend');

const myTemplate = {
...
Resources: {
MyEventBus: {
Type: 'AWS::Events::EventBus',
Properties: { ... }
}
}
};

const lambda = new cf.shortcuts.ScheduledLambda({
LogicalName: 'MyLambda',
Code: {
S3Bucket: 'my-code-bucket',
S3Key: 'path/to/code.zip'
},
EventBusName: cf.ref('MyEventBus'),
ScheduleExpression: 'cron(45 * * * ? *)',
});

module.exports = cf.merge(myTemplate, lambda);
```
<a name="ServiceRole"></a>
Expand Down
7 changes: 2 additions & 5 deletions lib/shortcuts/event-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class EventLambda extends Lambda {

const {
EventPattern,
EventBusName,
EventBusName = 'default',
State = 'ENABLED'
} = options;

Expand All @@ -93,6 +93,7 @@ class EventLambda extends Lambda {
]
},
State,
EventBusName,
EventPattern,
Targets: [
{
Expand All @@ -105,10 +106,6 @@ class EventLambda extends Lambda {
}
};

if (EventBusName) {
this.Resources[`${this.LogicalName}Trigger`].Properties.EventBusName = EventBusName;
}

this.Resources[`${this.LogicalName}Permission`] = {
Type: 'AWS::Lambda::Permission',
Condition: this.Condition,
Expand Down
32 changes: 0 additions & 32 deletions lib/shortcuts/scheduled-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const Lambda = require('./lambda');
*
* @param {Object} options - Extends the options for [`Lambda`](#lambda) with the following additional attributes:
* @param {String} options.ScheduleExpression - See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-scheduleexpression).
* @param {String} [options.EventBusName='default'] - See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-eventbusname).
* @param {String} [options.State='ENABLED'] - See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-state).
*
* @example
Expand All @@ -27,40 +26,13 @@ const Lambda = require('./lambda');
* });
*
* module.exports = cf.merge(myTemplate, lambda);
*
* @example
* const cf = require('@mapbox/cloudfriend');
*
* const myTemplate = {
* ...
* Resources: {
* MyEventBus: {
* Type: 'AWS::Events::EventBus',
* Properties: { ... }
* }
* }
* };
*
* const lambda = new cf.shortcuts.ScheduledLambda({
* LogicalName: 'MyLambda',
* Code: {
* S3Bucket: 'my-code-bucket',
* S3Key: 'path/to/code.zip'
* },
* EventBusName: cf.ref('MyEventBus'),
* ScheduleExpression: 'cron(45 * * * ? *)',
* });
*
* module.exports = cf.merge(myTemplate, lambda);
*
*/
class ScheduledLambda extends Lambda {
constructor(options) {
if (!options) throw new Error('Options required');
super(options);

const {
EventBusName,
ScheduleExpression,
State = 'ENABLED'
} = options;
Expand Down Expand Up @@ -93,10 +65,6 @@ class ScheduledLambda extends Lambda {
}
};

if (EventBusName) {
this.Resources[`${this.LogicalName}Schedule`].Properties.EventBusName = EventBusName;
}

this.Resources[`${this.LogicalName}Permission`] = {
Type: 'AWS::Lambda::Permission',
Condition: this.Condition,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mapbox/cloudfriend",
"version": "7.2.0-0",
"version": "7.2.0-1",
"description": "Helper functions for assembling CloudFormation templates in JavaScript",
"main": "index.js",
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/shortcuts/event-lambda-custom-eventbus.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
]
},
"State": "DISABLED",
"EventBusName": "my-cool-eventbus",
"EventPattern": {
"source": [
"aws.ec2"
Expand All @@ -170,8 +171,7 @@
]
}
}
],
"EventBusName": "my-cool-eventbus"
]
}
},
"MyLambdaPermission": {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/shortcuts/event-lambda-defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
]
},
"State": "ENABLED",
"EventBusName": "default",
"EventPattern": {
"source": [
"aws.ec2"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/shortcuts/event-lambda-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
]
},
"State": "DISABLED",
"EventBusName": "default",
"EventPattern": {
"source": [
"aws.ec2"
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/shortcuts/hookshot-github-secret-ref.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "PassDeployment41d722f6"
"Ref": "PassDeployment2d092515"
},
"StageName": "hookshot",
"RestApiId": {
Expand All @@ -47,7 +47,7 @@
]
}
},
"PassDeployment41d722f6": {
"PassDeployment2d092515": {
"Type": "AWS::ApiGateway::Deployment",
"DependsOn": "PassMethod",
"Properties": {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/shortcuts/hookshot-github-secret-string.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "PassDeployment41d722f6"
"Ref": "PassDeployment2d092515"
},
"StageName": "hookshot",
"RestApiId": {
Expand All @@ -43,7 +43,7 @@
]
}
},
"PassDeployment41d722f6": {
"PassDeployment2d092515": {
"Type": "AWS::ApiGateway::Deployment",
"DependsOn": "PassMethod",
"Properties": {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/shortcuts/hookshot-github.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "PassDeployment41d722f6"
"Ref": "PassDeployment2d092515"
},
"StageName": "hookshot",
"RestApiId": {
Expand All @@ -43,7 +43,7 @@
]
}
},
"PassDeployment41d722f6": {
"PassDeployment2d092515": {
"Type": "AWS::ApiGateway::Deployment",
"DependsOn": "PassMethod",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "PassDeployment41d722f6"
"Ref": "PassDeployment2d092515"
},
"StageName": "hookshot",
"RestApiId": {
Expand Down Expand Up @@ -52,7 +52,7 @@
}
}
},
"PassDeployment41d722f6": {
"PassDeployment2d092515": {
"Type": "AWS::ApiGateway::Deployment",
"DependsOn": "PassMethod",
"Properties": {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/shortcuts/hookshot-passthrough-alarms.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "PassDeployment41d722f6"
"Ref": "PassDeployment2d092515"
},
"StageName": "hookshot",
"RestApiId": {
Expand All @@ -43,7 +43,7 @@
]
}
},
"PassDeployment41d722f6": {
"PassDeployment2d092515": {
"Type": "AWS::ApiGateway::Deployment",
"DependsOn": "PassMethod",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "PassDeployment41d722f6"
"Ref": "PassDeployment2d092515"
},
"StageName": "hookshot",
"RestApiId": {
Expand All @@ -43,7 +43,7 @@
]
}
},
"PassDeployment41d722f6": {
"PassDeployment2d092515": {
"Type": "AWS::ApiGateway::Deployment",
"DependsOn": "PassMethod",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "PassDeployment41d722f6"
"Ref": "PassDeployment2d092515"
},
"StageName": "hookshot",
"RestApiId": {
Expand All @@ -43,7 +43,7 @@
]
}
},
"PassDeployment41d722f6": {
"PassDeployment2d092515": {
"Type": "AWS::ApiGateway::Deployment",
"DependsOn": "PassMethod",
"Properties": {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/shortcuts/hookshot-passthrough-logging.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "PassDeployment41d722f6"
"Ref": "PassDeployment2d092515"
},
"StageName": "hookshot",
"RestApiId": {
Expand All @@ -43,7 +43,7 @@
]
}
},
"PassDeployment41d722f6": {
"PassDeployment2d092515": {
"Type": "AWS::ApiGateway::Deployment",
"DependsOn": "PassMethod",
"Properties": {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/shortcuts/hookshot-passthrough.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "PassDeployment41d722f6"
"Ref": "PassDeployment2d092515"
},
"StageName": "hookshot",
"RestApiId": {
Expand All @@ -43,7 +43,7 @@
]
}
},
"PassDeployment41d722f6": {
"PassDeployment2d092515": {
"Type": "AWS::ApiGateway::Deployment",
"DependsOn": "PassMethod",
"Properties": {
Expand Down
18 changes: 0 additions & 18 deletions test/shortcuts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,24 +345,6 @@ test('[shortcuts] scheduled-lambda', (assert) => {
fixtures.get('scheduled-lambda-full'),
'expected resources generated without defaults'
);

lambda = new cf.shortcuts.ScheduledLambda({
LogicalName: 'MyLambda',
Code: {
S3Bucket: 'my-code-bucket',
S3Key: 'path/to/code.zip'
},
EventBusName: 'my-cool-eventbus',
ScheduleExpression: 'rate(1 hour)',
State: 'DISABLED'
});
template = cf.merge(lambda);
if (update) fixtures.update('scheduled-lambda-custom-eventbus', template);
assert.deepEqual(
noUndefined(template),
fixtures.get('scheduled-lambda-custom-eventbus'),
'expected resources generated without defaults and a custom eventbus'
);
assert.end();
});

Expand Down

0 comments on commit e7cc5fb

Please sign in to comment.