-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
lambda.ts
36 lines (31 loc) · 1004 Bytes
/
lambda.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import events = require('@aws-cdk/aws-events');
import iam = require('@aws-cdk/aws-iam');
import lambda = require('@aws-cdk/aws-lambda');
/**
* Use an AWS Lambda function as an event rule target.
*/
export class LambdaFunction implements events.IEventRuleTarget {
/**
* @param handler The lambda function
*/
constructor(private readonly handler: lambda.IFunction) {
}
/**
* Returns a RuleTarget that can be used to trigger this Lambda as a
* result from a CloudWatch event.
*/
public asEventRuleTarget(ruleArn: string, ruleId: string): events.EventRuleTargetProps {
const permissionId = `AllowEventRule${ruleId}`;
if (!this.handler.node.tryFindChild(permissionId)) {
this.handler.addPermission(permissionId, {
action: 'lambda:InvokeFunction',
principal: new iam.ServicePrincipal('events.amazonaws.com'),
sourceArn: ruleArn
});
}
return {
id: this.handler.node.id,
arn: this.handler.functionArn,
};
}
}