-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvpc.tf
More file actions
108 lines (87 loc) · 2.31 KB
/
vpc.tf
File metadata and controls
108 lines (87 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
provider "aws" {
region = "eu-west-3"
}
# 1 - creation of the VPC
# 2 - creation of a public and private Network
# 3 - Routes all the traffic from the private network to the public network
# 4 - Routes all the traffic from the public network to internet
# 5 - assigns an elastic IP to the traffic coming from the public network (with the nat gateway)
# Virtual Private Cloud (VPC)
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
#Subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "public"
}
}
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
tags = {
Name = "private"
}
}
#Routes tables public
resource "aws_route_table" "routes-public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.ig.id
}
}
#Routes tables private
resource "aws_route_table" "routes-private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat.id
}
}
# Route Table Associations
resource "aws_route_table_association" "ehr-routes-public" {
route_table_id = aws_route_table.routes-public.id
subnet_id = aws_subnet.public_subnet.id
}
resource "aws_route_table_association" "ehr-routes-private" {
route_table_id = aws_route_table.routes-private.id
subnet_id = aws_subnet.private_subnet.id
}
# Internet Gateway
resource "aws_internet_gateway" "ig" {
vpc_id = "${aws_vpc.main.id}"
}
# Elastic IP
resource "aws_eip" "nat_eip" {
vpc = true
depends_on = [aws_internet_gateway.ig]
}
# Nat Gateway
resource "aws_nat_gateway" "nat" {
allocation_id = "${aws_eip.nat_eip.id}"
subnet_id = "${element(aws_subnet.public_subnet.*.id, 0)}"
depends_on = [aws_internet_gateway.ig]
}
# 1 EC2 Instance in public/ Private Subnet (Amazon Linux 2 AMI)
resource "aws_instance" "my_instance" {
ami = "ami-00798d7180f25aac2" # eu-west-3
instance_type = "t2.micro"
}
# Security Groups
resource "aws_security_group" "sg" {
name = "security group"
description = "Allow inbound traffic"
vpc_id = aws_vpc.main.id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "my_sg"
}
}