Skip to content

Commit a84157d

Browse files
author
Elad Ben-Israel
authored
feat(lambda): allow specify event sources in props (#1746)
To enable declarative use cases, allow specifying IEventSources when defining the function.
1 parent 39df456 commit a84157d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

packages/@aws-cdk/aws-lambda/lib/function.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import iam = require('@aws-cdk/aws-iam');
44
import sqs = require('@aws-cdk/aws-sqs');
55
import cdk = require('@aws-cdk/cdk');
66
import { Code } from './code';
7+
import { IEventSource } from './event-source';
78
import { FunctionBase, FunctionImportProps, IFunction } from './function-base';
89
import { Version } from './lambda-version';
910
import { CfnFunction } from './lambda.generated';
@@ -190,6 +191,13 @@ export interface FunctionProps {
190191
* @see https://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html
191192
*/
192193
reservedConcurrentExecutions?: number;
194+
195+
/**
196+
* Event sources for this function.
197+
*
198+
* You can also add event sources using `addEventSource`.
199+
*/
200+
events?: IEventSource[];
193201
}
194202

195203
/**
@@ -383,6 +391,10 @@ export class Function extends FunctionBase {
383391
for (const layer of props.layers || []) {
384392
this.addLayer(layer);
385393
}
394+
395+
for (const event of props.events || []) {
396+
this.addEventSource(event);
397+
}
386398
}
387399

388400
/**

packages/@aws-cdk/aws-lambda/test/test.lambda.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,34 @@ export = {
12591259
Runtime: 'nodejs' },
12601260
DependsOn: [ 'MyLambdaServiceRole4539ECB6' ] } } });
12611261
test.done();
1262+
},
1263+
1264+
'its possible to specify event sources upon creation'(test: Test) {
1265+
// GIVEN
1266+
const stack = new cdk.Stack();
1267+
1268+
let bindCount = 0;
1269+
1270+
class EventSource implements lambda.IEventSource {
1271+
public bind(_: lambda.FunctionBase): void {
1272+
bindCount++;
1273+
}
1274+
}
1275+
1276+
// WHEN
1277+
new lambda.Function(stack, 'fn', {
1278+
code: lambda.Code.inline('boom'),
1279+
runtime: lambda.Runtime.NodeJS810,
1280+
handler: 'index.bam',
1281+
events: [
1282+
new EventSource(),
1283+
new EventSource(),
1284+
]
1285+
});
1286+
1287+
// THEN
1288+
test.deepEqual(bindCount, 2);
1289+
test.done();
12621290
}
12631291
};
12641292

0 commit comments

Comments
 (0)