AttributeError: <class 'aws_cdk.aws_elasticloadbalancingv2._IApplicationLoadBalancerProxy'>' object has no attribute 'metric_http_code_elb'

Viewed 73

I am using aws-cdk in Python & trying to reference an existing Load balancer and then make a call to Load balancer's 'metric_http_code_elb' attribute but getting below error:

AttributeError: '<class 'aws_cdk.Resource'>+<class 'aws_cdk.aws_elasticloadbalancingv2._IApplicationLoadBalancerProxy'>' object has no attribute 'metric_http_code_elb'.

Code snippet-

    load_balancer = elbv2.ApplicationLoadBalancer.from_lookup(
        self,
        "ALB",
        load_balancer_arn="arn:aws:elasticloadbalancing:ap-south-1:345396902820:loadbalancer/app/QA-Env-Public-ALB/573f88222ed64875"
    )        
    http_5xx_metric = load_balancer.metric_http_code_elb()

Complete Code- https://github.com/shiqs90/cdk-hands-on-project/blob/master/cw-dashboard/cw_dashboard/cw_dashboard_stack.py

1 Answers

Looks like this isn't supported by the CDK currently. Here is an open issue related to it: https://github.com/aws/aws-cdk/issues/10850

This comment explains it best:

This isn't a bug. FromTargetGroupAttributes does not return a ApplicationTargetGroup, which is why you can't cast it to one. It does return a IApplicationTargetGroupProxy, which is a concrete implementation of a IApplicationTargetGroup.

IApplicationTargetGroup doesn't currently support the metric* methods, as the metrics need the load balancer name, which an imported group doesn't have (at the moment).

As an alternative you could create a Metric object by adding the dimensions, namespace, etc, yourself but it is a pain.

Related