From f524b65b00651f931b9f4ad33d3b392957a7a60c Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Tue, 17 May 2022 17:31:12 -0700 Subject: [PATCH] fix: firelens configFileValue is optional https://github.com/aws/aws-for-fluent-bit/issues/352 Signed-off-by: Wesley Pettit --- .../aws-ecs-integ.template.json | 10 +-- .../manifest.json | 4 +- .../tree.json | 18 ++--- .../test/firelens.test.ts | 2 +- .../integ.all-service-addons.expected.json | 10 +-- .../aws-ecs/lib/firelens-log-router.ts | 73 ++++++++++++------- .../aws-ecs-integ.template.json | 6 +- .../manifest.json | 4 +- .../tree.json | 14 ++-- 9 files changed, 81 insertions(+), 60 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/aws-ecs-integ.template.json b/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/aws-ecs-integ.template.json index de73b03a2adba..8cac56d639b38 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/aws-ecs-integ.template.json +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/aws-ecs-integ.template.json @@ -635,7 +635,7 @@ "Type": "fluentbit" }, "Image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "LogConfiguration": { "LogDriver": "awslogs", @@ -1481,7 +1481,7 @@ "Type": "fluentbit" }, "Image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "LogConfiguration": { "LogDriver": "awslogs", @@ -2446,7 +2446,7 @@ "Type": "fluentbit" }, "Image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "LogConfiguration": { "LogDriver": "awslogs", @@ -3293,9 +3293,9 @@ } }, "Parameters": { - "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter": { + "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter": { "Type": "AWS::SSM::Parameter::Value", - "Default": "/aws/service/aws-for-fluent-bit/latest" + "Default": "/aws/service/aws-for-fluent-bit/stable" } }, "Outputs": { diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/manifest.json b/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/manifest.json index 6fbe6f49577fb..ef401592d841a 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/manifest.json +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/manifest.json @@ -243,10 +243,10 @@ "data": "nameenvoytoappmesh2767D3E6" } ], - "/aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": [ + "/aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": [ { "type": "aws:cdk:logicalId", - "data": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "data": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" } ], "/aws-ecs-integ/name-publish-metrics/Resource": [ diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/tree.json b/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/tree.json index 4a90a3789d527..6d96d12b393a1 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/tree.json +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/all-service-addons.integ.snapshot/tree.json @@ -1038,7 +1038,7 @@ ], "essential": true, "image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "memoryReservation": 50, "name": "firelens", @@ -1536,17 +1536,17 @@ "version": "0.0.0" } }, - "SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": { - "id": "SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", - "path": "aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", + "SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": { + "id": "SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", + "path": "aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", "constructInfo": { "fqn": "@aws-cdk/core.CfnParameter", "version": "0.0.0" } }, - "SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118": { - "id": "SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118", - "path": "aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118", + "SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118": { + "id": "SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118", + "path": "aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118", "constructInfo": { "fqn": "@aws-cdk/core.Resource", "version": "0.0.0" @@ -2370,7 +2370,7 @@ ], "essential": true, "image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "memoryReservation": 50, "name": "firelens", @@ -3889,7 +3889,7 @@ ], "essential": true, "image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "memoryReservation": 50, "name": "firelens", 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 6cdd83d07610c..619b61afb9b4d 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 @@ -80,7 +80,7 @@ describe('firelens', () => { Type: 'fluentbit', }, Image: { - Ref: 'SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter', + Ref: 'SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter', }, LogConfiguration: { LogDriver: 'awslogs', diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json b/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json index 4f51a31f422dd..732c206e1770a 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json @@ -766,7 +766,7 @@ "Type": "fluentbit" }, "Image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "LogConfiguration": { "LogDriver": "awslogs", @@ -1615,7 +1615,7 @@ "Type": "fluentbit" }, "Image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "LogConfiguration": { "LogDriver": "awslogs", @@ -2587,7 +2587,7 @@ "Type": "fluentbit" }, "Image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "LogConfiguration": { "LogDriver": "awslogs", @@ -3437,9 +3437,9 @@ } }, "Parameters": { - "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter": { + "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter": { "Type": "AWS::SSM::Parameter::Value", - "Default": "/aws/service/aws-for-fluent-bit/latest" + "Default": "/aws/service/aws-for-fluent-bit/stable" } }, "Outputs": { 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..4505ea0f7be91 100644 --- a/packages/@aws-cdk/aws-ecs/lib/firelens-log-router.ts +++ b/packages/@aws-cdk/aws-ecs/lib/firelens-log-router.ts @@ -61,7 +61,7 @@ export interface FirelensOptions { /** * Custom configuration file, S3 ARN or a file path */ - readonly configFileValue: string; + readonly configFileValue?: string; } /** @@ -109,6 +109,14 @@ 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 must be set together and are optional + 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 { @@ -116,7 +124,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!, }, }; } @@ -203,32 +211,45 @@ export class FirelensLogRouter extends ContainerDefinition { 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)) + (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]], - })); + + if (options.configFileValue != undefined) { + this.firelensConfig = { + type: props.firelensConfig.type, + options: { + enableECSLogMetadata, + configFileType, + configFileValue: options.configFileValue, + }, + }; + } else { + this.firelensConfig = { + type: props.firelensConfig.type, + options: { + enableECSLogMetadata: enableECSLogMetadata, + }, + }; } + + if (options.configFileValue != undefined) { + // 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; } diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/aws-ecs-integ.template.json b/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/aws-ecs-integ.template.json index a188c8c3180a4..3cdb7b357828a 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/aws-ecs-integ.template.json +++ b/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/aws-ecs-integ.template.json @@ -462,7 +462,7 @@ "Type": "fluentbit" }, "Image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "LogConfiguration": { "LogDriver": "awslogs", @@ -627,9 +627,9 @@ } }, "Parameters": { - "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter": { + "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter": { "Type": "AWS::SSM::Parameter::Value", - "Default": "/aws/service/aws-for-fluent-bit/latest" + "Default": "/aws/service/aws-for-fluent-bit/stable" } } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/manifest.json b/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/manifest.json index e306a9bebf43b..8f65c2686f552 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/manifest.json +++ b/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/manifest.json @@ -207,10 +207,10 @@ "data": "ServiceD69D759B" } ], - "/aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": [ + "/aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": [ { "type": "aws:cdk:logicalId", - "data": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "data": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" } ] }, diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/tree.json b/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/tree.json index 702122211bcff..ca7774bab17eb 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/tree.json +++ b/packages/@aws-cdk/aws-ecs/test/fargate/firelens-cloudwatch.integ.snapshot/tree.json @@ -797,7 +797,7 @@ { "essential": true, "image": { - "Ref": "SsmParameterValueawsserviceawsforfluentbitlatestC96584B6F00A464EAD1953AFF4B05118Parameter" + "Ref": "SsmParameterValueawsserviceawsforfluentbitstableC96584B6F00A464EAD1953AFF4B05118Parameter" }, "memoryReservation": 50, "name": "log-router", @@ -1081,17 +1081,17 @@ "version": "0.0.0" } }, - "SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": { - "id": "SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", - "path": "aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", + "SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": { + "id": "SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", + "path": "aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", "constructInfo": { "fqn": "@aws-cdk/core.CfnParameter", "version": "0.0.0" } }, - "SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118": { - "id": "SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118", - "path": "aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--latest:C96584B6-F00A-464E-AD19-53AFF4B05118", + "SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118": { + "id": "SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118", + "path": "aws-ecs-integ/SsmParameterValue:--aws--service--aws-for-fluent-bit--stable:C96584B6-F00A-464E-AD19-53AFF4B05118", "constructInfo": { "fqn": "@aws-cdk/core.Resource", "version": "0.0.0"