-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (55 loc) · 2.08 KB
/
index.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module.exports = function (arc, cloudformation, stage) {
if (!arc.logs) {
return cloudformation;
}
const params = {};
arc.logs.forEach(e => {
if (e.length === 2) {
params[e[0]] = e[1];
} else {
params[e[0]] = e.slice(1, e.length);
}
});
const subscriptionFunction = params.subscriptionFunction || false;
const subscriptionFilter = params.subscriptionFilter ? params.subscriptionFilter.join(' ') : false;
const retention = params.retention || 14;
cloudformation.Resources.Role.Properties.Policies[0].PolicyDocument.Statement[0].Action.push('logs:PutSubscriptionFilter');
Object.entries(cloudformation.Resources).forEach(([key, value]) => {
if (value.Type === 'AWS::Serverless::Function') {
//Set Log Group
cloudformation.Resources[`${key}LogGroup`] = {
Type: 'AWS::Logs::LogGroup',
Properties: {
LogGroupName: { 'Fn::Join': ['', ['/aws/lambda/', { Ref: key }]] },
RetentionInDays: retention
}
};
//skip if subscription consumer
if (subscriptionFunction && key !== subscriptionFunction) {
cloudformation.Resources[`${key}LogGroupLambdaInvokePermission`] = {
Type: 'AWS::Lambda::Permission',
Properties: {
Principal: { 'Fn::Sub': 'logs.${AWS::Region}.amazonaws.com' },
Action: 'lambda:InvokeFunction',
FunctionName: { 'Fn::GetAtt': [subscriptionFunction, 'Arn'] },
SourceAccount: { Ref: 'AWS::AccountId' },
SourceArn: { 'Fn::GetAtt': [`${key}LogGroup`, 'Arn'] }
}
};
if (!subscriptionFilter || !subscriptionFunction) {
return;
}
cloudformation.Resources[`${key}SubscriptionFilter`] = {
Type: 'AWS::Logs::SubscriptionFilter',
DependsOn: `${key}LogGroupLambdaInvokePermission`,
Properties: {
LogGroupName: { Ref: `${key}LogGroup` },
FilterPattern: subscriptionFilter,
DestinationArn: { 'Fn::GetAtt': [subscriptionFunction, 'Arn'] }
}
};
}
}
});
return cloudformation;
};