首页 > 解决方案 > 向 Application Load Balancer (ALB) 注册 EC2 实例时出现问题

问题描述

根据文档,我应该能够使用elb_instance模块注册 EC2 实例,但播放失败并显示ELB Staging1 does not exist. 对于健全性检查,我尝试使用 AWS CLI 工具,但使用An error occurred (LoadBalancerNotFound) when calling the RegisterInstancesWithLoadBalancer operation: There is no ACTIVE Load Balancer named 'Staging1'. 作为情节转折:

似乎应用程序负载均衡器仅在较新的 api 版本中受支持,并且模块或 cli 不适合使用它。但是我找不到明确提到它们不应该工作。我也知道elbv2command,但它似乎没有提供将实例注册到 ELB 的方法。

我在这里想念什么?

我写的 Ansible 任务:

- name: Register instances in the ALB
  local_action:
        module: elb_instance
        aws_access_key: "{{ aws_credentials.access_key_id }}"
        aws_secret_key: "{{ aws_credentials.secret_access_key }}"
        region: "{{ aws_region }}"
        instance_id: "{{ item.instance_ids.0 }}"
        ec2_elbs: "Staging1"
        state: present
        wait: yes
  loop: "{{ new_instances.results }}"

使用 cli 工具替代aws(也不起作用):

    - name: Register instance in ALB workaround
      command: "aws elb register-instances-with-load-balancer --load-balancer-name Staging1 --instances {{ instance_ids | join(' ') }}"
      environment:
        AWS_REGION: "{{ aws_region }}"
        AWS_ACCESS_KEY: "{{ aws_credentials.access_key_id }}"
        AWS_SECRET_KEY: "{{ aws_credentials.secret_access_key }}"

标签: amazon-web-servicesansibleamazon-elbaws-application-load-balancer

解决方案


elbv2 确实提供了一种将目标注册到 elb 的方法。你这样做:

  1. 创建目标组
  2. 然后使用注册目标组API 注册您的 EC2 实例
  3. 然后使用Create Listener API 将 ELB 与目标组关联

在我提供的链接中有这样做的例子。


推荐阅读