Repository: cdklabs/cdk-ecs-service-extensions (language: python)

Issue:
HttpLoadBalancerExtension should expose targetGroup as a member
Hello.

We are trying to replace a deployment of an application that uses `ApplicationLoadBalancedFargateService` with one made using `@aws-cdk-containers/ecs-service-extensions`. We have been able to configure almost everything but are a little stuck when configuring the health check for the application. Specifically, we are not able to find how to set up the health check path and port.

With our previous deployment we used :
```js
application.targetGroup.configureHealthCheck({
      path: '/ping',
      port: '3000',
      healthyHttpCodes: '200-299',
      interval: Duration.seconds(30),
      timeout: Duration.seconds(5),
      healthyThresholdCount: 5,
      unhealthyThresholdCount: 5,
    });
```

But using service extensions, specifically the `HttpLoadBalancerExtension` there is no straightforward way to access the load balancer instance nor the target group.

We tried using a `ContainerMutatingHook` but werw only able to access the docker native health check, which didn't work for our use case. 

We have a couple more ideas of how to do it but were wondering if there is a recommended way since we understand that ServiceExtensions should allow us to do the same as `ApplicationLoadBalancedFargateService` but having more freedom to configure the infrastructure.

Thanks!


Discussion / hints:
Thanks so much for bringing this to our attention. There are a number of parameter gaps in the default constructs, and I think we should be exposing the target group on the HttpLoadBalancerExtension. I'll discuss getting this prioritized.

Fix patch:
diff --git a/API.md b/API.md
index 4dc1721e..5be1b2ae 100644
--- a/API.md
+++ b/API.md
@@ -552,6 +552,7 @@ Any object.
 | <code><a href="#@aws-cdk-containers/ecs-service-extensions.Service.property.vpc">vpc</a></code> | <code>aws-cdk-lib.aws_ec2.IVpc</code> | The VPC where this service should be placed. |
 | <code><a href="#@aws-cdk-containers/ecs-service-extensions.Service.property.scalableTaskCount">scalableTaskCount</a></code> | <code>aws-cdk-lib.aws_ecs.ScalableTaskCount</code> | The scalable attribute representing task count. |
 | <code><a href="#@aws-cdk-containers/ecs-service-extensions.Service.property.ecsService">ecsService</a></code> | <code>aws-cdk-lib.aws_ecs.Ec2Service \| aws-cdk-lib.aws_ecs.FargateService</code> | The underlying ECS service that was created. |
+| <code><a href="#@aws-cdk-containers/ecs-service-extensions.Service.property.targetGroup">targetGroup</a></code> | <code>aws-cdk-lib.aws_elasticloadbalancingv2.ApplicationTargetGroup</code> | The application target group if the service has an HTTPLoadBalancerExtension. |
 
 ---
 
@@ -667,6 +668,18 @@ The underlying ECS service that was created.
 
 ---
 
+##### `targetGroup`<sup>Optional</sup> <a name="targetGroup" id="@aws-cdk-containers/ecs-service-extensions.Service.property.targetGroup"></a>
+
+```typescript
+public readonly targetGroup: ApplicationTargetGroup;
+```
+
+- *Type:* aws-cdk-lib.aws_elasticloadbalancingv2.ApplicationTargetGroup
+
+The application target group if the service has an HTTPLoadBalancerExtension.
+
+---
+
 
 ## Structs <a name="Structs" id="Structs"></a>
 
diff --git a/README.md b/README.md
index f2f754c7..ac421daa 100644
--- a/README.md
+++ b/README.md
@@ -220,6 +220,24 @@ const nameService = new Service(this, 'name', {
   taskRole,
 });
 ```
+## Configure Custom Health Check
+
+When you add an HTTPLoadBalancerExtension, you can customize the health checks by accessing the targetGroup field on the service.
+
+```ts
+const service = new Service(stack, 'my-service', {
+  environment,
+  serviceDescription,
+  autoScaleTaskCount: {
+    maxTaskCount: 5,
+  },
+});
+  
+service.targetGroup.configureHealthCheck({
+  path: '/',
+  port: '80',
+});
+```
 
 ## Task Auto-Scaling
 
diff --git a/src/extensions/http-load-balancer.ts b/src/extensions/http-load-balancer.ts
index 768d2b0e..8249878b 100644
--- a/src/extensions/http-load-balancer.ts
+++ b/src/extensions/http-load-balancer.ts
@@ -65,6 +65,7 @@ export class HttpLoadBalancerExtension extends ServiceExtension {
       port: 80,
       targets: [service],
     });
+    this.parentService.targetGroup = targetGroup;
 
     if (this.requestsPerTarget) {
       if (!this.parentService.scalableTaskCount) {
@@ -72,7 +73,7 @@ export class HttpLoadBalancerExtension extends ServiceExtension {
       }
       this.parentService.scalableTaskCount.scaleOnRequestCount(`${this.parentService.id}-target-request-count-${this.requestsPerTarget}`, {
         requestsPerTarget: this.requestsPerTarget,
-        targetGroup,
+        targetGroup: this.parentService.targetGroup,
       });
       this.parentService.enableAutoScalingPolicy();
     }
diff --git a/src/service.ts b/src/service.ts
index 3fb87315..37a9f96a 100644
--- a/src/service.ts
+++ b/src/service.ts
@@ -1,5 +1,6 @@
 import * as ec2 from 'aws-cdk-lib/aws-ec2';
 import * as ecs from 'aws-cdk-lib/aws-ecs';
+import { ApplicationTargetGroup } from 'aws-cdk-lib/aws-elasticloadbalancingv2';
 import * as iam from 'aws-cdk-lib/aws-iam';
 import { Construct } from 'constructs';
 import { IEnvironment } from './environment';
@@ -128,6 +129,11 @@ export class Service extends Construct {
    */
   public readonly scalableTaskCount?: ecs.ScalableTaskCount;
 
+  /**
+   * The application target group if the service has an HTTPLoadBalancerExtension.
+   */
+  public targetGroup?: ApplicationTargetGroup;
+
   /**
    * The flag to track if auto scaling policies have been configured
    * for the service.


Test patch:
diff --git a/test/http-load-balancer.test.ts b/test/http-load-balancer.test.ts
index 2c5e7440..f9e7e983 100644
--- a/test/http-load-balancer.test.ts
+++ b/test/http-load-balancer.test.ts
@@ -87,7 +87,7 @@ describe('http load balancer', () => {
 
     serviceDescription.add(new HttpLoadBalancerExtension({ requestsPerTarget: 100 }));
 
-    new Service(stack, 'my-service', {
+    const service = new Service(stack, 'my-service', {
       environment,
       serviceDescription,
       autoScaleTaskCount: {
@@ -96,6 +96,7 @@ describe('http load balancer', () => {
     });
 
     // THEN
+    expect(service.targetGroup).not.toBeUndefined();
     Template.fromStack(stack).hasResourceProperties('AWS::ApplicationAutoScaling::ScalableTarget', {
       MaxCapacity: 5,
       MinCapacity: 1,