daniel-rhoades.aws-security-groups
aws-security-group-role
AWSアカウント内のEC2セキュリティグループのプロビジョニングと廃止を簡単にするためのAnsibleロールです。
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: セキュリティグループの状態を「inbound」としてデフォルトは
present
; - ec2_internal_inbound_group_state: セキュリティグループの状態を「internal inbound」としてデフォルトは
present
; - ec2_outbound_group_state: セキュリティグループの状態を「outbound」としてデフォルトは
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: 提供された「inbound」変数を使用して
ec2_group_module
を実行した結果として作成されたAWS EC2グループオブジェクト; - ec2_group_internal_inbound_sg: 提供された「internal inbound」変数を使用して
ec2_group_module
を実行した結果として作成されたAWS EC2グループオブジェクト; - ec2_group_outbound_sg: 提供された「outbound」変数を使用して
ec2_group_module
を実行した結果として作成されたAWS EC2グループオブジェクト。
ロールは以下の順序でセキュリティグループを処理します:
- インバウンド;
- 内部インバウンド;
- アウトバウンド。
これは、インバウンドリスト内で作成されるセキュリティグループを内部インバウンドリストで参照できることを意味します。これは、インバウンドと内部インバウンドグループ間でトラフィックをルーティングする場合に特に便利です。
依存関係
他のロールに依存関係はありませんが、このロールを適用する前にVPCがすでに存在しているか、作成される必要があります。
例プレイブック
このロールを使用する前に、ロールをインストールする必要があります。最も簡単な方法は:ansible-galaxy install daniel-rhoades.aws-security-group-role
です。
以下の例プレイブックは、指定されたようにAWSにEC2セキュリティグループがプロビジョニングされることを保証します。たとえば、すでに一致するものがある場合、ロールは何もしません。それ以外の場合は作成されます。完全性のために、例はEC2セキュリティグループが存在するVPCも作成します:daniel-rhoades.aws-vpc
。
- name: My System | 必要なインフラをプロビジョニングする
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の外から直接アクセスされるべきでないサービス
#
# インバウンドセキュリティグループが作成された後に動的に含められる必要があるため、ファイルである必要があります。
my_internal_inbound_security_groups_file: "internal-securitygroups.yml"
# アウトバウンドルール、例:ウェブサーバーが自分自身でアクセスできるサービス
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 | 必要なインフラを廃止する
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の外から直接アクセスされるべきでないサービス
#
# インバウンドセキュリティグループが作成された後に動的に含められる必要があるため、ファイルである必要があります。
my_internal_inbound_security_groups_file: "internal-securitygroups.yml"
# アウトバウンドルール、例:ウェブサーバーが自分自身でアクセスできるサービス
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
著者情報
ダニエル・ローズ (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