官术网_书友最值得收藏!

How to do it...

We can create a VPC by using an ec2_vpc_net module. This module will take a name, the regions, and a CIDR block as the argument along with our credentials. 

  1. Let us define the task:
- name: Create AWS VPC
ec2_vpc_net:
name: "{{ vpc_name }}"
cidr_block: "{{ vpc_cidr_block }}"
region: "{{ aws_region }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
state: present
register: my_first_vpc

Note that we have registered the output of the task in a variable called my_first_vpc. We will use values from this variable in the subsequent tasks. We have used quite a few variables as well. Using variables appropriately makes it easier to reuse the roles and playbooks at a later point. Other than access_key and secret_key, the rest of the variables are defined in chapter2/roles/ec2/vars/main.yml:

# VPC Information
vpc_name: "My VPC"
vpc_cidr_block: "10.0.0.0/16"
aws_region: "us-east-1"
  1. Now let us create a public and a private subnet using an ec2_vpc_subnet module. We will supply a smaller block of CIDR out of the CIDR block that we used while creating the VPC. We also need to provide information about the region and the availability zone within the region. We will get the VPC ID from the variable that we registered in the previous task:
- name: Create Public Subnet in VPC
ec2_vpc_subnet:
vpc_id: "{{ my_first_vpc.vpc.id }}"
cidr: "{{ vpc_public_subnet_cidr }}"
region: "{{ aws_region }}"
az: "{{ aws_zone }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
state: present
tags:
Name: Public Subnet
register: my_public_subnet

- name: Create Private Subnet in VPC
ec2_vpc_subnet:
vpc_id: "{{ my_first_vpc.vpc.id }}"
cidr: "{{ vpc_private_subnet_cidr }}"
region: "{{ aws_region }}"
az: "{{ aws_zone }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
state: present
tags:
Name: Private Subnet
register: my_private_subnet

We have created two subnets using these two tasks. The tasks are identical, except for the CIDR block allocated to them. At this point, there is not much of a difference between the public and private subnet in terms of functionality. The functional difference will arise when we attach route tables later. We will register the output of these tasks in a variable for further use. For these tasks, we need to add the following variables to our roles/ec2/vars/main.yml:

aws_zone: "us-east-1a"

# Subnets
vpc_public_subnet_cidr: "10.0.0.0/24"

# Subnets
vpc_private_subnet_cidr: "10.0.1.0/24"
  1. Let us create the Internet Gateway now. This is quite simple. All we need to do is provide the VPC ID and region along with the credentials. We will register the output of this task in a variable:
- name: Create Internet Gateway
ec2_vpc_igw:
vpc_id: "{{ my_first_vpc.vpc.id }}"
region: "{{ aws_region }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
state: present
register: my_first_igw
  1. After this, we will create the NAT Gateway. One thing to note here is that the NAT Gateway is attached to the private subnet but it is created in the public subnet. This is because inbound traffic needs to reach this instance, which will then be translated and forward onto instances in the private subnet. We will get the public subnet ID from the variable that we registered:
- name: Create NAT Gateway
ec2_vpc_nat_gateway:
if_exist_do_not_create: yes
subnet_id: "{{ my_public_subnet.subnet.id }}"
region: "{{ aws_region }}"
state: present
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
wait: yes
register: my_first_nat_gateway
  1. With both the Internet Gateway and NAT Gateway created, we will create and attach the routing table using an ec2_vpc_route_table module. We will get the VPC ID, subnet ID, and gateway ID from the variables that we have registered before:
- name: Create Route Table for Public Subnet
ec2_vpc_route_table:
vpc_id: "{{ my_first_vpc.vpc.id }}"
region: "{{ aws_region }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ my_first_igw.gateway_id }}"
subnets:
- "{{ my_public_subnet.subnet.id }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
tags:
Name: Public Subnet Route Table

- name: Create Route Table for Private Subnet
ec2_vpc_route_table:
vpc_id: "{{ my_first_vpc.vpc.id }}"
region: "{{ aws_region }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ my_first_nat_gateway.nat_gateway_id }}"
subnets:
- "{{ my_private_subnet.subnet.id }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
tags:
Name: Private Subnet Route Table

With these tasks, our VPC is configured and ready to use. We can create resources in this VPC and use them to deploy our applications.

主站蜘蛛池模板: 砀山县| 嵊泗县| 平陆县| 武强县| 云霄县| 保康县| 隆回县| 鸡泽县| 大兴区| 东乡族自治县| 铁岭市| 昭觉县| 宝坻区| 靖州| 台中市| 长海县| 安图县| 香河县| 东乌珠穆沁旗| 临安市| 孝义市| 密山市| 南开区| 河西区| 吴堡县| 称多县| 正宁县| 永福县| 枝江市| 依安县| 游戏| 小金县| 柘荣县| 峨边| 桐柏县| 阿瓦提县| 乐业县| 黄平县| 辉县市| 沁水县| 民丰县|