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..5ca472336f349 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,10 @@ export interface FirelensOptions { /** * Custom configuration file, S3 ARN or a file path + * + * @default - no config file value */ - readonly configFileValue: string; + readonly configFileValue?: string; } /** @@ -109,12 +111,22 @@ export interface FirelensLogRouterDefinitionOptions extends ContainerDefinitionO 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 ?? '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', + 'enable-ecs-log-metadata': firelensConfig.options.enableECSLogMetadata ?? 'false', 'config-file-type': firelensConfig.options.configFileType!, 'config-file-value': firelensConfig.options.configFileValue, }, @@ -201,34 +213,48 @@ export class FirelensLogRouter extends ContainerDefinition { super(scope, id, props); const options = props.firelensConfig.options; if (options) { + 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, - configFileType, - configFileValue: options.configFileValue, - }, - }; - - // 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]], - })); + (cdk.Token.isUnresolved(options.configFileValue) || /arn:aws[a-zA-Z-]*:s3:::.+/.test(options.configFileValue || '')) + ? FirelensConfigFileType.S3 : FirelensConfigFileType.FILE; + + if (hasConfig) { + this.firelensConfig = { + type: props.firelensConfig.type, + options: { + enableECSLogMetadata, + configFileType, + configFileValue: options.configFileValue, + }, + }; + } else { + this.firelensConfig = { + type: props.firelensConfig.type, + options: { + enableECSLogMetadata: enableECSLogMetadata, + }, + }; + } + + 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; }