首页 > 解决方案 > 将安全组设置为 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% 确定我拥有与此名称完全相同的负载均衡器。我在这里做错了什么?谢谢!

标签: pythonamazon-web-servicesaws-cdk

解决方案


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

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

文档

本参考涵盖了支持Classic Load Balancer的 2012-06-01 API


要创建与 Application Load Balancer 一起使用的客户端,您需要提供elbv2客户端类型

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

文档

本参考涵盖以下负载均衡器类型:

Application Load Balancer - 在应用层(第 7 层)运行,支持 HTTP 和 HTTPS。

网络负载均衡器 - 在传输层(第 4 层)运行并支持 TCP、TLS 和 UDP。

网关负载均衡器 - 在网络层(第 3 层)运行。


推荐阅读