forked from auerswal/pubcloud2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvni.tf
290 lines (263 loc) · 6.94 KB
/
vni.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Terraform configuration for AWS virtual network infrastructure.
# Copyright (C) 2020 Erik Auerswald <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# providers - AWS in this case, region from AWS CLI is ignored
provider "aws" {
version = "~> 2.52"
profile = "default"
region = "eu-central-1"
}
### variables
# select AMI flavor for VMs
variable "ami_owner" {
default = "099720109477"
}
variable "ami_name" {
default = "ubuntu/images/hvm-ssd/ubuntu-*-18.04-amd64-server-????????"
}
# CIDR prefixes to use
variable "vpc_prefix" {
default = "10.42.0.0/16"
}
variable "priv_prefix" {
default = "10.42.0.0/24"
}
variable "pub_prefix" {
default = "10.42.255.0/24"
}
### data sources
# AMI ID for the three servers
data "aws_ami" "gnu_linux_image" {
owners = [var.ami_owner]
most_recent = true
filter {
name = "name"
values = [var.ami_name]
}
filter {
name = "state"
values = ["available"]
}
}
### resources
# public SSH key for remote access to EC2 instances
resource "aws_key_pair" "course_ssh_key" {
key_name = "tf-pubcloud2020"
public_key = file("../../../pubcloud2020_rsa_id.pub")
}
# a new VPC for this deployment
resource "aws_vpc" "ex4_vpc" {
cidr_block = var.vpc_prefix
enable_dns_support = true
enable_dns_hostnames = true
# dedicated hardware not needed -> use default tenancy
instance_tenancy = "default"
tags = {
Name = "Ex. 4 VPC"
}
}
# a new (public) subnet in the new VPC
resource "aws_subnet" "ex4_public" {
vpc_id = aws_vpc.ex4_vpc.id
cidr_block = var.pub_prefix
map_public_ip_on_launch = true
tags = {
Name = "Ex. 4 public subnet"
}
}
# a new (private) subnet in the new VPC
resource "aws_subnet" "ex4_private" {
vpc_id = aws_vpc.ex4_vpc.id
availability_zone = aws_subnet.ex4_public.availability_zone
cidr_block = var.priv_prefix
tags = {
Name = "Ex. 4 private subnet"
}
}
# a new Internet Gateway for the VPC
resource "aws_internet_gateway" "ex4_igw" {
vpc_id = aws_vpc.ex4_vpc.id
tags = {
Name = "Ex. 4 Internet gateway"
}
}
# a new route table for the public subnet with default route to the IGW
resource "aws_route_table" "ex4_rt" {
vpc_id = aws_vpc.ex4_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.ex4_igw.id
}
tags = {
Name = "Ex. 4 route table for Internet access"
}
}
# associate the route table with the public subnet
resource "aws_route_table_association" "rt2public" {
subnet_id = aws_subnet.ex4_public.id
route_table_id = aws_route_table.ex4_rt.id
}
# default Security Group of the new VPC
resource "aws_default_security_group" "def_sg" {
vpc_id = aws_vpc.ex4_vpc.id
ingress {
self = true
from_port = 0
to_port = 0
protocol = "-1"
description = "Allow everything inside the SG"
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow SSH from the Internet"
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow HTTP from the Internet"
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow HTTPS from the Internet"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow Internet access for, e.g., updates"
}
tags = {
Name = "Ex. 4 default Security Group"
}
}
# web server EC2 instance
resource "aws_instance" "ex4_web" {
depends_on = [aws_internet_gateway.ex4_igw]
ami = data.aws_ami.gnu_linux_image.id
instance_type = "t2.micro"
subnet_id = aws_subnet.ex4_public.id
key_name = aws_key_pair.course_ssh_key.id
user_data = file("web_server.cloud-config")
tags = {
Name = "Ex. 4 web server"
}
}
# jump host EC2 instance
resource "aws_instance" "ex4_jump" {
depends_on = [aws_internet_gateway.ex4_igw]
ami = data.aws_ami.gnu_linux_image.id
instance_type = "t2.micro"
subnet_id = aws_subnet.ex4_public.id
key_name = aws_key_pair.course_ssh_key.id
user_data = file("jump_host.cloud-config")
tags = {
Name = "Ex. 4 jump host"
}
}
# another EC2 instance
resource "aws_instance" "ex4_other" {
ami = data.aws_ami.gnu_linux_image.id
instance_type = "t2.micro"
subnet_id = aws_subnet.ex4_private.id
key_name = aws_key_pair.course_ssh_key.id
user_data = file("another.cloud-config")
tags = {
Name = "Ex. 4 private host"
}
}
# elastic IP address
resource "aws_eip" "ex4_eip" {
depends_on = [aws_internet_gateway.ex4_igw]
instance = aws_instance.ex4_web.id
vpc = true
}
# elastic network interface
resource "aws_network_interface" "ex4_eni" {
subnet_id = aws_subnet.ex4_private.id
attachment {
instance = aws_instance.ex4_jump.id
device_index = 1
}
}
### outputs
# CIDR prefixes
output "VPC_prefix" {
value = aws_vpc.ex4_vpc.cidr_block
}
output "private_subnet_prefix" {
value = aws_subnet.ex4_private.cidr_block
}
output "public_subnet_prefix" {
value = aws_subnet.ex4_public.cidr_block
}
# web server info (probably wrong b/c of EIP)
output "web_server_name" {
value = aws_instance.ex4_web.public_dns
}
output "web_server_ip" {
value = aws_instance.ex4_web.public_ip
}
output "web_server_private_name" {
value = aws_instance.ex4_web.private_dns
}
output "web_server_private_ip" {
value = aws_instance.ex4_web.private_ip
}
# jump host info
output "jump_host_name" {
value = aws_instance.ex4_jump.public_dns
}
output "jump_host_ip" {
value = aws_instance.ex4_jump.public_ip
}
output "jump_host_privat_name" {
value = aws_instance.ex4_jump.private_dns
}
output "jump_host_privat_ip" {
value = aws_instance.ex4_jump.private_ip
}
# private host info
output "private_host_name" {
value = aws_instance.ex4_other.private_dns
}
output "private_host_ip" {
value = aws_instance.ex4_other.private_ip
}
# EIP info
output "eip_ip" {
value = aws_eip.ex4_eip.public_ip
}
output "eip_name" {
value = aws_eip.ex4_eip.public_dns
}
output "eip_private_ip" {
value = aws_eip.ex4_eip.private_ip
}
output "eip_private_name" {
value = aws_eip.ex4_eip.private_dns
}
# ENI info
output "eni_private_ip" {
value = aws_network_interface.ex4_eni.private_ip
}