forked from snyk-labs/nodejs-goof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc.tf
199 lines (168 loc) · 4.76 KB
/
vpc.tf
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
resource "aws_instance" "web_host" {
# ec2 have plain text secrets in user data
ami = "${var.ami}"
instance_type = "t2.nano"
vpc_security_group_ids = [
"${aws_security_group.web-node.id}"]
subnet_id = "${aws_subnet.web_subnet.id}"
user_data = <<EOF
#! /bin/bash
sudo apt-get update
sudo apt-get install -y apache2
sudo systemctl start apache2
sudo systemctl enable apache2
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMAAA
export AWS_SECRET_ACCESS_KEY=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
export AWS_DEFAULT_REGION=us-west-2
echo "<h1>Deployed via Terraform</h1>" | sudo tee /var/www/html/index.html
EOF
tags = {
Name = "${local.resource_prefix.value}-ec2"
}
}
resource "aws_ebs_volume" "web_host_storage" {
# unencrypted volume
availability_zone = "${var.availability_zone}"
#encrypted = false # Setting this causes the volume to be recreated on apply
size = 1
tags = {
Name = "${local.resource_prefix.value}-ebs"
}
}
resource "aws_ebs_snapshot" "example_snapshot" {
# ebs snapshot without encryption
volume_id = "${aws_ebs_volume.web_host_storage.id}"
description = "${local.resource_prefix.value}-ebs-snapshot"
tags = {
Name = "${local.resource_prefix.value}-ebs-snapshot"
}
}
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.web_host_storage.id}"
instance_id = "${aws_instance.web_host.id}"
}
resource "aws_security_group" "web-node" {
# security group is open to the world in SSH port
name = "${local.resource_prefix.value}-sg"
description = "${local.resource_prefix.value} Security Group"
vpc_id = aws_vpc.web_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [
"0.0.0.0/0"]
}
depends_on = [aws_vpc.web_vpc]
}
resource "aws_vpc" "web_vpc" {
cidr_block = "172.16.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "${local.resource_prefix.value}-vpc"
}
}
resource "aws_subnet" "web_subnet" {
vpc_id = aws_vpc.web_vpc.id
cidr_block = "172.16.10.0/24"
availability_zone = var.availability_zone
map_public_ip_on_launch = true
tags = {
Name = "${local.resource_prefix.value}-subnet"
}
}
resource "aws_subnet" "web_subnet2" {
vpc_id = aws_vpc.web_vpc.id
cidr_block = "172.16.11.0/24"
availability_zone = var.availability_zone2
map_public_ip_on_launch = true
tags = {
Name = "${local.resource_prefix.value}-subnet2"
}
}
resource "aws_internet_gateway" "web_igw" {
vpc_id = aws_vpc.web_vpc.id
tags = {
Name = "${local.resource_prefix.value}-igw"
}
}
resource "aws_route_table" "web_rtb" {
vpc_id = aws_vpc.web_vpc.id
tags = {
Name = "${local.resource_prefix.value}-rtb"
}
}
resource "aws_route_table_association" "rtbassoc" {
subnet_id = aws_subnet.web_subnet.id
route_table_id = aws_route_table.web_rtb.id
}
resource "aws_route_table_association" "rtbassoc2" {
subnet_id = aws_subnet.web_subnet2.id
route_table_id = aws_route_table.web_rtb.id
}
resource "aws_route" "public_internet_gateway" {
route_table_id = aws_route_table.web_rtb.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.web_igw.id
timeouts {
create = "5m"
}
}
resource "aws_network_interface" "web-eni" {
subnet_id = aws_subnet.web_subnet.id
private_ips = ["172.16.10.100"]
tags = {
Name = "${local.resource_prefix.value}-primary_network_interface"
}
}
# VPC Flow Logs to S3
resource "aws_flow_log" "vpcflowlogs" {
log_destination = aws_s3_bucket.flowbucket.arn
log_destination_type = "s3"
traffic_type = "ALL"
vpc_id = aws_vpc.web_vpc.id
tags = {
Name = "${local.resource_prefix.value}-flowlogs"
Environment = local.resource_prefix.value
}
}
resource "aws_s3_bucket" "flowbucket" {
bucket = "${local.resource_prefix.value}-flowlogs"
force_destroy = true
tags = {
Name = "${local.resource_prefix.value}-flowlogs"
Environment = local.resource_prefix.value
}
}
output "ec2_public_dns" {
description = "Web Host Public DNS name"
value = aws_instance.web_host.public_dns
}
output "vpc_id" {
description = "The ID of the VPC"
value = aws_vpc.web_vpc.id
}
output "public_subnet" {
description = "The ID of the Public subnet"
value = aws_subnet.web_subnet.id
}
output "public_subnet2" {
description = "The ID of the Public subnet"
value = aws_subnet.web_subnet2.id
}