-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
firelens-log-router.ts
273 lines (247 loc) · 8.7 KB
/
firelens-log-router.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { Construct } from 'constructs';
import { TaskDefinition } from './base/task-definition';
import { ContainerDefinition, ContainerDefinitionOptions, ContainerDefinitionProps } from './container-definition';
import { ContainerImage } from './container-image';
import { CfnTaskDefinition } from './ecs.generated';
import { LogDriverConfig } from './log-drivers/log-driver';
import * as iam from '../../aws-iam';
import * as ssm from '../../aws-ssm';
import * as cdk from '../../core';
/**
* Firelens log router type, fluentbit or fluentd.
* https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_firelens.html
*/
export enum FirelensLogRouterType {
/**
* fluentbit
*/
FLUENTBIT = 'fluentbit',
/**
* fluentd
*/
FLUENTD = 'fluentd',
}
/**
* Firelens configuration file type, s3 or file path.
* https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_firelens.html#firelens-taskdef-customconfig
*/
export enum FirelensConfigFileType {
/**
* s3
*/
S3 = 's3',
/**
* fluentd
*/
FILE = 'file',
}
/**
* The options for firelens log router
* https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_firelens.html#firelens-taskdef-customconfig
*/
export interface FirelensOptions {
/**
* By default, Amazon ECS adds additional fields in your log entries that help identify the source of the logs.
* You can disable this action by setting enable-ecs-log-metadata to false.
* @default - true
*/
readonly enableECSLogMetadata?: boolean;
/**
* Custom configuration file, s3 or file.
* Both configFileType and configFileValue must be used together
* to define a custom configuration source.
*
* @default - determined by checking configFileValue with S3 ARN.
*/
readonly configFileType?: FirelensConfigFileType;
/**
* Custom configuration file, S3 ARN or a file path
* Both configFileType and configFileValue must be used together
* to define a custom configuration source.
*
* @default - no config file value
*/
readonly configFileValue?: string;
}
/**
* Firelens Configuration
* https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_firelens.html#firelens-taskdef
*/
export interface FirelensConfig {
/**
* The log router to use
* @default - fluentbit
*/
readonly type: FirelensLogRouterType;
/**
* Firelens options
* @default - no additional options
*/
readonly options?: FirelensOptions;
}
/**
* The properties in a firelens log router.
*/
export interface FirelensLogRouterProps extends ContainerDefinitionProps {
/**
* Firelens configuration
*/
readonly firelensConfig: FirelensConfig;
}
/**
* The options for creating a firelens log router.
*/
export interface FirelensLogRouterDefinitionOptions extends ContainerDefinitionOptions {
/**
* Firelens configuration
*/
readonly firelensConfig: FirelensConfig;
}
/**
* Render to CfnTaskDefinition.FirelensConfigurationProperty from FirelensConfig
*/
function renderFirelensConfig(firelensConfig: FirelensConfig): CfnTaskDefinition.FirelensConfigurationProperty {
if (!firelensConfig.options) {
return { type: firelensConfig.type };
} else if (firelensConfig.options.configFileValue === undefined) {
// config file options work as a pair together to define a custom config source
// a custom config source is optional,
// and thus the `config-file-x` keys should be set together or not at all
return {
type: firelensConfig.type,
options: {
'enable-ecs-log-metadata': firelensConfig.options.enableECSLogMetadata ? 'true' : 'false',
},
};
} else {
// firelensConfig.options.configFileType has been filled with s3 or file type in constructor.
return {
type: firelensConfig.type,
options: {
'enable-ecs-log-metadata': firelensConfig.options.enableECSLogMetadata ? 'true' : 'false',
'config-file-type': firelensConfig.options.configFileType!,
'config-file-value': firelensConfig.options.configFileValue,
},
};
}
}
/**
* SSM parameters for latest fluent bit docker image in ECR
* https://github.com/aws/aws-for-fluent-bit#using-ssm-to-find-available-versions
*/
const fluentBitImageSSMPath = '/aws/service/aws-for-fluent-bit';
/**
* Obtain Fluent Bit image in Amazon ECR and setup corresponding IAM permissions.
* ECR image pull permissions will be granted in task execution role.
* Cloudwatch logs, Kinesis data stream or firehose permissions will be grant by check options in logDriverConfig.
* https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_firelens.html#firelens-using-fluentbit
*/
export function obtainDefaultFluentBitECRImage(task: TaskDefinition, logDriverConfig?: LogDriverConfig, imageTag?: string): ContainerImage {
// grant ECR image pull permissions to executor role
task.addToExecutionRolePolicy(new iam.PolicyStatement({
actions: [
'ecr:GetAuthorizationToken',
'ecr:BatchCheckLayerAvailability',
'ecr:GetDownloadUrlForLayer',
'ecr:BatchGetImage',
],
resources: ['*'],
}));
// grant cloudwatch or firehose permissions to task role
const logName = logDriverConfig && logDriverConfig.logDriver === 'awsfirelens'
&& logDriverConfig.options && logDriverConfig.options.Name;
if (logName === 'cloudwatch') {
task.addToTaskRolePolicy(new iam.PolicyStatement({
actions: [
'logs:CreateLogGroup',
'logs:CreateLogStream',
'logs:DescribeLogStreams',
'logs:PutLogEvents',
],
resources: ['*'],
}));
} else if (logName === 'firehose') {
task.addToTaskRolePolicy(new iam.PolicyStatement({
actions: [
'firehose:PutRecordBatch',
],
resources: ['*'],
}));
} else if (logName === 'kinesis') {
task.addToTaskRolePolicy(new iam.PolicyStatement({
actions: [
'kinesis:PutRecords',
],
resources: ['*'],
}));
}
const fluentBitImageTag = imageTag || 'latest';
const fluentBitImage = `${fluentBitImageSSMPath}/${fluentBitImageTag}`;
// Not use ContainerImage.fromEcrRepository since it's not support parsing ECR repo URI,
// use repo ARN might result in complex Fn:: functions in cloudformation template.
return ContainerImage.fromRegistry(ssm.StringParameter.valueForStringParameter(task, fluentBitImage));
}
/**
* Firelens log router
*/
export class FirelensLogRouter extends ContainerDefinition {
/**
* Firelens configuration
*/
public readonly firelensConfig: FirelensConfig;
/**
* Constructs a new instance of the FirelensLogRouter class.
*/
constructor(scope: Construct, id: string, props: FirelensLogRouterProps) {
super(scope, id, props);
const options = props.firelensConfig.options;
if (options) {
if ((options.configFileValue && options.configFileType === undefined) || (options.configFileValue === undefined && options.configFileType)) {
throw new Error('configFileValue and configFileType must be set together to define a custom config source');
}
const hasConfig = (options.configFileValue !== undefined);
const enableECSLogMetadata = options.enableECSLogMetadata || options.enableECSLogMetadata === undefined;
const configFileType = (options.configFileType === undefined || options.configFileType === FirelensConfigFileType.S3) &&
(cdk.Token.isUnresolved(options.configFileValue) || /arn:aws[a-zA-Z-]*:s3:::.+/.test(options.configFileValue || ''))
? FirelensConfigFileType.S3 : FirelensConfigFileType.FILE;
this.firelensConfig = {
type: props.firelensConfig.type,
options: {
enableECSLogMetadata,
...(hasConfig ? {
configFileType,
configFileValue: options.configFileValue,
} : {}),
},
};
if (hasConfig) {
// grant s3 access permissions
if (configFileType === FirelensConfigFileType.S3) {
props.taskDefinition.addToExecutionRolePolicy(new iam.PolicyStatement({
actions: [
's3:GetObject',
],
resources: [(options.configFileValue ?? '')],
}));
props.taskDefinition.addToExecutionRolePolicy(new iam.PolicyStatement({
actions: [
's3:GetBucketLocation',
],
resources: [(options.configFileValue ?? '').split('/')[0]],
}));
}
}
} else {
this.firelensConfig = props.firelensConfig;
}
}
/**
* Render this container definition to a CloudFormation object
*/
public renderContainerDefinition(_taskDefinition?: TaskDefinition): CfnTaskDefinition.ContainerDefinitionProperty {
return {
...(super.renderContainerDefinition()),
firelensConfiguration: this.firelensConfig && renderFirelensConfig(this.firelensConfig),
};
}
}