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

feat(lambda): allow specify event sources in props #1746

Merged
merged 1 commit into from
Feb 12, 2019
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
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import iam = require('@aws-cdk/aws-iam');
import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/cdk');
import { Code } from './code';
import { IEventSource } from './event-source';
import { FunctionBase, FunctionImportProps, IFunction } from './function-base';
import { Version } from './lambda-version';
import { CfnFunction } from './lambda.generated';
Expand Down Expand Up @@ -190,6 +191,13 @@ export interface FunctionProps {
* @see https://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html
*/
reservedConcurrentExecutions?: number;

/**
* Event sources for this function.
*
* You can also add event sources using `addEventSource`.
*/
events?: IEventSource[];
eladb marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -383,6 +391,10 @@ export class Function extends FunctionBase {
for (const layer of props.layers || []) {
this.addLayer(layer);
}

for (const event of props.events || []) {
this.addEventSource(event);
}
}

/**
Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,34 @@ export = {
Runtime: 'nodejs' },
DependsOn: [ 'MyLambdaServiceRole4539ECB6' ] } } });
test.done();
},

'its possible to specify event sources upon creation'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

let bindCount = 0;

class EventSource implements lambda.IEventSource {
public bind(_: lambda.FunctionBase): void {
bindCount++;
}
}

// WHEN
new lambda.Function(stack, 'fn', {
code: lambda.Code.inline('boom'),
runtime: lambda.Runtime.NodeJS810,
handler: 'index.bam',
events: [
new EventSource(),
new EventSource(),
]
});

// THEN
test.deepEqual(bindCount, 2);
test.done();
}
};

Expand Down