Skip to content
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

Shortcut bugfixes #34

Merged
merged 6 commits into from
Aug 8, 2018
Merged
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
19 changes: 10 additions & 9 deletions lib/shortcuts/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ LogGroup, a Role, an Alarm on function errors, and the Lambda Function itself.
- `options.Handler` **[String][23]** See [AWS documentation][29] (optional, default `'index.handler'`)
- `options.KmsKeyArn` **[String][23]** See [AWS documentation][30] (optional, default `undefined`)
- `options.MemorySize` **[Number][31]** See [AWS documentation][32] (optional, default `128`)
- `options.ReservedConcurrencyExecutions` **[Number][31]** See [AWS documentation][33] (optional, default `undefined`)
- `options.ReservedConcurrentExecutions` **[Number][31]** See [AWS documentation][33] (optional, default `undefined`)
- `options.Runtime` **[String][23]** See [AWS documentation][34] (optional, default `'nodejs8.10'`)
- `options.Tags` **[Array][35]<[Object][22]>** See [AWS documentation][36] (optional, default `undefined`)
- `options.Timeout` **[Number][31]** See [AWS documentation][37] (optional, default `300`)
Expand Down Expand Up @@ -136,8 +136,9 @@ mapping.
- `options` **[Object][22]** configuration options for the scheduled Lambda
function and related resources. Extends [the `options` for a vanilla Lambda
function][2] with the following additional attributes: (optional, default `{}`)
- `options.EventSourceArn` **[String][23]** See [AWS documentation][54]
- `options.ReservedConcurrencyExecutions` **[Number][31]** See [AWS documentation][33]
- `options.BatchSize` **[Number][31]** See [AWS documentation][54] (optional, default `1`)
- `options.EventSourceArn` **[String][23]** See [AWS documentation][55]
- `options.ReservedConcurrentExecutions` **[Number][31]** See [AWS documentation][33]

### Examples

Expand All @@ -153,7 +154,7 @@ const lambda = new cf.shortcuts.QueueLambda({
S3Key: 'path/to/code.zip'
},
EventSourceArn: cf.getAtt('MyQueue', 'Arn'),
ReservedConcurrencyExecutions: 30
ReservedConcurrentExecutions: 30
});

module.exports = cf.merge(myTemplate, lambda);
Expand All @@ -172,8 +173,8 @@ source mapping.
- `options` **[Object][22]** configuration options for the scheduled Lambda
function and related resources. Extends [the `options` for a vanilla Lambda
function][2] with the following additional attributes: (optional, default `{}`)
- `options.EventSourceArn` **[String][23]** See [AWS documentation][54]
- `options.BatchSize` **[Number][31]** See [AWS documentation][55] (optional, default `1`)
- `options.EventSourceArn` **[String][23]** See [AWS documentation][55]
- `options.BatchSize` **[Number][31]** See [AWS documentation][54] (optional, default `1`)
- `options.Enabled` **[Boolean][56]** See [AWS documentation][57] (optional, default `true`)
- `options.StartingPosition` **[String][23]** See [AWS documentation][58] (optional, default `'LATEST'`)

Expand Down Expand Up @@ -256,7 +257,7 @@ to publish messages to the queue.
within the CloudFormation template. This is also used to construct the logical
names of the other resources.
- `options.VisibilityTimeout` **[Number][31]** See [AWS documentation][64] (optional, default `300`)
- `options.maxReceiveCount` **[Number][31]** See [AWS documentation][65] (optional, default `300`)
- `options.maxReceiveCount` **[Number][31]** See [AWS documentation][65] (optional, default `10`)
- `options.ContentBasedDeduplication` **[Boolean][56]** See [AWS documentation][66] (optional, default `undefined`)
- `options.DelaySeconds` **[Number][31]** See [AWS documentation][67] (optional, default `undefined`)
- `options.FifoQueue` **[Boolean][56]** See [AWS documentation][68] (optional, default `undefined`)
Expand Down Expand Up @@ -397,9 +398,9 @@ module.exports = cf.merge(myTemplate, queue);

[53]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-state

[54]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html#cfn-lambda-eventsourcemapping-eventsourcearn
[54]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html#cfn-lambda-eventsourcemapping-batchsize

[55]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html#cfn-lambda-eventsourcemapping-batchsize
[55]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html#cfn-lambda-eventsourcemapping-eventsourcearn

[56]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean

