From ad722713f8fe095b5045ee82e130b88f8575e802 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Fri, 21 Jun 2019 16:50:24 +0200 Subject: [PATCH] refactor(ecs): update LogDriver to new bind pattern (#2990) Update LogDriver bind in accordance with how we do binds in the rest of the library. BREAKING CHANGES: * **ecs**: `AwsLogDriver` no longer takes construct properties. --- .../lib/base/load-balanced-service-base.ts | 2 +- .../lib/base/queue-processing-service-base.ts | 2 +- .../lib/ecs/scheduled-ecs-task.ts | 2 +- ...integ.scheduled-ecs-task.lit.expected.json | 8 +- .../aws-ecs-patterns/test/ec2/test.l3s.ts | 4 +- .../ec2/test.queue-processing-ecs-service.ts | 2 +- .../test/ec2/test.scheduled-ecs-task.ts | 8 +- .../fargate/integ.asset-image.expected.json | 6 +- .../fargate/integ.executionrole.expected.json | 6 +- .../test/fargate/integ.l3.expected.json | 6 +- .../test.queue-processing-fargate-service.ts | 2 +- .../aws-ecs/lib/container-definition.ts | 10 +- .../aws-ecs/lib/log-drivers/aws-log-driver.ts | 31 +++---- .../aws-ecs/lib/log-drivers/log-driver.ts | 29 ++++-- .../aws-ecs/test/test.aws-log-driver.ts | 92 +++++++++++-------- .../aws-ecs/test/test.container-definition.ts | 6 +- .../integ.event-ec2-task.lit.expected.json | 6 +- .../test/ecs/integ.event-ec2-task.lit.ts | 2 +- .../integ.event-fargate-task.expected.json | 8 +- .../test/ecs/integ.event-fargate-task.ts | 2 +- .../test/integ.ec2-task.expected.json | 6 +- .../test/integ.ec2-task.ts | 2 +- .../test/integ.fargate-task.expected.json | 8 +- .../test/integ.fargate-task.ts | 2 +- 24 files changed, 143 insertions(+), 109 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts index 77299cdeea30f..f1239317977c7 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts @@ -185,6 +185,6 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { } private createAWSLogDriver(prefix: string): ecs.AwsLogDriver { - return new ecs.AwsLogDriver(this, 'Logging', { streamPrefix: prefix }); + return new ecs.AwsLogDriver({ streamPrefix: prefix }); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-processing-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-processing-service-base.ts index f35fa64a6cfb0..08b24683ec3ae 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-processing-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-processing-service-base.ts @@ -153,6 +153,6 @@ export abstract class QueueProcessingServiceBase extends cdk.Construct { * @param prefix the Cloudwatch logging prefix */ private createAWSLogDriver(prefix: string): ecs.AwsLogDriver { - return new ecs.AwsLogDriver(this, 'ProcessingContainerLogging', { streamPrefix: prefix }); + return new ecs.AwsLogDriver({ streamPrefix: prefix }); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts index b9b851ab7144f..ea7de37ba7d0e 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts @@ -94,7 +94,7 @@ export class ScheduledEc2Task extends cdk.Construct { cpu: props.cpu, command: props.command, environment: props.environment, - logging: new ecs.AwsLogDriver(this, 'ScheduledTaskLogging', { streamPrefix: this.node.id }) + logging: new ecs.AwsLogDriver({ streamPrefix: this.node.id }) }); // Use Ec2TaskEventRuleTarget as the target of the EventRule diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.expected.json b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.expected.json index 61916c2affd30..0ffc0270d1f57 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.expected.json +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.expected.json @@ -664,7 +664,7 @@ "LogDriver": "awslogs", "Options": { "awslogs-group": { - "Ref": "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3" + "Ref": "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6" }, "awslogs-stream-prefix": "ScheduledEc2Task", "awslogs-region": { @@ -740,7 +740,7 @@ "Effect": "Allow", "Resource": { "Fn::GetAtt": [ - "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3", + "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6", "Arn" ] } @@ -826,7 +826,7 @@ ] } }, - "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3": { + "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6": { "Type": "AWS::Logs::LogGroup", "DeletionPolicy": "Retain" }, @@ -868,4 +868,4 @@ "Default": "/aws/service/ecs/optimized-ami/amazon-linux/recommended/image_id" } } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts index 4050b72674cab..f46b734e5c677 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts @@ -119,7 +119,7 @@ export = { LogConfiguration: { LogDriver: "awslogs", Options: { - "awslogs-group": { Ref: "ServiceLoggingLogGroupC3D6A581" }, + "awslogs-group": { Ref: "ServiceTaskDefwebLogGroup2A898F61" }, "awslogs-stream-prefix": "Service", "awslogs-region": { Ref: "AWS::Region" } } @@ -175,7 +175,7 @@ export = { LogConfiguration: { LogDriver: "awslogs", Options: { - "awslogs-group": { Ref: "ServiceLoggingLogGroupC3D6A581" }, + "awslogs-group": { Ref: "ServiceTaskDefwebLogGroup2A898F61" }, "awslogs-stream-prefix": "Service", "awslogs-region": { Ref: "AWS::Region" } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.queue-processing-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.queue-processing-ecs-service.ts index 09a05dfe8e80f..a4c438207314d 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.queue-processing-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.queue-processing-ecs-service.ts @@ -47,7 +47,7 @@ export = { LogDriver: "awslogs", Options: { "awslogs-group": { - Ref: "ServiceProcessingContainerLoggingLogGroupF40B9C5D" + Ref: "ServiceQueueProcessingTaskDefQueueProcessingContainerLogGroupD52338D1" }, "awslogs-stream-prefix": "Service", "awslogs-region": { diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts index d1c4ba987318c..fb47b71d64e65 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts @@ -49,7 +49,7 @@ export = { LogDriver: "awslogs", Options: { "awslogs-group": { - Ref: "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3" + Ref: "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6" }, "awslogs-stream-prefix": "ScheduledEc2Task", "awslogs-region": { @@ -126,7 +126,7 @@ export = { LogDriver: "awslogs", Options: { "awslogs-group": { - Ref: "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3" + Ref: "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6" }, "awslogs-stream-prefix": "ScheduledEc2Task", "awslogs-region": { @@ -174,7 +174,7 @@ export = { LogDriver: "awslogs", Options: { "awslogs-group": { - Ref: "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3" + Ref: "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6" }, "awslogs-stream-prefix": "ScheduledEc2Task", "awslogs-region": { @@ -228,7 +228,7 @@ export = { LogDriver: "awslogs", Options: { "awslogs-group": { - Ref: "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3" + Ref: "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6" }, "awslogs-stream-prefix": "ScheduledEc2Task", "awslogs-region": { diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.expected.json b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.expected.json index 7a24b26882683..87dfe4da9093d 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.expected.json +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.expected.json @@ -346,7 +346,7 @@ "ClusterEB0386A7": { "Type": "AWS::ECS::Cluster" }, - "FargateServiceLoggingLogGroup9B16742A": { + "FargateServiceTaskDefwebLogGroup71FAF541": { "Type": "AWS::Logs::LogGroup", "DeletionPolicy": "Retain" }, @@ -589,7 +589,7 @@ "LogDriver": "awslogs", "Options": { "awslogs-group": { - "Ref": "FargateServiceLoggingLogGroup9B16742A" + "Ref": "FargateServiceTaskDefwebLogGroup71FAF541" }, "awslogs-stream-prefix": "FargateService", "awslogs-region": { @@ -739,7 +739,7 @@ "Effect": "Allow", "Resource": { "Fn::GetAtt": [ - "FargateServiceLoggingLogGroup9B16742A", + "FargateServiceTaskDefwebLogGroup71FAF541", "Arn" ] } diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.executionrole.expected.json b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.executionrole.expected.json index 0fde02d4dfda5..f0f2ab36c64df 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.executionrole.expected.json +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.executionrole.expected.json @@ -399,7 +399,7 @@ "Effect": "Allow", "Resource": { "Fn::GetAtt": [ - "L3LoggingLogGroupBD1F02DD", + "L3TaskDefwebLogGroupC6E4A38A", "Arn" ] } @@ -554,7 +554,7 @@ "LogDriver": "awslogs", "Options": { "awslogs-group": { - "Ref": "L3LoggingLogGroupBD1F02DD" + "Ref": "L3TaskDefwebLogGroupC6E4A38A" }, "awslogs-stream-prefix": "L3", "awslogs-region": { @@ -596,7 +596,7 @@ "Volumes": [] } }, - "L3LoggingLogGroupBD1F02DD": { + "L3TaskDefwebLogGroupC6E4A38A": { "Type": "AWS::Logs::LogGroup", "DeletionPolicy": "Retain" }, diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.expected.json b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.expected.json index 1124545dc7763..fa8007a38d028 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.expected.json +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.expected.json @@ -485,7 +485,7 @@ "LogDriver": "awslogs", "Options": { "awslogs-group": { - "Ref": "L3LoggingLogGroupBD1F02DD" + "Ref": "L3TaskDefwebLogGroupC6E4A38A" }, "awslogs-stream-prefix": "L3", "awslogs-region": { @@ -567,7 +567,7 @@ "Effect": "Allow", "Resource": { "Fn::GetAtt": [ - "L3LoggingLogGroupBD1F02DD", + "L3TaskDefwebLogGroupC6E4A38A", "Arn" ] } @@ -583,7 +583,7 @@ ] } }, - "L3LoggingLogGroupBD1F02DD": { + "L3TaskDefwebLogGroupC6E4A38A": { "Type": "AWS::Logs::LogGroup", "DeletionPolicy": "Retain" }, diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.queue-processing-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.queue-processing-fargate-service.ts index 6d79f0003906c..15462be611f24 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.queue-processing-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.queue-processing-fargate-service.ts @@ -47,7 +47,7 @@ export = { LogDriver: "awslogs", Options: { "awslogs-group": { - Ref: "ServiceProcessingContainerLoggingLogGroupF40B9C5D" + Ref: "ServiceQueueProcessingTaskDefQueueProcessingContainerLogGroupD52338D1" }, "awslogs-stream-prefix": "Service", "awslogs-region": { diff --git a/packages/@aws-cdk/aws-ecs/lib/container-definition.ts b/packages/@aws-cdk/aws-ecs/lib/container-definition.ts index 2342a78fc5fab..2001670bbedb3 100644 --- a/packages/@aws-cdk/aws-ecs/lib/container-definition.ts +++ b/packages/@aws-cdk/aws-ecs/lib/container-definition.ts @@ -4,7 +4,7 @@ import { NetworkMode, TaskDefinition } from './base/task-definition'; import { ContainerImage, ContainerImageConfig } from './container-image'; import { CfnTaskDefinition } from './ecs.generated'; import { LinuxParameters } from './linux-parameters'; -import { LogDriver } from './log-drivers/log-driver'; +import { LogDriver, LogDriverConfig } from './log-drivers/log-driver'; export interface ContainerDefinitionOptions { /** @@ -249,6 +249,8 @@ export class ContainerDefinition extends cdk.Construct { private readonly imageConfig: ContainerImageConfig; + private readonly logDriverConfig?: LogDriverConfig; + constructor(scope: cdk.Construct, id: string, private readonly props: ContainerDefinitionProps) { super(scope, id); this.essential = props.essential !== undefined ? props.essential : true; @@ -257,7 +259,9 @@ export class ContainerDefinition extends cdk.Construct { this.linuxParameters = props.linuxParameters; this.imageConfig = props.image.bind(this, this); - if (props.logging) { props.logging.bind(this); } + if (props.logging) { + this.logDriverConfig = props.logging.bind(this, this); + } props.taskDefinition._linkContainer(this); } @@ -410,7 +414,7 @@ export class ContainerDefinition extends cdk.Construct { user: this.props.user, volumesFrom: this.volumesFrom.map(renderVolumeFrom), workingDirectory: this.props.workingDirectory, - logConfiguration: this.props.logging && this.props.logging.renderLogDriver(), + logConfiguration: this.logDriverConfig, environment: this.props.environment && renderKV(this.props.environment, 'name', 'value'), extraHosts: this.props.extraHosts && renderKV(this.props.extraHosts, 'hostname', 'ipAddress'), healthCheck: this.props.healthCheck && renderHealthCheck(this.props.healthCheck), diff --git a/packages/@aws-cdk/aws-ecs/lib/log-drivers/aws-log-driver.ts b/packages/@aws-cdk/aws-ecs/lib/log-drivers/aws-log-driver.ts index 2c752ba0ad1ea..2be6c7f1125fc 100644 --- a/packages/@aws-cdk/aws-ecs/lib/log-drivers/aws-log-driver.ts +++ b/packages/@aws-cdk/aws-ecs/lib/log-drivers/aws-log-driver.ts @@ -1,9 +1,7 @@ import logs = require('@aws-cdk/aws-logs'); -import cdk = require('@aws-cdk/cdk'); -import { Stack } from '@aws-cdk/cdk'; +import { Construct, Stack } from '@aws-cdk/cdk'; import { ContainerDefinition } from '../container-definition'; -import { CfnTaskDefinition } from '../ecs.generated'; -import { LogDriver } from "./log-driver"; +import { LogDriver, LogDriverConfig } from "./log-driver"; /** * Properties for defining a new AWS Log Driver @@ -67,38 +65,35 @@ export interface AwsLogDriverProps { export class AwsLogDriver extends LogDriver { /** * The log group that the logs will be sent to + * + * Only available after the LogDriver has been bound to a ContainerDefinition. */ - public readonly logGroup: logs.ILogGroup; + public logGroup?: logs.ILogGroup; - constructor(scope: cdk.Construct, id: string, private readonly props: AwsLogDriverProps) { - super(scope, id); + constructor(private readonly props: AwsLogDriverProps) { + super(); if (props.logGroup && props.logRetention) { throw new Error('Cannot specify both `logGroup` and `logRetentionDays`.'); } - - this.logGroup = props.logGroup || new logs.LogGroup(this, 'LogGroup', { - retention: props.logRetention || Infinity, - }); } /** * Called when the log driver is configured on a container */ - public bind(containerDefinition: ContainerDefinition): void { + public bind(scope: Construct, containerDefinition: ContainerDefinition): LogDriverConfig { + this.logGroup = this.props.logGroup || new logs.LogGroup(scope, 'LogGroup', { + retention: this.props.logRetention || Infinity, + }); + this.logGroup.grantWrite(containerDefinition.taskDefinition.obtainExecutionRole()); - } - /** - * Return the log driver CloudFormation JSON - */ - public renderLogDriver(): CfnTaskDefinition.LogConfigurationProperty { return { logDriver: 'awslogs', options: removeEmpty({ 'awslogs-group': this.logGroup.logGroupName, 'awslogs-stream-prefix': this.props.streamPrefix, - 'awslogs-region': Stack.of(this).region, + 'awslogs-region': Stack.of(containerDefinition).region, 'awslogs-datetime-format': this.props.datetimeFormat, 'awslogs-multiline-pattern': this.props.multilinePattern, }), diff --git a/packages/@aws-cdk/aws-ecs/lib/log-drivers/log-driver.ts b/packages/@aws-cdk/aws-ecs/lib/log-drivers/log-driver.ts index bca87e561aa68..234545e4a0dd5 100644 --- a/packages/@aws-cdk/aws-ecs/lib/log-drivers/log-driver.ts +++ b/packages/@aws-cdk/aws-ecs/lib/log-drivers/log-driver.ts @@ -1,18 +1,35 @@ -import cdk = require('@aws-cdk/cdk'); +import { Construct } from '@aws-cdk/cdk'; import { ContainerDefinition } from '../container-definition'; -import { CfnTaskDefinition } from '../ecs.generated'; +import { AwsLogDriver, AwsLogDriverProps } from './aws-log-driver'; /** * Base class for log drivers */ -export abstract class LogDriver extends cdk.Construct { +export abstract class LogDriver { /** - * Return the log driver CloudFormation JSON + * Create an AWS Logs logdriver */ - public abstract renderLogDriver(): CfnTaskDefinition.LogConfigurationProperty; + public static awsLogs(props: AwsLogDriverProps): LogDriver { + return new AwsLogDriver(props); + } /** * Called when the log driver is configured on a container */ - public abstract bind(containerDefinition: ContainerDefinition): void; + public abstract bind(scope: Construct, containerDefinition: ContainerDefinition): LogDriverConfig; } + +/** + * Configuration to create a log driver from + */ +export interface LogDriverConfig { + /** + * Name of the log driver to use + */ + readonly logDriver: string; + + /** + * Log-driver specific option set + */ + readonly options?: { [key: string]: string }; +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-ecs/test/test.aws-log-driver.ts b/packages/@aws-cdk/aws-ecs/test/test.aws-log-driver.ts index f1f5b41998ea7..6f232daf7df8c 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.aws-log-driver.ts +++ b/packages/@aws-cdk/aws-ecs/test/test.aws-log-driver.ts @@ -1,20 +1,31 @@ -import { expect, haveResource } from '@aws-cdk/assert'; +import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert'; import logs = require('@aws-cdk/aws-logs'); import cdk = require('@aws-cdk/cdk'); import { Test } from 'nodeunit'; import ecs = require('../lib'); +let stack: cdk.Stack; +let td: ecs.TaskDefinition; +const image = ecs.ContainerImage.fromRegistry('test-image'); + export = { - 'create an aws log driver'(test: Test) { - // GIVEN - const stack = new cdk.Stack(); + 'setUp'(cb: () => void) { + stack = new cdk.Stack(); + td = new ecs.FargateTaskDefinition(stack, 'TaskDefinition'); + cb(); + }, + + 'create an aws log driver'(test: Test) { // WHEN - const driver = new ecs.AwsLogDriver(stack, 'Log', { - datetimeFormat: 'format', - logRetention: logs.RetentionDays.ONE_MONTH, - multilinePattern: 'pattern', - streamPrefix: 'hello' + td.addContainer('Container', { + image, + logging: new ecs.AwsLogDriver({ + datetimeFormat: 'format', + logRetention: logs.RetentionDays.ONE_MONTH, + multilinePattern: 'pattern', + streamPrefix: 'hello' + }) }); // THEN @@ -22,57 +33,64 @@ export = { RetentionInDays: logs.RetentionDays.ONE_MONTH })); - test.deepEqual( - stack.resolve(driver.renderLogDriver()), - { - logDriver: 'awslogs', - options: { - 'awslogs-group': { Ref: 'LogLogGroup427F779C' }, - 'awslogs-stream-prefix': 'hello', - 'awslogs-region': { Ref: 'AWS::Region' }, - 'awslogs-datetime-format': 'format', - 'awslogs-multiline-pattern': 'pattern' + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + LogConfiguration: { + LogDriver: 'awslogs', + Options: { + 'awslogs-group': { Ref: 'TaskDefinitionContainerLogGroup4D0A87C1' }, + 'awslogs-stream-prefix': 'hello', + 'awslogs-region': { Ref: 'AWS::Region' }, + 'awslogs-datetime-format': 'format', + 'awslogs-multiline-pattern': 'pattern' + } + } } - } - ); + ] + })); test.done(); }, 'with a defined log group'(test: Test) { // GIVEN - const stack = new cdk.Stack(); const logGroup = new logs.LogGroup(stack, 'LogGroup'); // WHEN - const driver = new ecs.AwsLogDriver(stack, 'Log', { - logGroup, - streamPrefix: 'hello' + td.addContainer('Container', { + image, + logging: new ecs.AwsLogDriver({ + logGroup, + streamPrefix: 'hello' + }) }); // THEN - test.deepEqual( - stack.resolve(driver.renderLogDriver()), - { - logDriver: 'awslogs', - options: { - 'awslogs-group': { Ref: 'LogGroupF5B46931' }, - 'awslogs-stream-prefix': 'hello', - 'awslogs-region': { Ref: 'AWS::Region' } + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + LogConfiguration: { + LogDriver: 'awslogs', + Options: { + 'awslogs-group': { Ref: 'LogGroupF5B46931' }, + 'awslogs-stream-prefix': 'hello', + 'awslogs-region': { Ref: 'AWS::Region' } + } + } } - } - ); + ] + })); test.done(); }, 'throws when specifying log retention and log group'(test: Test) { // GIVEN - const stack = new cdk.Stack(); const logGroup = new logs.LogGroup(stack, 'LogGroup'); // THEN - test.throws(() => new ecs.AwsLogDriver(stack, 'Log', { + test.throws(() => new ecs.AwsLogDriver({ logGroup, logRetention: logs.RetentionDays.FIVE_DAYS, streamPrefix: 'hello' diff --git a/packages/@aws-cdk/aws-ecs/test/test.container-definition.ts b/packages/@aws-cdk/aws-ecs/test/test.container-definition.ts index ccf9a0ee2c78d..9e0e96a67ff8e 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.container-definition.ts +++ b/packages/@aws-cdk/aws-ecs/test/test.container-definition.ts @@ -294,7 +294,7 @@ export = { taskDefinition.addContainer('cont', { image: ecs.ContainerImage.fromRegistry('test'), memoryLimitMiB: 1024, - logging: new ecs.AwsLogDriver(stack, 'Logging', { streamPrefix: 'prefix' }) + logging: new ecs.AwsLogDriver({ streamPrefix: 'prefix' }) }); // THEN @@ -304,7 +304,7 @@ export = { LogConfiguration: { LogDriver: "awslogs", Options: { - "awslogs-group": { Ref: "LoggingLogGroupC6B8E20B" }, + "awslogs-group": { Ref: "TaskDefcontLogGroup4E10DCBF" }, "awslogs-stream-prefix": "prefix", "awslogs-region": { Ref: "AWS::Region" } } @@ -319,7 +319,7 @@ export = { { Action: ["logs:CreateLogStream", "logs:PutLogEvents"], Effect: "Allow", - Resource: { "Fn::GetAtt": ["LoggingLogGroupC6B8E20B", "Arn"] } + Resource: { "Fn::GetAtt": ["TaskDefcontLogGroup4E10DCBF", "Arn"] } } ], Version: "2012-10-17" diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.expected.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.expected.json index c1643598f14ae..aec70731e4d81 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.expected.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.expected.json @@ -753,7 +753,7 @@ "LogDriver": "awslogs", "Options": { "awslogs-group": { - "Ref": "TaskLoggingLogGroupC7E938D4" + "Ref": "TaskDefTheContainerLogGroupD94C8EF5" }, "awslogs-stream-prefix": "EventDemo", "awslogs-region": { @@ -897,7 +897,7 @@ "Effect": "Allow", "Resource": { "Fn::GetAtt": [ - "TaskLoggingLogGroupC7E938D4", + "TaskDefTheContainerLogGroupD94C8EF5", "Arn" ] } @@ -983,7 +983,7 @@ ] } }, - "TaskLoggingLogGroupC7E938D4": { + "TaskDefTheContainerLogGroupD94C8EF5": { "Type": "AWS::Logs::LogGroup", "DeletionPolicy": "Retain" }, diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts index 542a63e2af2f2..e8238e82debf5 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts @@ -25,7 +25,7 @@ class EventStack extends cdk.Stack { taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromAsset(path.resolve(__dirname, 'eventhandler-image')), memoryLimitMiB: 256, - logging: new ecs.AwsLogDriver(this, 'TaskLogging', { streamPrefix: 'EventDemo' }) + logging: new ecs.AwsLogDriver({ streamPrefix: 'EventDemo' }) }); // An Rule that describes the event trigger (in this case a scheduled run) diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.expected.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.expected.json index bfb51a9cbcc2b..654ac00374d93 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.expected.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.expected.json @@ -332,7 +332,7 @@ "LogDriver": "awslogs", "Options": { "awslogs-group": { - "Ref": "TaskLoggingLogGroupC7E938D4" + "Ref": "TaskDefTheContainerLogGroupD94C8EF5" }, "awslogs-stream-prefix": "EventDemo", "awslogs-region": { @@ -477,7 +477,7 @@ "Effect": "Allow", "Resource": { "Fn::GetAtt": [ - "TaskLoggingLogGroupC7E938D4", + "TaskDefTheContainerLogGroupD94C8EF5", "Arn" ] } @@ -756,7 +756,7 @@ }, "DeletionPolicy": "Delete" }, - "TaskLoggingLogGroupC7E938D4": { + "TaskDefTheContainerLogGroupD94C8EF5": { "Type": "AWS::Logs::LogGroup", "DeletionPolicy": "Retain" }, @@ -1110,4 +1110,4 @@ "Description": "Artifact hash for asset \"aws-ecs-integ-fargate/AWS679f53fac002430cb0da5b7982bd2287/Code\"" } } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts index f807679e77dac..fba34df0564f3 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts @@ -21,7 +21,7 @@ class EventStack extends cdk.Stack { const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromAsset(path.resolve(__dirname, 'eventhandler-image')), - logging: new ecs.AwsLogDriver(this, 'TaskLogging', { streamPrefix: 'EventDemo' }) + logging: new ecs.AwsLogDriver({ streamPrefix: 'EventDemo' }) }); // A rule that describes the event trigger (in this case a scheduled run) diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.expected.json b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.expected.json index 42861dbecbadd..ab46bba0cd6f1 100644 --- a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.expected.json +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.expected.json @@ -537,7 +537,7 @@ "LogDriver": "awslogs", "Options": { "awslogs-group": { - "Ref": "TaskLoggingLogGroupC7E938D4" + "Ref": "TaskDefTheContainerLogGroupD94C8EF5" }, "awslogs-stream-prefix": "EventDemo", "awslogs-region": "test-region" @@ -671,7 +671,7 @@ "Effect": "Allow", "Resource": { "Fn::GetAtt": [ - "TaskLoggingLogGroupC7E938D4", + "TaskDefTheContainerLogGroupD94C8EF5", "Arn" ] } @@ -687,7 +687,7 @@ ] } }, - "TaskLoggingLogGroupC7E938D4": { + "TaskDefTheContainerLogGroupD94C8EF5": { "Type": "AWS::Logs::LogGroup", "DeletionPolicy": "Retain" }, diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.ts index 37e3594c30a22..0fc37744320d0 100644 --- a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.ts +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.ts @@ -28,7 +28,7 @@ const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromAsset(path.resolve(__dirname, 'eventhandler-image')), memoryLimitMiB: 256, - logging: new ecs.AwsLogDriver(stack, 'TaskLogging', { streamPrefix: 'EventDemo' }) + logging: new ecs.AwsLogDriver({ streamPrefix: 'EventDemo' }) }); // Build state machine diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.expected.json b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.expected.json index b37ca00c1fc4b..9759d6be40421 100644 --- a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.expected.json +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.expected.json @@ -126,7 +126,7 @@ "LogDriver": "awslogs", "Options": { "awslogs-group": { - "Ref": "TaskLoggingLogGroupC7E938D4" + "Ref": "TaskDefTheContainerLogGroupD94C8EF5" }, "awslogs-stream-prefix": "EventDemo", "awslogs-region": "test-region" @@ -262,7 +262,7 @@ "Effect": "Allow", "Resource": { "Fn::GetAtt": [ - "TaskLoggingLogGroupC7E938D4", + "TaskDefTheContainerLogGroupD94C8EF5", "Arn" ] } @@ -278,7 +278,7 @@ ] } }, - "TaskLoggingLogGroupC7E938D4": { + "TaskDefTheContainerLogGroupD94C8EF5": { "Type": "AWS::Logs::LogGroup", "DeletionPolicy": "Retain" }, @@ -587,4 +587,4 @@ "Description": "Artifact hash for asset \"aws-ecs-integ2/AdoptEcrRepositorydbc60defc59544bcaa5c28c95d68f62c/Code\"" } } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.ts index 37647931c92e4..40de98378b283 100644 --- a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.ts +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.ts @@ -27,7 +27,7 @@ const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef', { taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromAsset(path.resolve(__dirname, 'eventhandler-image')), memoryLimitMiB: 256, - logging: new ecs.AwsLogDriver(stack, 'TaskLogging', { streamPrefix: 'EventDemo' }) + logging: new ecs.AwsLogDriver({ streamPrefix: 'EventDemo' }) }); // Build state machine