Skip to content

Commit 7e0585a

Browse files
committed
🎉 first commit
0 parents  commit 7e0585a

File tree

10 files changed

+250
-0
lines changed

10 files changed

+250
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.terraform
2+
*terraform.tfstate*
3+
.terraform.lock.hcl
4+
test.sh
5+
test.tfvars

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 zoe zhang
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# terraform-aws-vpc
2+
3+
## overview
4+
a light terraform module to create a vpc with 3 private subnets, 3 public subnets, 1 gateway, 3 nat gateway.
5+
6+
## usage
7+
see example folder.
8+
```tf
9+
module "test" {
10+
source = "../"
11+
name_prefix = "test"
12+
vpc_cidr = "10.17.0.0/20"
13+
natgateway = ["a", "b", "c"]
14+
public_subnets = {
15+
a = "10.17.0.0/23"
16+
b = "10.17.2.0/23"
17+
c = "10.17.4.0/23"
18+
}
19+
private_subnets = {
20+
a = "10.17.6.0/23"
21+
b = "10.17.8.0/23"
22+
c = "10.17.10.0/23"
23+
}
24+
}
25+
```
26+
27+
## input
28+
| Name | Description | Type | Default | Required |
29+
|------|-------------|------|---------|:--------:|
30+
| name_prefix | a name prefix used to tag vpc | `string` | true | yes |
31+
| vpc_cidr | vpc cidr block | `string` | false | yes |
32+
| public_subnets | public subnets map, availability zone map to cidr block | `map` | false | yes |
33+
| private_subnets | private subnets map, availability zone map to cidr block | `map` | false | yes |
34+
| natgateway | nat gateway list of availability zone to spread | `list` | false | yes |
35+
36+
## output
37+
vpc_id

example/main.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module "test" {
2+
source = "../"
3+
name_prefix = "test"
4+
vpc_cidr = "10.17.0.0/20"
5+
natgateway = ["a", "b", "c"]
6+
public_subnets = {
7+
a = "10.17.0.0/23"
8+
b = "10.17.2.0/23"
9+
c = "10.17.4.0/23"
10+
}
11+
private_subnets = {
12+
a = "10.17.6.0/23"
13+
b = "10.17.8.0/23"
14+
c = "10.17.10.0/23"
15+
}
16+
}

example/provider.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
provider "aws" {
2+
# i am using environment variables locally
3+
region = "us-east-1"
4+
# access_key = "AKIAZGWEKBAWRKPVLRIL"
5+
# secret_key = "my-secret-key"
6+
default_tags {
7+
tags = {
8+
Provisioner = "Terraform"
9+
Project = "POC"
10+
OWNER = "ops"
11+
}
12+
}
13+
}

example/version.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~>4.8"
6+
}
7+
}
8+
required_version = "1.1.7"
9+
}

main.tf

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
data "aws_region" "current" {
2+
3+
}
4+
resource "aws_vpc" "vpc" {
5+
6+
cidr_block = var.vpc_cidr
7+
enable_dns_hostnames = true
8+
enable_dns_support = true
9+
tags = {
10+
"Name" = "${var.name_prefix}-vpc"
11+
}
12+
13+
}
14+
15+
resource "aws_default_security_group" "vpc-default-sg" {
16+
17+
vpc_id = aws_vpc.vpc.id
18+
egress {
19+
from_port = 0
20+
to_port = 0
21+
protocol = "-1"
22+
cidr_blocks = ["0.0.0.0/0"]
23+
ipv6_cidr_blocks = ["::/0"]
24+
}
25+
}
26+
27+
28+
resource "aws_default_route_table" "default-rt" {
29+
30+
default_route_table_id = aws_vpc.vpc.default_route_table_id
31+
32+
}
33+
34+
resource "aws_internet_gateway" "igw" {
35+
36+
vpc_id = aws_vpc.vpc.id
37+
38+
}
39+
40+
41+
resource "aws_route_table" "public-rt" {
42+
vpc_id = aws_vpc.vpc.id
43+
44+
route {
45+
cidr_block = "0.0.0.0/0"
46+
gateway_id = aws_internet_gateway.igw.id
47+
}
48+
}
49+
50+
resource "aws_subnet" "public-subnet" {
51+
for_each = var.public_subnets
52+
vpc_id = aws_vpc.vpc.id
53+
cidr_block = each.value
54+
availability_zone = format("%s%s", data.aws_region.current.name, each.key)
55+
map_public_ip_on_launch = "true"
56+
tags = {
57+
"Name" = format("%s-public-%s", var.name_prefix, each.key)
58+
}
59+
}
60+
resource "aws_route_table_association" "public-rba" {
61+
for_each = var.public_subnets
62+
63+
subnet_id = aws_subnet.public-subnet[each.key].id
64+
route_table_id = aws_route_table.public-rt.id
65+
}
66+
resource "aws_eip" "nateip" {
67+
for_each = toset(var.natgateway)
68+
vpc = true
69+
lifecycle {
70+
prevent_destroy = false
71+
}
72+
}
73+
74+
resource "aws_nat_gateway" "natgw" {
75+
for_each = toset(var.natgateway)
76+
allocation_id = aws_eip.nateip[each.key].id
77+
subnet_id = aws_subnet.public-subnet[each.key].id
78+
depends_on = [aws_internet_gateway.igw]
79+
}
80+
81+
resource "aws_subnet" "private-subnet" {
82+
for_each = var.private_subnets
83+
vpc_id = aws_vpc.vpc.id
84+
cidr_block = each.value
85+
availability_zone = format("%s%s", data.aws_region.current.name, each.key)
86+
map_public_ip_on_launch = "false"
87+
tags = {
88+
"Name" = format("%s-private-%s", var.name_prefix, each.key)
89+
}
90+
}
91+
92+
resource "aws_route_table" "private-rt" {
93+
for_each = var.private_subnets
94+
vpc_id = aws_vpc.vpc.id
95+
route {
96+
cidr_block = "0.0.0.0/0"
97+
nat_gateway_id = aws_nat_gateway.natgw[each.key].id
98+
}
99+
100+
}
101+
resource "aws_route_table_association" "private-rba" {
102+
for_each = var.private_subnets
103+
104+
subnet_id = aws_subnet.private-subnet[each.key].id
105+
route_table_id = aws_route_table.private-rt[each.key].id
106+
}

output.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "vpc_id" {
2+
value=aws_vpc.vpc.id
3+
}

variables.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
variable "name_prefix" {
2+
type = string
3+
description = "a name tag for resources"
4+
default = "test"
5+
}
6+
variable "vpc_cidr" {
7+
type = string
8+
default = "10.240.0.0/16"
9+
description = "vpc cidr block"
10+
}
11+
variable "public_subnets" {
12+
default = {
13+
a = "10.240.0.0/22"
14+
b = "10.240.4.0/22"
15+
c = "10.240.8.0/22"
16+
}
17+
description = "public subnets map, availability zone map to cidr block"
18+
}
19+
variable "private_subnets" {
20+
default = {
21+
a = "10.240.12.0/22"
22+
b = "10.240.16.0/22"
23+
c = "10.240.20.0/22"
24+
}
25+
description = "private subnets map, availability zone map to cidr block"
26+
}
27+
28+
variable "natgateway" {
29+
default = ["a", "b", "c"]
30+
description = "nat gateway list of availability zone to spread"
31+
}

version.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = ">=4.8"
6+
}
7+
}
8+
required_version = ">=0.13"
9+
}

0 commit comments

Comments
 (0)