Skip to content

Commit 09a9013

Browse files
author
rashid4lyf
committed
initial commit
1 parent 835ee1f commit 09a9013

10 files changed

+278
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.lock.hcl
2+
.terraform

README.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Demo VPC, IGW, Public & Private Subnets
2+
3+
This repository creates a VPC, IGW, public and private subnets and route tables.
4+
5+
## Architecture
6+
7+
The following will be created:
8+
9+
![image](assets/cloud_vpc.png)
10+
11+
## Configuring S3 Backend
12+
13+
This project can be configured to use S3 as a backend to store the state file. In addition a DynamoDB locks table will be created.
14+
15+
To enable S3 first set the varible `backend_enabled` to `true`. Then run a `terraform innit` and `terraform apply`. Once applied you can now change `versions.tf` and enable backend support.
16+
17+
```
18+
terraform {
19+
backend "s3" {
20+
bucket = "ENTER_BUCKET_NAME"
21+
key = "terraform_vpc/terraform.tfstate"
22+
region = "eu-west-1"
23+
encrypt = true
24+
kms_key_id = "ENTER_KMS_ID"
25+
dynamodb_table = "ENTER_DYNAMODB_TABLE_NAME"
26+
}
27+
// other terraform configuration
28+
}
29+
```
30+
These values can be gathered from the outputs from the `terraform apply`.
31+
32+
## Requirements
33+
34+
| Name | Version |
35+
|------|---------|
36+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | 4.10.0 |
37+
38+
## Providers
39+
40+
| Name | Version |
41+
|------|---------|
42+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.10.0 |
43+
44+
## Modules
45+
46+
| Name | Source | Version |
47+
|------|--------|---------|
48+
| <a name="module_remote-state-s3-backend"></a> [remote-state-s3-backend](#module\_remote-state-s3-backend) | nozaq/remote-state-s3-backend/aws | 1.2.0 |
49+
50+
## Resources
51+
52+
| Name | Type |
53+
|------|------|
54+
| [aws_eip.nat](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/eip) | resource |
55+
| [aws_internet_gateway.igw](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/internet_gateway) | resource |
56+
| [aws_nat_gateway.nat](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/nat_gateway) | resource |
57+
| [aws_route.private_nat_gateway](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/route) | resource |
58+
| [aws_route.public_internet_gateway](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/route) | resource |
59+
| [aws_route_table.private](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/route_table) | resource |
60+
| [aws_route_table.public](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/route_table) | resource |
61+
| [aws_route_table_association.private](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/route_table_association) | resource |
62+
| [aws_route_table_association.public](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/route_table_association) | resource |
63+
| [aws_security_group.default](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/security_group) | resource |
64+
| [aws_subnet.private](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/subnet) | resource |
65+
| [aws_subnet.public](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/subnet) | resource |
66+
| [aws_vpc.main](https://registry.terraform.io/providers/hashicorp/aws/4.10.0/docs/resources/vpc) | resource |
67+
68+
## Inputs
69+
70+
| Name | Description | Type | Default | Required |
71+
|------|-------------|------|---------|:--------:|
72+
| <a name="input_availability_zones"></a> [availability\_zones](#input\_availability\_zones) | n/a | `list(string)` | <pre>[<br> "eu-west-1a",<br> "eu-west-1b",<br> "eu-west-1c"<br>]</pre> | no |
73+
| <a name="input_backend_enabled"></a> [backend\_enabled](#input\_backend\_enabled) | n/a | `bool` | `false` | no |
74+
| <a name="input_cidr_block"></a> [cidr\_block](#input\_cidr\_block) | VPC cidr block. Example: 10.0.0.0/16 | `string` | `"10.0.0.0/16"` | no |
75+
| <a name="input_region"></a> [region](#input\_region) | AWS Region | `string` | `"eu-west-1"` | no |
76+
| <a name="input_replica_region"></a> [replica\_region](#input\_replica\_region) | Region in which S3 bucket to be replicated. | `string` | `"eu-west-2"` | no |
77+
78+
## Outputs
79+
80+
| Name | Description |
81+
|------|-------------|
82+
| <a name="output_dynamodb_table"></a> [dynamodb\_table](#output\_dynamodb\_table) | n/a |
83+
| <a name="output_kms_key"></a> [kms\_key](#output\_kms\_key) | n/a |
84+
| <a name="output_state_bucket"></a> [state\_bucket](#output\_state\_bucket) | n/a |

assets/cloud_vpc.png

208 KB
Loading

network.tf

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
resource "aws_vpc" "main" {
2+
cidr_block = var.cidr_block
3+
instance_tenancy = "default"
4+
tags = {
5+
Name = "CloudDemo"
6+
Demo = "Terraform"
7+
}
8+
}
9+
10+
resource "aws_subnet" "public" {
11+
vpc_id = aws_vpc.main.id
12+
count = length(var.availability_zones)
13+
cidr_block = cidrsubnet(var.cidr_block, 8, count.index)
14+
availability_zone = element(var.availability_zones, count.index)
15+
16+
map_public_ip_on_launch = true
17+
18+
tags = {
19+
Name = "Public Subnet: ${element(var.availability_zones, count.index)}"
20+
Type = "Public"
21+
}
22+
}
23+
24+
resource "aws_subnet" "private" {
25+
vpc_id = aws_vpc.main.id
26+
count = length(var.availability_zones)
27+
cidr_block = cidrsubnet(var.cidr_block, 8, count.index + length(var.availability_zones))
28+
availability_zone = element(var.availability_zones, count.index)
29+
30+
map_public_ip_on_launch = false
31+
32+
tags = {
33+
Name = "Private Subnet: ${element(var.availability_zones, count.index)}"
34+
Type = "Private"
35+
}
36+
}
37+
38+
resource "aws_internet_gateway" "igw" {
39+
vpc_id = aws_vpc.main.id
40+
tags = {
41+
"Name" = "${var.region}-igw"
42+
"Owner" = "CloudDemo"
43+
}
44+
}
45+
46+
47+
resource "aws_eip" "nat" {
48+
count = length(var.availability_zones)
49+
vpc = true
50+
}
51+
52+
resource "aws_nat_gateway" "nat" {
53+
count = length(var.availability_zones)
54+
subnet_id = element(aws_subnet.public.*.id, count.index)
55+
allocation_id = element(aws_eip.nat.*.id, count.index)
56+
57+
tags = {
58+
"Name" = "NAT: ${element(var.availability_zones, count.index)}"
59+
"Owner" = "CloudDemo"
60+
}
61+
62+
depends_on = [aws_internet_gateway.igw]
63+
}
64+
65+
resource "aws_route_table" "public" {
66+
vpc_id = aws_vpc.main.id
67+
68+
tags = {
69+
Name = "Public"
70+
}
71+
}
72+
73+
resource "aws_route" "public_internet_gateway" {
74+
route_table_id = aws_route_table.public.id
75+
destination_cidr_block = "0.0.0.0/0"
76+
gateway_id = aws_internet_gateway.igw.id
77+
}
78+
79+
resource "aws_route_table_association" "public" {
80+
count = length(var.availability_zones)
81+
subnet_id = element(aws_subnet.public.*.id, count.index)
82+
route_table_id = aws_route_table.public.id
83+
}
84+
85+
86+
resource "aws_route_table" "private" {
87+
vpc_id = aws_vpc.main.id
88+
count = length(var.availability_zones)
89+
90+
tags = {
91+
"Name" = "Private: ${element(var.availability_zones, count.index)}"
92+
}
93+
}
94+
95+
resource "aws_route" "private_nat_gateway" {
96+
count = length(var.availability_zones)
97+
route_table_id = element(aws_route_table.private.*.id, count.index)
98+
destination_cidr_block = "0.0.0.0/0"
99+
nat_gateway_id = element(aws_nat_gateway.nat.*.id, count.index)
100+
}
101+
102+
resource "aws_route_table_association" "private" {
103+
count = length(var.availability_zones)
104+
subnet_id = element(aws_subnet.private.*.id, count.index)
105+
route_table_id = element(aws_route_table.private.*.id, count.index)
106+
}

outputs.tf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "state_bucket" {
2+
value = var.backend_enabled ? module.remote-state-s3-backend.state_bucket.bucket : ""
3+
}
4+
5+
output "dynamodb_table" {
6+
value = var.backend_enabled ? module.remote-state-s3-backend.dynamodb_table.id : ""
7+
}
8+
9+
output "kms_key" {
10+
value = var.backend_enabled ? module.remote-state-s3-backend.kms_key.id: ""
11+
}

providers.tf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
provider "aws" {
2+
region = var.region
3+
}
4+
5+
provider "aws" {
6+
alias = "replica"
7+
region = var.replica_region
8+
}
9+

s3.tf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module "remote-state-s3-backend" {
2+
count = var.backend_enabled ? 1 : 0
3+
source = "nozaq/remote-state-s3-backend/aws"
4+
version = "1.2.0"
5+
providers = {
6+
aws = aws
7+
aws.replica = aws.replica
8+
}
9+
terraform_iam_policy_create = false
10+
}
11+

sg.tf

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "aws_security_group" "default" {
2+
name = "${var.region}-default-sg"
3+
description = "Default security group to allow inbound/outbound from the VPC"
4+
vpc_id = "${aws_vpc.main.id}"
5+
depends_on = [aws_vpc.main]
6+
ingress {
7+
from_port = "0"
8+
to_port = "0"
9+
protocol = "-1"
10+
self = true
11+
}
12+
13+
egress {
14+
from_port = "0"
15+
to_port = "0"
16+
protocol = "-1"
17+
self = "true"
18+
}
19+
}

variables.tf

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
variable "region" {
2+
description = "AWS Region"
3+
default = "eu-west-1"
4+
}
5+
6+
variable "replica_region" {
7+
description = "Region in which S3 bucket to be replicated."
8+
default = "eu-west-2"
9+
10+
}
11+
12+
variable "cidr_block" {
13+
type = string
14+
description = "VPC cidr block. Example: 10.0.0.0/16"
15+
default = "10.0.0.0/16"
16+
}
17+
18+
variable "availability_zones" {
19+
type = list(string)
20+
default = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
21+
}
22+
23+
variable "backend_enabled" {
24+
type = bool
25+
default = false
26+
27+
}

versions.tf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = "~> 1.1.0"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = "4.10.0"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)