@@ -221,6 +221,13 @@ export abstract class BaseService extends Resource
221
221
}
222
222
}
223
223
224
+ /**
225
+ * The CloudMap service created for this service, if any.
226
+ */
227
+ public get cloudMapService ( ) : cloudmap . IService | undefined {
228
+ return this . cloudmapService ;
229
+ }
230
+
224
231
/**
225
232
* This method is called to attach this service to an Application Load Balancer.
226
233
*
@@ -303,6 +310,68 @@ export abstract class BaseService extends Resource
303
310
} ) ;
304
311
}
305
312
313
+ /**
314
+ * Enable CloudMap service discovery for the service
315
+ *
316
+ * @returns The created CloudMap service
317
+ */
318
+ public enableCloudMap ( options : CloudMapOptions ) : cloudmap . Service {
319
+ const sdNamespace = this . cluster . defaultCloudMapNamespace ;
320
+ if ( sdNamespace === undefined ) {
321
+ throw new Error ( "Cannot enable service discovery if a Cloudmap Namespace has not been created in the cluster." ) ;
322
+ }
323
+
324
+ // Determine DNS type based on network mode
325
+ const networkMode = this . taskDefinition . networkMode ;
326
+ if ( networkMode === NetworkMode . NONE ) {
327
+ throw new Error ( "Cannot use a service discovery if NetworkMode is None. Use Bridge, Host or AwsVpc instead." ) ;
328
+ }
329
+
330
+ // Bridge or host network mode requires SRV records
331
+ let dnsRecordType = options . dnsRecordType ;
332
+
333
+ if ( networkMode === NetworkMode . BRIDGE || networkMode === NetworkMode . HOST ) {
334
+ if ( dnsRecordType === undefined ) {
335
+ dnsRecordType = cloudmap . DnsRecordType . SRV ;
336
+ }
337
+ if ( dnsRecordType !== cloudmap . DnsRecordType . SRV ) {
338
+ throw new Error ( "SRV records must be used when network mode is Bridge or Host." ) ;
339
+ }
340
+ }
341
+
342
+ // Default DNS record type for AwsVpc network mode is A Records
343
+ if ( networkMode === NetworkMode . AWS_VPC ) {
344
+ if ( dnsRecordType === undefined ) {
345
+ dnsRecordType = cloudmap . DnsRecordType . A ;
346
+ }
347
+ }
348
+
349
+ // If the task definition that your service task specifies uses the AWSVPC network mode and a type SRV DNS record is
350
+ // used, you must specify a containerName and containerPort combination
351
+ const containerName = dnsRecordType === cloudmap . DnsRecordType . SRV ? this . taskDefinition . defaultContainer ! . containerName : undefined ;
352
+ const containerPort = dnsRecordType === cloudmap . DnsRecordType . SRV ? this . taskDefinition . defaultContainer ! . containerPort : undefined ;
353
+
354
+ const cloudmapService = new cloudmap . Service ( this , 'CloudmapService' , {
355
+ namespace : sdNamespace ,
356
+ name : options . name ,
357
+ dnsRecordType : dnsRecordType ! ,
358
+ customHealthCheck : { failureThreshold : options . failureThreshold || 1 }
359
+ } ) ;
360
+
361
+ const serviceArn = cloudmapService . serviceArn ;
362
+
363
+ // add Cloudmap service to the ECS Service's serviceRegistry
364
+ this . addServiceRegistry ( {
365
+ arn : serviceArn ,
366
+ containerName,
367
+ containerPort
368
+ } ) ;
369
+
370
+ this . cloudmapService = cloudmapService ;
371
+
372
+ return cloudmapService ;
373
+ }
374
+
306
375
/**
307
376
* This method returns the specified CloudWatch metric name for this service.
308
377
*/
@@ -430,66 +499,6 @@ export abstract class BaseService extends Resource
430
499
this . serviceRegistries . push ( sr ) ;
431
500
}
432
501
433
- /**
434
- * Enable CloudMap service discovery for the service
435
- */
436
- private enableCloudMap ( options : CloudMapOptions ) : cloudmap . Service {
437
- const sdNamespace = this . cluster . defaultCloudMapNamespace ;
438
- if ( sdNamespace === undefined ) {
439
- throw new Error ( "Cannot enable service discovery if a Cloudmap Namespace has not been created in the cluster." ) ;
440
- }
441
-
442
- // Determine DNS type based on network mode
443
- const networkMode = this . taskDefinition . networkMode ;
444
- if ( networkMode === NetworkMode . NONE ) {
445
- throw new Error ( "Cannot use a service discovery if NetworkMode is None. Use Bridge, Host or AwsVpc instead." ) ;
446
- }
447
-
448
- // Bridge or host network mode requires SRV records
449
- let dnsRecordType = options . dnsRecordType ;
450
-
451
- if ( networkMode === NetworkMode . BRIDGE || networkMode === NetworkMode . HOST ) {
452
- if ( dnsRecordType === undefined ) {
453
- dnsRecordType = cloudmap . DnsRecordType . SRV ;
454
- }
455
- if ( dnsRecordType !== cloudmap . DnsRecordType . SRV ) {
456
- throw new Error ( "SRV records must be used when network mode is Bridge or Host." ) ;
457
- }
458
- }
459
-
460
- // Default DNS record type for AwsVpc network mode is A Records
461
- if ( networkMode === NetworkMode . AWS_VPC ) {
462
- if ( dnsRecordType === undefined ) {
463
- dnsRecordType = cloudmap . DnsRecordType . A ;
464
- }
465
- }
466
-
467
- // If the task definition that your service task specifies uses the AWSVPC network mode and a type SRV DNS record is
468
- // used, you must specify a containerName and containerPort combination
469
- const containerName = dnsRecordType === cloudmap . DnsRecordType . SRV ? this . taskDefinition . defaultContainer ! . containerName : undefined ;
470
- const containerPort = dnsRecordType === cloudmap . DnsRecordType . SRV ? this . taskDefinition . defaultContainer ! . containerPort : undefined ;
471
-
472
- const cloudmapService = new cloudmap . Service ( this , 'CloudmapService' , {
473
- namespace : sdNamespace ,
474
- name : options . name ,
475
- dnsRecordType : dnsRecordType ! ,
476
- customHealthCheck : { failureThreshold : options . failureThreshold || 1 }
477
- } ) ;
478
-
479
- const serviceArn = cloudmapService . serviceArn ;
480
-
481
- // add Cloudmap service to the ECS Service's serviceRegistry
482
- this . addServiceRegistry ( {
483
- arn : serviceArn ,
484
- containerName,
485
- containerPort
486
- } ) ;
487
-
488
- this . cloudmapService = cloudmapService ;
489
-
490
- return cloudmapService ;
491
- }
492
-
493
502
/**
494
503
* Return the default grace period when load balancers are configured and
495
504
* healthCheckGracePeriod is not already set
0 commit comments