daniel-rhoades.aws-elb

Circle CI

aws-elb-role

这是一个 Ansible 角色,用于简化在 AWS 账户中创建和撤销 ELB(弹性负载均衡)的过程。

有关创建的详细信息:

此角色将完全设置一个多可用区(Multi-AZ)的 ELB,并在您选择的区域中注册一个 Route 53 记录。

要求

需要最新的 Ansible EC2 支持模块和 Boto。

您还需要配置 Ansible 环境以便与 AWS 一起使用,详见 http://docs.ansible.com/ansible/guide_aws.html。

角色变量

默认变量:

  • elb_purge_subnets: 清除 ELB 上未在监听器中找到的现有监听器,默认为 true;
  • elb_cross_az_load_balancing: 在所有配置的可用区之间分配负载,默认为 true;
  • elb_connection_draining_timeout: 等待指定超时以允许连接排空,然后终止实例,默认为 60 秒;
  • elb_listeners: ELB 的监听器,默认提供 80:80;
  • elb_health_check: 用于判断实例健康状况的健康检查,默认是通过 TCP 连接到 80 端口;
  • route53_overwrite: 若需要,覆盖已存在的条目,默认为 true;
  • route53_alias_evaluate_target_health: 如果希望 Route 53 监控目标健康状况,默认为 true;
  • ec2_elb_state: ELB 的状态,默认为 "present"(存在);
  • route53_zone_state: Route 53 区域的状态,默认为 "present"(存在)。

必需的变量:

  • elb_name: 您必须指定要创建的 ELB 的名称,例如:my-elb;
  • elb_security_groups: 您必须指定要应用于 ELB 的安全组,请参见下面的示例剧本;
  • elb_region: 您必须指定希望创建 ELB 的区域,例如:eu-west-1;
  • elb_subnets: 您必须指定希望 ELB 所在的子网,请参见下面的示例剧本;
  • route53_zone: 您必须指定希望定义的区域名称,例如:example.com;
  • route53_host: 您必须指定希望在该区域中定义的主机名称,例如:www.example.com。

依赖关系

此处应列出其他在 Galaxy 上托管的角色,以及与其他角色需要设置的参数或使用的变量的任何详细信息。

示例剧本

使用此角色之前,您需要先安装该角色,最简单的方法是:ansible-galaxy install daniel-rhoades.aws-elb-role

为了完整性,以下示例创建了:

  • 用于保存 ECS 集群的 VPC,使用角色: daniel-rhoades.aws-vpc
  • 应用于 EC2 实例的 EC2 安全组,使用角色: daniel-rhoades.aws-security-group

创建后,您可以将 EC2 实例注册到 ELB。示例期望 my_route53_zone 被作为命令行环境变量传入。

- name: 我的系统 | 提供所有所需基础设施
  hosts: localhost
  connection: local
  gather_facts: no
  vars:
    my_vpc_name: "my_example_vpc"
    my_vpc_region: "eu-west-1"
    my_vpc_cidr: "172.40.0.0/16"
    everywhere_cidr: "0.0.0.0/0"

    # VPC 内的子网
    my_vpc_subnets:
      - cidr: "172.40.10.0/24"
        az: "{{ my_vpc_region }}a"

      - cidr: "172.40.20.0/24"
        az: "{{ my_vpc_region }}b"

    # 允许子网路由到外部
    my_public_subnet_routes:
      - subnets:
          - "{{ my_vpc_subnets[0].cidr }}"
          - "{{ my_vpc_subnets[1].cidr }}"
        routes:
          - dest: "{{ everywhere_cidr }}"
            gw: igw

    # 入站安全组,例如公共服务如负载均衡器
    my_inbound_security_groups:
      - sg_name: inbound-web
        sg_description: 允许 http 和 https 访问(公共)
        sg_rules:
          - proto: tcp
            from_port: 80
            to_port: 80
            cidr_ip: "{{ everywhere_cidr }}"

          - proto: tcp
            from_port: 443
            to_port: 443
            cidr_ip: "{{ everywhere_cidr }}"

      # 仅允许从 VPC 内部的 SSH 访问,要访问 VPC 内的任何服务,您需要创建一个临时堡垒主机
      - sg_name: inbound-ssh
        sg_description: 允许 ssh 访问
        sg_rules:
         - proto: tcp
           from_port: 22
           to_port: 22
           cidr_ip: "{{ my_vpc_cidr }}"

    # 内部入站安全组,例如不应直接从外部访问的服务,如在负载均衡器后面的 Web 服务器。
    #
    # 这必须是一个文件,因为它需要在创建入站安全组后动态包含
    my_internal_inbound_security_groups_file: "internal-securitygroups.yml"

    # 出站规则,例如 Web 服务器可以自行访问的服务
    my_outbound_security_groups:
      - sg_name: outbound-all
        sg_description: 允许出站流量到任何 IP 地址
        sg_rules:
          - proto: all
            cidr_ip: "{{ everywhere_cidr }}"

    # ELB 名称
    my_elb_name: "my-elb"

    # 在 Route 53 区域上创建的主机名称
    my_route53_host: "my-service-test"

  roles:
    # 提供网络
    - {
        role: daniel-rhoades.aws-vpc,
        vpc_name: "{{ my_vpc_name }}",
        vpc_region: "{{ my_vpc_region }}",
        vpc_cidr_block: "{{ my_vpc_cidr }}",
        vpc_subnets: "{{ my_vpc_subnets }}",
        public_subnet_routes: "{{ my_public_subnet_routes }}"
      }

    # 提供安全组
    - {
        role: daniel-rhoades.aws-security-groups,
        vpc_region: "{{ my_vpc_region }}",
        vpc_id: "{{ vpc.vpc_id }}",
        ec2_group_inbound_sg: "{{ my_inbound_security_groups }}",
        ec2_group_internal_inbound_sg_file: "{{ my_internal_inbound_security_groups_file }}",
        ec2_group_outbound_sg: "{{ my_outbound_security_groups }}"
      }

    # 提供一个 ELB 并在 Route 53 中注册为别名
    - {
        role: aws-elb-role,
        elb_name: "{{ my_elb_name }}",
        vpc_name: "{{ my_vpc_name }}",
        elb_security_groups: [
          "{{ ec2_group_inbound_sg.results[0].group_id }}"
          ],
        elb_region: "{{ my_vpc_region }}",
        elb_subnets: [
           "{{ vpc.subnets[0].id }}",
           "{{ vpc.subnets[1].id }}"
         ],
        route53_zone: "{{ my_route53_zone }}",
        route53_host: "{{ my_route53_host }}"
      }

