diff --git a/packages/@aws-cdk/aws-ecs/lib/cluster.ts b/packages/@aws-cdk/aws-ecs/lib/cluster.ts index 5c2bf1c45c024..cf7fc1877f1df 100644 --- a/packages/@aws-cdk/aws-ecs/lib/cluster.ts +++ b/packages/@aws-cdk/aws-ecs/lib/cluster.ts @@ -40,6 +40,13 @@ export interface ClusterProps { * @default - no EC2 capacity will be added, you can use `addCapacity` to add capacity later. */ readonly capacity?: AddCapacityOptions; + + /** + * If true CloudWatch Container Insights will be enabled for the cluster + * + * @default - Container Insights will be disabled for this cluser. + */ + readonly containerInsights?: boolean; } /** @@ -96,8 +103,12 @@ export class Cluster extends Resource implements ICluster { physicalName: props.clusterName, }); + const containerInsights = props.containerInsights !== undefined ? props.containerInsights : false; + const clusterSettings = containerInsights ? [{name: "containerInsights", value: "enabled"}] : undefined; + const cluster = new CfnCluster(this, 'Resource', { clusterName: this.physicalName, + clusterSettings, }); this.clusterArn = this.getResourceArnAttribute(cluster.attrArn, { diff --git a/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts b/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts index 09576362eb60d..8694f66567647 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts +++ b/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts @@ -1,4 +1,4 @@ -import { countResources, expect, haveResource } from '@aws-cdk/assert'; +import { countResources, expect, haveResource, ResourcePart } from '@aws-cdk/assert'; import * as ec2 from '@aws-cdk/aws-ec2'; import * as cloudmap from '@aws-cdk/aws-servicediscovery'; import * as cdk from '@aws-cdk/core'; @@ -1313,4 +1313,46 @@ export = { // THEN test.done(); }, + + "enable container insights"(test: Test) { + // GIVEN + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + + new ecs.Cluster(stack, 'EcsCluster', { containerInsights: true }); + + // THEN + expect(stack).to(haveResource("AWS::ECS::Cluster", { + ClusterSettings: [ + { + Name: "containerInsights", + Value: "enabled" + } + ] + }, ResourcePart.Properties)); + + test.done(); + }, + + "default container insights undefined"(test: Test) { + // GIVEN + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + + new ecs.Cluster(stack, 'EcsCluster'); + + // THEN + const assembly = app.synth(); + const stackAssembly = assembly.getStackByName(stack.stackName); + const template = stackAssembly.template; + + test.equal( + template.Resources.EcsCluster97242B84.Properties === undefined || + template.Resources.EcsCluster97242B84.Properties.ClusterSettings === undefined, + true, + "ClusterSettings should not be defined" + ); + + test.done(); + }, };