/
lambda.ts
37 lines (34 loc) · 1.2 KB
/
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
37
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import * as logs from '@aws-cdk/aws-logs';
import { Construct } from '@aws-cdk/core';
/**
* Options that may be provided to LambdaDestination
*/
export interface LambdaDestinationOptions{
/** Whether or not to add Lambda Permissions.
* @default true
*/
readonly addPermissions?: boolean;
}
/**
* Use a Lambda Function as the destination for a log subscription
*/
export class LambdaDestination implements logs.ILogSubscriptionDestination {
/** LambdaDestinationOptions */
constructor(private readonly fn: lambda.IFunction, private readonly options: LambdaDestinationOptions = {}) {
}
public bind(scope: Construct, logGroup: logs.ILogGroup): logs.LogSubscriptionDestinationConfig {
const arn = logGroup.logGroupArn;
if (this.options.addPermissions !== false) {
this.fn.addPermission('CanInvokeLambda', {
principal: new iam.ServicePrincipal('logs.amazonaws.com'),
sourceArn: arn,
// Using SubScription Filter as scope is okay, since every Subscription Filter has only
// one destination.
scope,
});
}
return { arn: this.fn.functionArn };
}
}