question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[elbv2] support metric methods on IApplicationTargetGroup

See original GitHub issue

ApplicationTargetGroup.FromTargetGroupAttributes returns an IApplicationTargetGroupProxy instead of IApplicationTargetGroup, as outlined in the documentation. This class cannot be cast to ApplicationTargetGroup

Reproduction Steps

var targetGroup = (ApplicationTargetGroup) ApplicationTargetGroup.FromTargetGroupAttributes(this, "TargetGroup", new TargetGroupAttributes {
    TargetGroupArn = props.TargetGroupArn,
    LoadBalancerArns = props.LoadBalancerArn
});
_metric = targetGroup.MetricHttpCodeTarget(props.StatusCode);

What did you expect to happen?

IApplicationTargetGroup is cast to ApplicationTargetGroup and returns HttpCode metric

What actually happened?

On cdk synth:

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'Amazon.CDK.AWS.ElasticLoadBalancingV2.IApplicationTargetGroupProxy' to type 'Amazon.CDK.AWS.ElasticLoadBalancingV2.ApplicationTargetGroup'.

Environment

  • CLI Version : 1.67.0
  • Framework Version: 1.67.0
  • Node.js Version: v12.19.0
  • OS : OSX 10.14.6
  • Language (Version): .NET Core 3.1.200

Other


This is 🐛 Bug Report

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
njlynchcommented, Oct 19, 2020

We can convert this to a feature request for exposing the metric methods for IApplicationTargetGroup. I can’t comment on or commit to a timeline when these will be added; however, pull requests are always appreciated!

2reactions
ysfarancommented, Jul 19, 2022

In my case we separated resources from deployment. This means in our CI/CD pipeline we first have a job that creates all resources (ECS Cluster, ALB, TargetGroups, DynamoDB’s etc…). Secondly all ECS services are created and deployed to the ECS cluster.

Therefor we import the TargetGroup via ApplicationTargetGroup.fromTargetGroupAttributes during the second (deployment) step:

import elbv2 = require("aws-cdk-lib/aws-elasticloadbalancingv2")

const targetGroup = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(scope, `TargetGroup`, {
  targetGroupArn: myArn
})

And register the service + add a scaling metric for requests (scaleOnRequestCount):

import ecs = require("aws-cdk-lib/aws-ecs")

const service = new ecs.Ec2Service(this, "Service", { /* ... */ })

targetGroup.addTarget(service)

const scaling = service.autoScaleTaskCount({
  minCapacity: 1,
  maxCapacity: 4,
})

scaling.scaleOnRequestCount("RequestScaling", {
  requestsPerTarget: 1200,
  //  ERROR: Type 'IApplicationTargetGroup' is not assignable to type 'ApplicationTargetGroup'.
  targetGroup: targetGroup, 
})

Unfortunately ApplicationTargetGroup.fromTargetGroupAttributes only returns IApplicationTargetGroup and not ApplicationTargetGroup which ultimately leads to the error as seen in the snippet.

I also tried to cast it but it will fail during runtime then:

//  ERROR: Invalid resource label 'undefined/undefined' for predefined metric type ALBRequestCountPerTarget
targetGroup: props.targetGroup as elbv2.ApplicationTargetGroup

Is there any clean way to get around this as of now? How to get the actual ApplicationTargetGroup instance?

Read more comments on GitHub >

github_iconTop Results From Across the Web

aws-cdk/aws-elasticloadbalancingv2 module
'internetFacing' is 'false' // by default, which creates an internal load balancer. const lb = new elbv2.ApplicationLoadBalancer(this, 'LB', { vpc, ...
Read more >
AttributeError: <class 'aws_cdk.aws_elasticloadbalancingv2 ...
IApplicationTargetGroup doesn't currently support the metric* methods, as the metrics need the load balancer name, which an imported group ...
Read more >
@aws-cdk/aws-elasticloadbalancingv2 - Package Manager
acm: DaysToExpiry metric (#15424 (ff044ed); appmesh: add support for shared Meshes ... elbv2: explicitly implement IApplicationTargetGroup (#1806 (828a2d7), ...
Read more >
awselasticloadbalancingv2 - Go Packages
type IApplicationLoadBalancerTarget; type IApplicationTargetGroup ... Metric // The number of load balancer capacity units (LCU) used by ...
Read more >
AWS ELBv2 Best Practices - Trend Micro
Ensure ELBv2 load balancers have secure and valid security groups. ... Ensure that support for gRPC protocol is enabled for Application Load Balancers ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found