Home > Software engineering >  Set security groups to an ALB aws
Set security groups to an ALB aws

Time:10-12

I am trying to set a few security groups to my ALB. This is code I wrote:

def set_alb_security_group(cfd_sg):
    global ALB_ARN
    
    client = boto3.client('elb', 'eu-central-1')
    result = client.apply_security_groups_to_load_balancer(
        LoadBalancerName='Jenkins-ELB',
        SecurityGroups=['sg-088257e3c09954802', 'sg-0f99e3a27f7ceb393', 'sg-0c262b4c866c7258a']
    )
    logging.info(result)

Unfortunately it doesn't work and here it is the code I get:

{
  "errorMessage": "An error occurred (LoadBalancerNotFound) when calling the ApplySecurityGroupsToLoadBalancer operation: There is no ACTIVE Load Balancer named 'Jenkins-ELB'",
  "errorType": "AccessPointNotFoundException",
  "stackTrace": [
    "  File \"/var/task/lambda.py\", line 44, in lambda_handler\n    update_security_groups(cf_ranges)\n",
    "  File \"/var/task/lambda.py\", line 58, in update_security_groups\n    rangeToUpdate = get_security_groups_for_update(client, True)\n",
    "  File \"/var/task/lambda.py\", line 245, in get_security_groups_for_update\n    return create_security_groups(client, response)\n",
    "  File \"/var/task/lambda.py\", line 227, in create_security_groups\n    set_alb_security_group(created_sgs)\n",
    "  File \"/var/task/lambda.py\", line 279, in set_alb_security_group\n    result = client.apply_security_groups_to_load_balancer(\n",
    "  File \"/var/runtime/botocore/client.py\", line 386, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 705, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n"
  ]
}

I am 100% sure I have the load balancer with exact this name. What I am doing wrong here? Thanks!

CodePudding user response:

You are using "elb" as a client, which is used just for "Classic" load balancers. Since you're using ALB, you should use "elbv2" as a client

CodePudding user response:

Your client is set up to work with Classic Load Balancers as you're using elb as the client type, not Application Load Balancers.

client = boto3.client('elb', 'eu-central-1')

Documentation:

This reference covers the 2012-06-01 API, which supports Classic Load Balancers


To create a client which works with Application Load Balancers, you need to provide elbv2 as the client type:

client = boto3.client('elbv2, 'eu-central-1')

Documentation:

This reference covers the following load balancer types:

Application Load Balancer - Operates at the application layer (layer 7) and supports HTTP and HTTPS.

Network Load Balancer - Operates at the transport layer (layer 4) and supports TCP, TLS, and UDP.

Gateway Load Balancer - Operates at the network layer (layer 3).

  • Related