daniel-rhoades.aws-security-groups

Circle CI

aws-security-group-role

这是一个 Ansible 角色,用于简化 AWS 账户内 EC2 安全组的创建和销毁。

有关使用 Ansible 创建 EC2 安全组的详细信息,请参见该模块的官方文档:http://docs.ansible.com/ansible/ec2_group_module.html。

需求

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

您还需要为 AWS 配置 Ansible 环境,请参见 http://docs.ansible.com/ansible/guide_aws.html。

角色变量

默认设置:

  • security_group_resource_tags:默认设置为安全组的名称;
  • ec2_inbound_group_state:安全组列表的状态设置为“入站”,默认为 present
  • ec2_internal_inbound_group_state:安全组列表的状态设置为“内部入站”,默认为 present
  • ec2_outbound_group_state:安全组列表的状态设置为“出站”,默认为 present

必需变量:

  • vpc_region:您必须指定创建 VPC 的区域,例如 eu-west-1;
  • vpc_id:您必须指定希望创建安全组的 VPC ID,例如 vpc-xxxxxxxx;
  • ec2_group_inbound_sg:您必须指定要创建的入站安全组列表,具体信息请参见下面的示例剧本部分;
  • ec2_group_internal_inbound_sg_file:您必须指定要创建的内部入站安全组列表,具体信息请参见下面的示例剧本部分。注意,这必须是一个文件,因为它将在创建入站组后动态包含;
  • ec2_group_outbound_sg:您必须指定要创建的出站安全组列表,具体信息请参见下面的示例剧本部分。

入站安全组应该应用于公开服务,例如负载均衡器。内部入站安全组预期分配给负载均衡器后面的实例或 VPC 内部。出站安全组是服务在内部网络中可以调用的端口。

输出:

  • ec2_group_inbound_sg:运行 ec2_group_module 生成的 AWS EC2 组对象,使用提供的“入站”变量;
  • ec2_group_internal_inbound_sg:运行 ec2_group_module 生成的 AWS EC2 组对象,使用提供的“内部入站”变量;
  • ec2_group_outbound_sg:运行 ec2_group_module 生成的 AWS EC2 组对象,使用提供的“出站”变量。

该角色按以下顺序处理安全组:

  • 入站;
  • 内部入站;
  • 出站。

这意味着您可以在“内部入站”列表中引用将在“入站”列表中创建的安全组,这在您希望仅在入站和内部入站组之间路由流量时非常有用,例如负载均衡器和 Web 服务器之间的流量。

依赖

不依赖于其他角色,但在应用此角色之前,必须先创建 VPC。

示例剧本

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

以下示例剧本确保在 AWS 中根据指定条件创建 EC2 安全组,例如,如果已经有一个匹配的,角色将不执行任何操作,否则将创建一个。为完整性,示例还使用角色 daniel-rhoades.aws-vpc 创建一个 VPC,在其中放置 EC2 安全组。

- name: My System | Provision all required infrastructure
  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 }}"

    # 内部入站安全组,例如不应直接从 VPC 外部访问的服务,如
    # 后面的 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 }}"
  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 }}"
      }

示例 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 }}"

要销毁安全组:

- name: My System | Decommission all required infrastructure
  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 访问
      - 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 }}"
  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 }}",

        ec2_inbound_group_state: "absent",
        ec2_internal_inbound_group_state: "absent",
        ec2_outbound_group_state: "absent"
      }

许可证

MIT

作者信息

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

关于项目

Ansible role for simplifying the provisioning and decommissioning of a EC2 Security Groups within an AWS account

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