Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecs): add cloudMapNamespace as a property of cloudMapOptions #4890

Merged
merged 4 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down Expand Up @@ -663,10 +663,17 @@ export interface CloudMapOptions {
*/
readonly name?: string,

/**
* The service discovery namespace for the Cloud Map service to attach to the ECS service.
*
* @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,

Expand Down
68 changes: 68 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
67 changes: 67 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down