Expand Down
6 changes: 3 additions & 3 deletions lib/shortcuts/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @param {String} [options.Handler='index.handler'] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-handler)
* @param {String} [options.KmsKeyArn=undefined] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-kmskeyarn)
* @param {Number} [options.MemorySize=128] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-memorysize)
* @param {Number} [options.ReservedConcurrencyExecutions=undefined] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-reservedconcurrentexecutions)
* @param {Number} [options.ReservedConcurrentExecutions=undefined] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-reservedconcurrentexecutions)
* @param {String} [options.Runtime='nodejs8.10'] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-runtime)
* @param {Array<Object>} [options.Tags=undefined] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-tags)
* @param {Number} [options.Timeout=300] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-timeout)
Expand Down Expand Up @@ -70,7 +70,7 @@ class Lambda {
Handler = 'index.handler',
KmsKeyArn,
MemorySize = 128,
ReservedConcurrencyExecutions,
ReservedConcurrentExecutions,
Runtime = Code && Code.ZipFile ? 'nodejs6.10' : 'nodejs8.10',
Tags,
Timeout = 300,
Expand Down Expand Up @@ -166,7 +166,7 @@ class Lambda {
Handler,
KmsKeyArn,
MemorySize,
ReservedConcurrencyExecutions,
ReservedConcurrentExecutions,
Role: { 'Fn::GetAtt': [`${LogicalName}Role`, 'Arn'] },
Runtime,
Timeout,
Expand Down
21 changes: 14 additions & 7 deletions lib/shortcuts/queue-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const Lambda = require('./lambda');
* @param {Object} options configuration options for the scheduled Lambda
* function and related resources. Extends [the `options` for a vanilla Lambda
* function](#parameters) with the following additional attributes:
* @param {Number} [options.BatchSize=1] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html#cfn-lambda-eventsourcemapping-batchsize)
* @param {String} options.EventSourceArn See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html#cfn-lambda-eventsourcemapping-eventsourcearn)
* @param {Number} options.ReservedConcurrencyExecutions See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-reservedconcurrentexecutions)
* @param {Number} options.ReservedConcurrentExecutions See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-reservedconcurrentexecutions)
*
* @example
* const cf = require('@mapbox/cloudfriend');
Expand All @@ -25,7 +26,7 @@ const Lambda = require('./lambda');
* S3Key: 'path/to/code.zip'
* },
* EventSourceArn: cf.getAtt('MyQueue', 'Arn'),
* ReservedConcurrencyExecutions: 30
* ReservedConcurrentExecutions: 30
* });
*
* module.exports = cf.merge(myTemplate, lambda);
Expand All @@ -34,11 +35,15 @@ class QueueLambda extends Lambda {
constructor(options = {}) {
super(options);

const { EventSourceArn, ReservedConcurrencyExecutions } = options;
const {
BatchSize = 1,
EventSourceArn,
ReservedConcurrentExecutions
} = options;

const required = [EventSourceArn, ReservedConcurrencyExecutions];
const required = [EventSourceArn, ReservedConcurrentExecutions];
if (required.some((variable) => !variable))
throw new Error('You must provide an EventSourceArn and ReservedConcurrencyExecutions');
throw new Error('You must provide an EventSourceArn and ReservedConcurrentExecutions');

const { Enabled = true } = options;

Expand All @@ -47,8 +52,9 @@ class QueueLambda extends Lambda {
Condition: this.Condition,
Properties: {
Enabled,
BatchSize,
EventSourceArn,
FunctionName: this.FunctionName
FunctionName: { Ref: this.LogicalName }
}
};

Expand All @@ -57,7 +63,8 @@ class QueueLambda extends Lambda {
Effect: 'Allow',
Action: [
'sqs:DeleteMessage',
'sqs:ReceiveMessage'
'sqs:ReceiveMessage',
'sqs:GetQueueAttributes'
],
Resource: [
EventSourceArn,
Expand Down
12 changes: 7 additions & 5 deletions lib/shortcuts/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* within the CloudFormation template. This is also used to construct the logical
* names of the other resources.
* @param {Number} [options.VisibilityTimeout=300] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html#aws-sqs-queue-visibilitytimeout)
* @param {Number} [options.maxReceiveCount=300] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues-redrivepolicy.html#aws-sqs-queue-redrivepolicy-maxcount)
* @param {Number} [options.maxReceiveCount=10] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues-redrivepolicy.html#aws-sqs-queue-redrivepolicy-maxcount)
* @param {Boolean} [options.ContentBasedDeduplication=undefined] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html#cfn-sqs-queue-contentbaseddeduplication)
* @param {Number} [options.DelaySeconds=undefined] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html#aws-sqs-queue-delayseconds)
* @param {Boolean} [options.FifoQueue=undefined] See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html#cfn-sqs-queue-fifoqueue)
Expand Down Expand Up @@ -108,10 +108,12 @@ class Queue {
Properties: {
TopicName,
DisplayName,
Subscription: {
Endpoint: { 'Fn::GetAtt': [LogicalName, 'Arn'] },
Protocol: 'sqs'
}
Subscription: [
{
Endpoint: { 'Fn::GetAtt': [LogicalName, 'Arn'] },
Protocol: 'sqs'
}
]
}
},

Expand Down
2 changes: 1 addition & 1 deletion lib/shortcuts/stream-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class StreamLambda extends Lambda {
BatchSize,
Enabled,
EventSourceArn,
FunctionName: this.FunctionName,
FunctionName: { Ref: this.LogicalName },
StartingPosition
}
};
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/shortcuts/lambda-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"Handler": "index.something",
"KmsKeyArn": "arn:aws:kms:us-east-1:123456789012:key/fake",
"MemorySize": 512,
"ReservedConcurrencyExecutions": 10,
"ReservedConcurrentExecutions": 10,
"Role": {
"Fn::GetAtt": [
"MyLambdaRole",
Expand Down
20 changes: 11 additions & 9 deletions test/fixtures/shortcuts/queue-defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@
"TopicName": {
"Fn::Sub": "${AWS::StackName}-MyQueue"
},
"Subscription": {
"Endpoint": {
"Fn::GetAtt": [
"MyQueue",
"Arn"
]
},
"Protocol": "sqs"
}
"Subscription": [
{
"Endpoint": {
"Fn::GetAtt": [
"MyQueue",
"Arn"
]
},
"Protocol": "sqs"
}
]
}
},
"MyQueuePolicy": {
Expand Down
20 changes: 11 additions & 9 deletions test/fixtures/shortcuts/queue-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@
"Properties": {
"TopicName": "my-topic",
"DisplayName": "topic-display-name",
"Subscription": {
"Endpoint": {
"Fn::GetAtt": [
"MyQueue",
"Arn"
]
},
"Protocol": "sqs"
}
"Subscription": [
{
"Endpoint": {
"Fn::GetAtt": [
"MyQueue",
"Arn"
]
},
"Protocol": "sqs"
}
]
}
},
"MyQueuePolicy": {
Expand Down
8 changes: 5 additions & 3 deletions test/fixtures/shortcuts/queue-lambda.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"Effect": "Allow",
"Action": [
"sqs:DeleteMessage",
"sqs:ReceiveMessage"
"sqs:ReceiveMessage",
"sqs:GetQueueAttributes"
],
"Resource": [
"arn:aws:sqs:us-east-1:123456789012:queue/fake",
Expand Down Expand Up @@ -91,7 +92,7 @@
},
"Handler": "index.handler",
"MemorySize": 128,
"ReservedConcurrencyExecutions": 10,
"ReservedConcurrentExecutions": 10,
"Role": {
"Fn::GetAtt": [
"MyLambdaRole",
Expand Down Expand Up @@ -141,9 +142,10 @@
"Type": "AWS::Lambda::EventSourceMapping",
"Properties": {
"Enabled": true,
"BatchSize": 1,
"EventSourceArn": "arn:aws:sqs:us-east-1:123456789012:queue/fake",
"FunctionName": {
"Fn::Sub": "${AWS::StackName}-MyLambda"
"Ref": "MyLambda"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/shortcuts/stream-lambda.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"Enabled": true,
"EventSourceArn": "arn:aws:sqs:us-east-1:123456789012:queue/fake",
"FunctionName": {
"Fn::Sub": "${AWS::StackName}-MyLambda"
"Ref": "MyLambda"
},
"StartingPosition": "LATEST"
}
Expand Down
6 changes: 3 additions & 3 deletions test/shortcuts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test('[shortcuts] lambda', (assert) => {
Handler: 'index.something',
KmsKeyArn: 'arn:aws:kms:us-east-1:123456789012:key/fake',
MemorySize: 512,
ReservedConcurrencyExecutions: 10,
ReservedConcurrentExecutions: 10,
Runtime: 'nodejs6.10',
Tags: [{ Key: 'a', Value: 'b' }],
Timeout: 30,
Expand Down Expand Up @@ -140,7 +140,7 @@ test('[shortcuts] queue-lambda', (assert) => {
S3Key: 'path/to/code.zip'
}
}),
/You must provide an EventSourceArn and ReservedConcurrencyExecutions/,
/You must provide an EventSourceArn and ReservedConcurrentExecutions/,
'throws without queue-lambda required parameters'
);

Expand All @@ -151,7 +151,7 @@ test('[shortcuts] queue-lambda', (assert) => {
S3Key: 'path/to/code.zip'
},
EventSourceArn: 'arn:aws:sqs:us-east-1:123456789012:queue/fake',
ReservedConcurrencyExecutions: 10
ReservedConcurrentExecutions: 10
});

const template = cf.merge(lambda);
Expand Down