diff --git a/packages/@aws-cdk/aws-lambda/lib/function.ts b/packages/@aws-cdk/aws-lambda/lib/function.ts index 590fca681d4a6..dfa04648cd51e 100644 --- a/packages/@aws-cdk/aws-lambda/lib/function.ts +++ b/packages/@aws-cdk/aws-lambda/lib/function.ts @@ -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'; @@ -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[]; } /** @@ -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); + } } /** diff --git a/packages/@aws-cdk/aws-lambda/test/test.lambda.ts b/packages/@aws-cdk/aws-lambda/test/test.lambda.ts index 030a44d15a78c..c1df96c06148d 100644 --- a/packages/@aws-cdk/aws-lambda/test/test.lambda.ts +++ b/packages/@aws-cdk/aws-lambda/test/test.lambda.ts @@ -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(); } };