diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/firelens.test.ts b/packages/@aws-cdk-containers/ecs-service-extensions/test/firelens.test.ts index 2f30e6139e33f..ebdfac8e876d8 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/firelens.test.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/firelens.test.ts @@ -78,6 +78,9 @@ describe('firelens', () => { Essential: true, FirelensConfiguration: { Type: 'fluentbit', + Options: { + 'enable-ecs-log-metadata': 'true', + }, }, Image: { Ref: 'SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter', diff --git a/packages/@aws-cdk/aws-ecs/lib/firelens-log-router.ts b/packages/@aws-cdk/aws-ecs/lib/firelens-log-router.ts index 0d77fca0eb0e2..b73267bcd883e 100644 --- a/packages/@aws-cdk/aws-ecs/lib/firelens-log-router.ts +++ b/packages/@aws-cdk/aws-ecs/lib/firelens-log-router.ts @@ -60,8 +60,9 @@ export interface FirelensOptions { /** * Custom configuration file, S3 ARN or a file path + * @default - No default file value */ - readonly configFileValue: string; + readonly configFileValue?: string; } /** @@ -78,7 +79,7 @@ export interface FirelensConfig { /** * Firelens options - * @default - no additional options + * @default - EnableECSLogMetadata option is set to true */ readonly options?: FirelensOptions; } @@ -116,7 +117,7 @@ function renderFirelensConfig(firelensConfig: FirelensConfig): CfnTaskDefinition options: { 'enable-ecs-log-metadata': firelensConfig.options.enableECSLogMetadata ? 'true' : 'false', 'config-file-type': firelensConfig.options.configFileType!, - 'config-file-value': firelensConfig.options.configFileValue, + 'config-file-value': firelensConfig.options.configFileValue!, }, }; } @@ -200,37 +201,43 @@ export class FirelensLogRouter extends ContainerDefinition { constructor(scope: Construct, id: string, props: FirelensLogRouterProps) { super(scope, id, props); const options = props.firelensConfig.options; - if (options) { - 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)) + const enableECSLogMetadata = options?.enableECSLogMetadata || options?.enableECSLogMetadata === undefined; + if (options?.configFileValue) { + 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, configFileType, - configFileValue: options.configFileValue, + configFileValue: options?.configFileValue, }, }; - // grant s3 access permissions if (configFileType === FirelensConfigFileType.S3) { props.taskDefinition.addToExecutionRolePolicy(new iam.PolicyStatement({ actions: [ 's3:GetObject', ], - resources: [options.configFileValue], + resources: [options?.configFileValue], })); props.taskDefinition.addToExecutionRolePolicy(new iam.PolicyStatement({ actions: [ 's3:GetBucketLocation', ], - resources: [options.configFileValue.split('/')[0]], + resources: [options?.configFileValue.split('/')[0]], })); } + } else if (options?.configFileType && !options?.configFileValue) { + throw new Error('Firelens Config Error: Config file value cannot be undefined when config file type is declared.'); } else { - this.firelensConfig = props.firelensConfig; + this.firelensConfig = { + type: props.firelensConfig.type, + options: { + enableECSLogMetadata, + }, + }; } } diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/integ.firelens-cloudwatch.expected.json b/packages/@aws-cdk/aws-ecs/test/fargate/integ.firelens-cloudwatch.expected.json index a10c635e498d6..fb627b0328ac2 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/integ.firelens-cloudwatch.expected.json +++ b/packages/@aws-cdk/aws-ecs/test/fargate/integ.firelens-cloudwatch.expected.json @@ -431,7 +431,10 @@ { "Essential": true, "FirelensConfiguration": { - "Type": "fluentbit" + "Type": "fluentbit", + "Options": { + "enable-ecs-log-metadata": "true" + } }, "Image": { "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" diff --git a/packages/@aws-cdk/aws-ecs/test/firelens-log-driver.test.ts b/packages/@aws-cdk/aws-ecs/test/firelens-log-driver.test.ts index 6ab96b05df364..e4ee7c06ccf9f 100644 --- a/packages/@aws-cdk/aws-ecs/test/firelens-log-driver.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/firelens-log-driver.test.ts @@ -35,6 +35,9 @@ describe('firelens log driver', () => { Essential: true, FirelensConfiguration: { Type: 'fluentbit', + Options: { + 'enable-ecs-log-metadata': 'true', + }, }, }, ], @@ -211,6 +214,9 @@ describe('firelens log driver', () => { Essential: true, FirelensConfiguration: { Type: 'fluentbit', + Options: { + 'enable-ecs-log-metadata': 'true', + }, }, }, ], @@ -250,6 +256,9 @@ describe('firelens log driver', () => { Essential: true, FirelensConfiguration: { Type: 'fluentbit', + Options: { + 'enable-ecs-log-metadata': 'true', + }, }, }, ], @@ -279,6 +288,9 @@ describe('firelens log driver', () => { Name: 'log_router', FirelensConfiguration: { Type: 'fluentd', + Options: { + 'enable-ecs-log-metadata': 'true', + }, }, }, ], @@ -359,8 +371,6 @@ describe('firelens log driver', () => { }, ], }); - - }); }); });