Skip to content

Commit

Permalink
Add cloudmap namespace as a property of cloudmap options
Browse files Browse the repository at this point in the history
  • Loading branch information
piradeepk committed Nov 8, 2019
1 parent 142bd0e commit d63c660
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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,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.
*
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

0 comments on commit d63c660

Please sign in to comment.