-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
210 lines (177 loc) · 4.81 KB
/
main.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
provider "oci" {
fingerprint = var.fingerprint
region = var.region
tenancy_ocid = var.tenancy_ocid
user_ocid = var.user_ocid
private_key_path = var.private_key_path
}
data "oci_identity_availability_domain" "ad" {
compartment_id = var.tenancy_ocid
ad_number = 1
}
// Networking
resource "oci_core_virtual_network" "vcn" {
cidr_block = "10.1.0.0/16"
compartment_id = var.compartment_ocid
display_name = "VCN"
dns_label = "vcn"
}
resource "oci_core_subnet" "subnet" {
cidr_block = "10.1.20.0/24"
display_name = "Subnet"
dns_label = "subnet"
security_list_ids = [oci_core_security_list.security_list.id]
compartment_id = var.compartment_ocid
vcn_id = oci_core_virtual_network.vcn.id
route_table_id = oci_core_route_table.route_table.id
dhcp_options_id = oci_core_virtual_network.vcn.default_dhcp_options_id
}
resource "oci_core_internet_gateway" "internet_gateway" {
compartment_id = var.compartment_ocid
display_name = "IG"
vcn_id = oci_core_virtual_network.vcn.id
}
resource "oci_core_route_table" "route_table" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_virtual_network.vcn.id
display_name = "RouteTable"
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_internet_gateway.internet_gateway.id
}
}
// Firewall rules
// See https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
// for protocol numbers
//
// TCP = 6
// UDP = 17
resource "oci_core_security_list" "security_list" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_virtual_network.vcn.id
display_name = "SecurityList"
egress_security_rules {
protocol = "6"
destination = "0.0.0.0/0"
}
ingress_security_rules {
protocol = "6"
source = "0.0.0.0/0"
description = "SSH"
tcp_options {
min = "22"
max = "22"
}
}
ingress_security_rules {
protocol = "6"
source = "0.0.0.0/0"
description = "HTTP"
tcp_options {
min = "80"
max = "80"
}
}
ingress_security_rules {
protocol = "6"
source = "0.0.0.0/0"
description = "HTTPS"
tcp_options {
min = "443"
max = "443"
}
}
ingress_security_rules {
protocol = "6"
source = "0.0.0.0/0"
description = "Caddy HTTP"
tcp_options {
min = "8080"
max = "8080"
}
}
ingress_security_rules {
protocol = "6"
source = "0.0.0.0/0"
description = "Caddy HTTPS"
tcp_options {
min = "4443"
max = "4443"
}
}
ingress_security_rules {
protocol = "17"
source = "0.0.0.0/0"
description = "Torrent DHT"
udp_options {
min = "6881"
max = "6881"
}
}
ingress_security_rules {
protocol = "6"
source = "0.0.0.0/0"
description = "Torrent TCP"
tcp_options {
min = "6890"
max = "6999"
}
}
ingress_security_rules {
protocol = "17"
source = "0.0.0.0/0"
description = "Mosh"
udp_options {
min = "60000"
max = "61000"
}
}
}
// Compute
resource "oci_core_instance" "free_instance" {
availability_domain = data.oci_identity_availability_domain.ad.name
compartment_id = var.compartment_ocid
display_name = "freeInstance"
shape = var.instance_shape
shape_config {
ocpus = var.instance_ocpus
memory_in_gbs = var.instance_shape_config_memory_in_gbs
}
create_vnic_details {
subnet_id = oci_core_subnet.subnet.id
display_name = "primaryvnic"
assign_public_ip = true
hostname_label = "freeinstance"
}
source_details {
source_type = "image"
source_id = lookup(data.oci_core_images.images.images[0], "id")
}
metadata = {
ssh_authorized_keys = (var.ssh_public_key != "") ? file(var.ssh_public_key) : tls_private_key.compute_ssh_key[0].public_key_openssh
}
}
output "compute_instance_ip" {
value = oci_core_instance.free_instance.public_ip
}
// Generate a pair for authentication if none is provided
resource "tls_private_key" "compute_ssh_key" {
count = var.ssh_public_key == "" ? 1 : 0
algorithm = "RSA"
rsa_bits = 2048
}
output "generated_private_key_pem" {
value = (var.ssh_public_key != "") ? var.ssh_public_key : tls_private_key.compute_ssh_key[0].private_key_pem
sensitive = true
}
// Pick up the last Oracle Linux image for provisioning
# See https://docs.oracle.com/iaas/images/
data "oci_core_images" "images" {
compartment_id = var.compartment_ocid
operating_system = "Oracle Linux"
operating_system_version = "8"
shape = var.instance_shape
sort_by = "TIMECREATED"
sort_order = "DESC"
}