示例 internal-securitygroups.yml 如下所示:

ec2_group_internal_inbound_sg:
  - sg_name: inbound-web-internal
    sg_description: 允许 http 和 https 访问(仅来自负载均衡器)
    sg_rules:
      - proto: tcp
        from_port: 80
        to_port: 80
        group_id: "{{ ec2_group_inbound_sg.results[0].group_id }}"

要撤销 ELB:

- name: 我的系统 | 撤销所有所需基础设施
  hosts: localhost
  connection: local
  gather_facts: no
  vars:
    my_vpc_name: "my_example_vpc"
    my_vpc_region: "eu-west-1"
    my_vpc_cidr: "172.40.0.0/16"
    everywhere_cidr: "0.0.0.0/0"

    # VPC 内的子网
    my_vpc_subnets:
      - cidr: "172.40.10.0/24"
        az: "{{ my_vpc_region }}a"

      - cidr: "172.40.20.0/24"
        az: "{{ my_vpc_region }}b"

    # 允许子网路由到外部
    my_public_subnet_routes:
      - subnets:
          - "{{ my_vpc_subnets[0].cidr }}"
          - "{{ my_vpc_subnets[1].cidr }}"
        routes:
          - dest: "{{ everywhere_cidr }}"
            gw: igw

    # 要创建的 ECS 集群名称
    my_ecs_cluster_name: "my-cluster"

    # ELB 名称
    my_elb_name: "my-elb"

    # 在 Route 53 区域上创建的主机名称
    my_route53_host: "my-service-test"

  roles:
    # 提供网络
    - {
        role: daniel-rhoades.aws-vpc,
        vpc_name: "{{ my_vpc_name }}",
        vpc_region: "{{ my_vpc_region }}",
        vpc_cidr_block: "{{ my_vpc_cidr }}",
        vpc_subnets: "{{ my_vpc_subnets }}",
        public_subnet_routes: "{{ my_public_subnet_routes }}"
      }

    # 提供安全组
    - {
        role: daniel-rhoades.aws-security-groups,
        vpc_region: "{{ my_vpc_region }}",
        vpc_id: "{{ vpc.vpc_id }}",
        ec2_group_inbound_sg: "{{ my_inbound_security_groups }}",
        ec2_group_internal_inbound_sg_file: "{{ my_internal_inbound_security_groups_file }}",
        ec2_group_outbound_sg: "{{ my_outbound_security_groups }}"
      }

    # 撤销 ELB (REST 端点)
    - {
        role: aws-elb-role,
        ec2_elb_state: "absent",
        vpc_name: "{{ my_vpc_name }}",
        elb_name: "{{ my_elb_name }}",
        route53_zone: "{{ my_route53_zone }}",
        route53_host: "{{ my_route53_host }}"
      }

许可证

MIT

作者信息

Daniel Rhoades (https://github.com/daniel-rhoades)

关于项目

Ansible role for simplifying the provisioning and decommissioning of an ELB within an AWS account

安装
ansible-galaxy install daniel-rhoades.aws-elb
许可证
mit
下载
83
拥有者
Strategist, Technologist and Engineer