将安全组设置为 ALB aws

Set security groups to an ALB aws

我正在尝试为我的 ALB 设置一些安全组。 这是我写的代码:

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)

不幸的是它不起作用,这是我得到的代码:

{
  "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"
  ]
}

我 100% 确定我有这个名称的负载均衡器。 我在这里做错了什么?谢谢!

您正在使用“elb”作为客户端,它仅用于“经典”负载平衡器。由于您使用的是 ALB,因此您应该使用“elbv2”作为客户端

您的客户端设置为使用 Classic Load Balancers,而不是 Application Load Balancer,因为您使用 elb 作为客户端类型。

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

Documentation:

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


要创建与 Application Load Balancer 配合使用的客户端,您需要提供 elbv2 作为客户端类型:

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).