From d63c660dab7578f8ac30eab7b41f18921438c18c Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Fri, 25 Oct 2019 10:56:21 -0700 Subject: [PATCH 1/3] Add cloudmap namespace as a property of cloudmap options --- .../@aws-cdk/aws-ecs/lib/base/base-service.ts | 9 ++- .../aws-ecs/test/ec2/test.ec2-service.ts | 68 +++++++++++++++++++ .../test/fargate/test.fargate-service.ts | 67 ++++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts index 168064d6b7db9..32676628fc5c5 100644 --- a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts @@ -456,7 +456,7 @@ export abstract class BaseService extends Resource * @returns The created CloudMap service */ public enableCloudMap(options: CloudMapOptions): cloudmap.Service { - const sdNamespace = this.cluster.defaultCloudMapNamespace; + const sdNamespace = options.cloudMapNamespace !== undefined ? options.cloudMapNamespace : this.cluster.defaultCloudMapNamespace; if (sdNamespace === undefined) { throw new Error("Cannot enable service discovery if a Cloudmap Namespace has not been created in the cluster."); } @@ -663,6 +663,13 @@ export interface CloudMapOptions { */ readonly name?: string, + /** + * The service discovery namespace created in this cluster + * + * @default - no service discovery namespace created + */ + readonly cloudMapNamespace?: cloudmap.INamespace; + /** * The DNS record type that you want AWS Cloud Map to create. The supported record types are A or SRV. * diff --git a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts index aa43f8cbdfe10..635a62a49ee92 100644 --- a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts +++ b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts @@ -51,6 +51,74 @@ export = { test.done(); }, + "with custom cloudmap namespace"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); + + const container = taskDefinition.addContainer("web", { + image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), + memoryLimitMiB: 512 + }); + container.addPortMappings({ containerPort: 8000 }); + + const cloudMapNamespace = new cloudmap.PrivateDnsNamespace(stack, 'TestCloudMapNamespace', { + name: "scorekeep.com", + vpc, + }); + + new ecs.Ec2Service(stack, "Ec2Service", { + cluster, + taskDefinition, + cloudMapOptions: { + name: "myApp", + failureThreshold: 20, + cloudMapNamespace, + }, + }); + + // THEN + expect(stack).to(haveResource('AWS::ServiceDiscovery::Service', { + DnsConfig: { + DnsRecords: [ + { + TTL: 60, + Type: "SRV" + } + ], + NamespaceId: { + 'Fn::GetAtt': [ + 'TestCloudMapNamespace1FB9B446', + 'Id' + ] + }, + RoutingPolicy: 'MULTIVALUE' + }, + HealthCheckCustomConfig: { + FailureThreshold: 20 + }, + Name: "myApp", + NamespaceId: { + 'Fn::GetAtt': [ + 'TestCloudMapNamespace1FB9B446', + 'Id' + ] + } + })); + + expect(stack).to(haveResource('AWS::ServiceDiscovery::PrivateDnsNamespace', { + Name: "scorekeep.com", + Vpc: { + Ref: "MyVpcF9F0CA6F" + } + })); + + test.done(); + }, + "with all properties set"(test: Test) { // GIVEN const stack = new cdk.Stack(); diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts b/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts index 96d72e1f568d0..a87af7dcdbf39 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts @@ -83,6 +83,73 @@ export = { test.done(); }, + "with custom cloudmap namespace"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef'); + + const container = taskDefinition.addContainer("web", { + image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), + memoryLimitMiB: 512 + }); + container.addPortMappings({ containerPort: 8000 }); + + const cloudMapNamespace = new cloudmap.PrivateDnsNamespace(stack, 'TestCloudMapNamespace', { + name: "scorekeep.com", + vpc, + }); + + new ecs.FargateService(stack, "FargateService", { + cluster, + taskDefinition, + cloudMapOptions: { + name: "myApp", + failureThreshold: 20, + cloudMapNamespace, + }, + }); + + // THEN + expect(stack).to(haveResource('AWS::ServiceDiscovery::Service', { + DnsConfig: { + DnsRecords: [ + { + TTL: 60, + Type: "A" + } + ], + NamespaceId: { + 'Fn::GetAtt': [ + 'TestCloudMapNamespace1FB9B446', + 'Id' + ] + }, + RoutingPolicy: 'MULTIVALUE' + }, + HealthCheckCustomConfig: { + FailureThreshold: 20 + }, + Name: "myApp", + NamespaceId: { + 'Fn::GetAtt': [ + 'TestCloudMapNamespace1FB9B446', + 'Id' + ] + } + })); + + expect(stack).to(haveResource('AWS::ServiceDiscovery::PrivateDnsNamespace', { + Name: "scorekeep.com", + Vpc: { + Ref: "MyVpcF9F0CA6F" + } + })); + + test.done(); + }, + "with all properties set"(test: Test) { // GIVEN const stack = new cdk.Stack(); From 16a6abf715050059127d8b298385eebbfa1162cc Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Thu, 7 Nov 2019 10:11:21 -0800 Subject: [PATCH 2/3] Address pr feedback --- packages/@aws-cdk/aws-ecs/lib/base/base-service.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts index 32676628fc5c5..0ae2dacecb485 100644 --- a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts @@ -664,16 +664,16 @@ export interface CloudMapOptions { readonly name?: string, /** - * The service discovery namespace created in this cluster + * The service discovery namespace created in this service. * - * @default - no service discovery namespace created + * @default - the defaultCloudMapNamespace associated to the cluster */ readonly cloudMapNamespace?: cloudmap.INamespace; /** * The DNS record type that you want AWS Cloud Map to create. The supported record types are A or SRV. * - * @default: A + * @default DnsRecordType.A */ readonly dnsRecordType?: cloudmap.DnsRecordType.A | cloudmap.DnsRecordType.SRV, From 5bff7b8fd3de5ba29c3841c4e244fb7cdde4f544 Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Fri, 8 Nov 2019 09:49:32 -0800 Subject: [PATCH 3/3] Address feedback --- packages/@aws-cdk/aws-ecs/lib/base/base-service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts index 0ae2dacecb485..87ba4f19f86da 100644 --- a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts @@ -664,7 +664,7 @@ export interface CloudMapOptions { readonly name?: string, /** - * The service discovery namespace created in this service. + * The service discovery namespace for the Cloud Map service to attach to the ECS service. * * @default - the defaultCloudMapNamespace associated to the cluster */