Skip to content

Commit

Permalink
feat(lambda): allow specify event sources in props (#1746)
Browse files Browse the repository at this point in the history
To enable declarative use cases, allow specifying IEventSources
when defining the function.
  • Loading branch information
Elad Ben-Israel committed Feb 12, 2019
1 parent 39df456 commit a84157d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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[];
}

/**
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

0 comments on commit a84157d

Please sign in to comment.