How to add (configure) load balancer health check path in the following AWS CDK (python) using class ApplicationLoadBalancedFargateService (construct)?

0

I tried using class ApplicationLoadBalancedFargateService (construct) since I started using CDK recently I end up with errors. I want to configure the following, path="/grapevine/index.jsp" as load balancer health check path into the following python based CDK script. Kindly help me to fix it.

# sudo apt-get install python3-pip
# sudo pip3 install virtualenv 
# virtualenv venv 
# source .venv/bin/activate
# python -m pip install -r requirements.txt

# from aws_cdk import core as cdk
# from aws_cdk import (core, aws_ec2 as ec2, aws_ecs as ecs,aws_ecs_patterns as ecs_patterns)
from aws_cdk import (
    aws_autoscaling as autoscaling,
    aws_ec2 as ec2,
    aws_ecs as ecs,
    aws_ecr as ecr,
    aws_iam as iam,
    aws_route53 as route53,
    aws_certificatemanager as certificatemanager,
    aws_elasticloadbalancingv2 as elasticloadbalancingv2,
    aws_ecs_patterns as ecs_patterns,
    App, CfnOutput, Stack
)
from constructs import Construct


class AwsCdkNginxStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        vpc = ec2.Vpc.from_lookup(self, 'VPC', vpc_id='vpc-07aefcbe9fe27a33a')
        domain_zone=route53.HostedZone.from_lookup(self, "Zone", domain_name="dev.devops.directwines.com")
        cluster = ecs.Cluster(self, "NGINX-Cluster", vpc=vpc)
        execution_role=iam.Role(self,"dwaws-ecs-execution-role", 
                           assumed_by=iam.ServicePrincipal("ecs-tasks.amazonaws.com"), 
                           managed_policies=[
                                iam.ManagedPolicy.from_aws_managed_policy_name("AmazonEC2ContainerRegistryFullAccess"),
                                iam.ManagedPolicy.from_aws_managed_policy_name("AmazonECS_FullAccess")
                               ],
         )
        ecs_patterns.ApplicationLoadBalancedFargateService(self, "NGINXFargateService",
            cluster=cluster,            # Required
            cpu=256,                    # Default is 256
            desired_count=1,            # Default is 1
            target_protocol=elasticloadbalancingv2.ApplicationProtocol.HTTPS, #Required for Port 443 in container
            task_image_options=ecs_patterns.ApplicationLoadBalancedTaskImageOptions(
                container_port=443,
                image=ecs.ContainerImage.from_registry("954719641350.dkr.ecr.eu-west-1.amazonaws.com/ecs-nginx-repository"),
                execution_role=execution_role),
            memory_limit_mib=512,      # Default is 512
            public_load_balancer=True,      
            protocol=elasticloadbalancingv2.ApplicationProtocol.HTTPS, #Required for Port 443 in container
            #certificate = certificatemanager.Certificate.from_certificate_arn(self, 'certificate','arn:aws:acm:eu-west-1:308484256524:certificate/7b136d15-3a21-4213-a670-8cf892b7218c'))
            domain_name="nginx-cdk.dev.devops.directwines.com",
            domain_zone=domain_zone),
2 Answers
1

Hello.

To configure the health check path for the load balancer in the ApplicationLoadBalancedFargateService construct, you'll need to make use of the health_check attribute inside the listener property of the construct.

domain_name="nginx-cdk.dev.devops.directwines.com",
domain_zone=domain_zone,
listener_port=443,
health_check=elasticloadbalancingv2.HealthCheck(
    path="/grapevine/index.jsp",
    # You can specify other health check parameters here, for example:
    # interval=core.Duration.seconds(30),
    # timeout=core.Duration.seconds(5),
    # healthy_http_codes="200"
)

This will configure the health check of the ALB to use the path /grapevine/index.jsp.

Regards, Andrii

profile picture
EXPERT
answered 7 months ago
profile picture
EXPERT
reviewed 11 days ago
  • Hi Andri, I went on holidays, resumed back today. Thanks you for helping me. but I end up with the below error, when I added the above suggested code change by you and given cdk ls command in my wsl. Please help me on this.

    <(.venv) gowtham@LAPTIND04486:~/git/aws-cdk-nginx-grapevine/app-cdk$ cdk ls Traceback (most recent call last): File "/home/gowtham/git/aws-cdk-nginx-grapevine/app-cdk/app.py", line 19, in <module> nginx_stack = NginxCdkStack( File "/home/gowtham/.local/lib/python3.10/site-packages/jsii/_runtime.py", line 112, in call inst = super().call(*args, **kwargs) File "/home/gowtham/git/aws-cdk-nginx-grapevine/app-cdk/app_cdk/nginx_cdk_stack.py", line 44, in init ecs_patterns.ApplicationLoadBalancedFargateService(self, "NGINXFargateService", File "/home/gowtham/.local/lib/python3.10/site-packages/jsii/_runtime.py", line 112, in call inst = super().call(*args, **kwargs) TypeError: ApplicationLoadBalancedFargateService.init() got an unexpected keyword argument 'health_check_path'

    Subprocess exited with error 1>

0

The health_check within the attribute ApplicationLoadBalancedFargateService.health_check is for the container. You want the health check of the load balancer, and need to do the below:

  1. Configure your ApplicationLoadBalancedFargateService as usual:
service = aws_ecs_patterns.ApplicationLoadBalancedFargateService(self, Service, ...)

And, then set the load balancer health check:

service.target_group.configure_health_check(
    path="/grapevine/index.jsp",
)

answered 6 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions