Skip to content

Commit 026df2c

Browse files
committed
feat: add 'enableServiceConnect' for client services
1 parent fccd9c4 commit 026df2c

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
@@ -292,6 +292,11 @@ export class Service extends Construct {
292292
throw new Error(`Unknown capacity type for service ${this.id}`);
293293
}
294294

295+
// Enable service connect if requested and it has not been enabled by an extension.
296+
if (props.enableServiceConnect && !serviceProps.serviceConnectConfiguration) {
297+
this.enableServiceConnect();
298+
}
299+
295300
// Create the auto scaling target and configure target tracking policies after the service is created
296301
if (props.autoScaleTaskCount) {
297302
this.scalableTaskCount = this.ecsService.autoScaleTaskCount({
@@ -376,4 +381,18 @@ export class Service extends Construct {
376381
public enableAutoScalingPolicy() {
377382
this.autoScalingPoliciesEnabled = true;
378383
}
384+
385+
/**
386+
* This method allows a service to opt in to ECS Service Connect as a client.
387+
* If this method is not called, the service will not be able to reach other
388+
* Service Connect enabled services via their terse DNS aliases.
389+
*/
390+
public enableServiceConnect() {
391+
if (!this.environment.cluster.defaultCloudMapNamespace) {
392+
throw new Error('Environment must have a default CloudMap namespace to enable Service Connect.');
393+
}
394+
this.ecsService.enableServiceConnect({
395+
namespace: this.environment.cluster.defaultCloudMapNamespace.namespaceName,
396+
});
397+
}
379398
}

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)