Skip to content

Commit 817da2b

Browse files
committed
feat: add 'enableServiceConnect' for client services
1 parent 8f2963b commit 817da2b

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/service.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ export class Service extends Construct {
298298
throw new Error(`Unknown capacity type for service ${this.id}`);
299299
}
300300

301+
// Enable service connect if requested and it has not been enabled by an extension.
302+
if (props.enableServiceConnect && !serviceProps.serviceConnectConfiguration) {
303+
this.enableServiceConnect();
304+
}
305+
301306
// Create the auto scaling target and configure target tracking policies after the service is created
302307
if (props.autoScaleTaskCount) {
303308
this.scalableTaskCount = this.ecsService.autoScaleTaskCount({
@@ -382,4 +387,18 @@ export class Service extends Construct {
382387
public enableAutoScalingPolicy() {
383388
this.autoScalingPoliciesEnabled = true;
384389
}
390+
391+
/**
392+
* This method allows a service to opt in to ECS Service Connect as a client.
393+
* If this method is not called, the service will not be able to reach other
394+
* Service Connect enabled services via their terse DNS aliases.
395+
*/
396+
public enableServiceConnect() {
397+
if (!this.environment.cluster.defaultCloudMapNamespace) {
398+
throw new Error('Environment must have a default CloudMap namespace to enable Service Connect.');
399+
}
400+
this.ecsService.enableServiceConnect({
401+
namespace: this.environment.cluster.defaultCloudMapNamespace.namespaceName,
402+
});
403+
}
385404
}

test/aliased-port.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,65 @@ describe('aliased port', () => {
117117
},
118118
});
119119
});
120+
121+
test('when enabling service connect on a client service', () => {
122+
serviceDescription.add(new Container({
123+
cpu: 256,
124+
memoryMiB: 512,
125+
trafficPort: 80,
126+
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
127+
}));
128+
129+
environment.addDefaultCloudMapNamespace({
130+
name: environment.id,
131+
});
132+
133+
const svc = new Service(stack, 'my-service', {
134+
environment,
135+
serviceDescription,
136+
});
137+
svc.enableServiceConnect();
138+
139+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
140+
ServiceConnectConfiguration: {
141+
Enabled: true,
142+
Namespace: 'production',
143+
},
144+
});
145+
});
146+
147+
test('cannot enable service connect on a cluster with no namespace', () => {
148+
serviceDescription.add(new Container({
149+
cpu: 256,
150+
memoryMiB: 512,
151+
trafficPort: 80,
152+
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
153+
}));
154+
const svc = new Service(stack, 'my-service', {
155+
environment,
156+
serviceDescription,
157+
});
158+
159+
expect(() => {
160+
svc.enableServiceConnect();
161+
}).toThrow('Environment must have a default CloudMap namespace to enable Service Connect.');
162+
});
163+
164+
test('cannot add two Aliased Port extensions', () => {
165+
serviceDescription.add(new Container({
166+
cpu: 256,
167+
memoryMiB: 512,
168+
trafficPort: 80,
169+
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
170+
}));
171+
serviceDescription.add(new AliasedPortExtension({
172+
alias: 'name',
173+
}));
174+
expect(() => {
175+
serviceDescription.add(new AliasedPortExtension({
176+
alias: 'name2',
177+
aliasPort: 8080,
178+
}));
179+
}).toThrow('An extension called aliasedPort has already been added');
180+
});
120181
});

0 commit comments

Comments
 (0)