Skip to content

aws_elasticloadbalancingv2: Can't interact with the Network or Application Target Group from EcsTarget #33881

Open
@BwL1289

Description

@BwL1289

Describe the feature

Maybe I've missed something, but if you do something like the following:

ecs_target = ecs.EcsTarget(
    container_name="container_name",
    container_port=80,
    new_target_group_id="TargetGroup80",
    listener=ecs.ListenerConfig.network_listener(
        listener=nlb_listener_80,
        port=80,
        deregistration_delay=Duration.seconds(10),
    ),
)

# Register the target with the target group
nlb_flask_fargate_service.service.register_load_balancer_targets(ecs_target)

There's no way to further interact with the lb.NetworkTargetGroup or lb.ApplicationTargetGroup.

If you try to import the TargetGroup via the following, you need the full ARN which the above EcsTarget construct doesn't give you (just the new_group_id)

def import_80_target_group(self) -> None:
    target_group_80 = lb.NetworkTargetGroup.from_target_group_attributes(
        self,
        "TargetGroup80Imported",
        target_group_arn=ecs_target.new_target_group_id,
        load_balancer_arns=nlb_flask_fargate_service.load_balancer.load_balancer_arn,
)

Use Case

Add custom metrics / monitoring / alarms / scaling to the target group:

target_group_80.metrics.custom(metric_name="RequestCount", period=Duration.minutes(1)).create_alarm(
    self,
    "HighNumRequestsAlarm",
     evaluation_periods=1,
     threshold=20,
     comparison_operator=cw.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
     alarm_description="ECS Scale up on Number of Incoming Requests if number of requests is greater than 20 in a minute",
)
scaling.scale_on_metric(
    "TargetResponseTimeScaling",
    metric=self.flask_nlb_fargate_service.target_group.metrics.target_response_time(),
    scaling_steps=[
        appscaling.ScalingInterval(lower=0, change=-1),  # Decreases tasks if requests are low.
        appscaling.ScalingInterval(lower=10.0, change=+1),  # Scale out if response time > 10s, add a task
       appscaling.ScalingInterval(lower=20.0, change=+2),  # Scale out more if > 20s, add 2 tasks
],
    adjustment_type=appscaling.AdjustmentType.CHANGE_IN_CAPACITY,
)

Proposed Solution

Expose the underlying TargetGroup construct via a property on EcsTarget or at least the full ARN.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.180.0

Environment details (OS name and version, etc.)

MacOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions