-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.tf
257 lines (200 loc) · 6.34 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
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
resource "oci_identity_compartment" "this" {
compartment_id = var.tenancy_ocid
description = var.name
name = replace(var.name, " ", "-")
enable_delete = true
}
resource "random_integer" "this" {
min = 0
max = 255
}
resource "oci_core_vcn" "this" {
compartment_id = oci_identity_compartment.this.id
cidr_blocks = [coalesce(var.cidr_block, "192.168.${random_integer.this.result}.0/24")]
display_name = var.name
dns_label = "vcn"
}
resource "oci_core_internet_gateway" "this" {
compartment_id = oci_identity_compartment.this.id
vcn_id = oci_core_vcn.this.id
display_name = oci_core_vcn.this.display_name
}
resource "oci_core_default_route_table" "this" {
manage_default_resource_id = oci_core_vcn.this.default_route_table_id
display_name = oci_core_vcn.this.display_name
route_rules {
network_entity_id = oci_core_internet_gateway.this.id
description = "Default route"
destination = "0.0.0.0/0"
}
}
resource "oci_core_default_security_list" "this" {
manage_default_resource_id = oci_core_vcn.this.default_security_list_id
dynamic "ingress_security_rules" {
for_each = [22, 80, 443]
iterator = port
content {
protocol = local.protocol_number.tcp
source = "0.0.0.0/0"
description = "SSH and HTTPS traffic from any origin"
tcp_options {
max = port.value
min = port.value
}
}
}
egress_security_rules {
destination = "0.0.0.0/0"
protocol = "all"
description = "All traffic to any destination"
}
}
resource "oci_core_subnet" "this" {
cidr_block = oci_core_vcn.this.cidr_blocks.0
compartment_id = oci_identity_compartment.this.id
vcn_id = oci_core_vcn.this.id
display_name = oci_core_vcn.this.display_name
dns_label = "subnet"
}
resource "oci_core_network_security_group" "this" {
compartment_id = oci_identity_compartment.this.id
vcn_id = oci_core_vcn.this.id
display_name = oci_core_vcn.this.display_name
}
resource "oci_core_network_security_group_security_rule" "this" {
direction = "INGRESS"
network_security_group_id = oci_core_network_security_group.this.id
protocol = local.protocol_number.icmp
source = "0.0.0.0/0"
}
data "oci_identity_availability_domains" "this" {
compartment_id = var.tenancy_ocid
}
resource "random_shuffle" "this" {
input = data.oci_identity_availability_domains.this.availability_domains[*].name
result_count = 1
}
data "oci_core_shapes" "this" {
for_each = toset(data.oci_identity_availability_domains.this.availability_domains[*].name)
compartment_id = oci_identity_compartment.this.id
availability_domain = each.key
}
data "cloudinit_config" "this" {
for_each = local.instance
part {
content = yamlencode(each.value.user_data)
content_type = "text/cloud-config"
}
}
data "oci_core_images" "this" {
for_each = local.instance
compartment_id = oci_identity_compartment.this.id
operating_system = each.value.operating_system
shape = each.value.shape
sort_by = "DISPLAYNAME"
sort_order = "DESC"
state = "AVAILABLE"
}
resource "oci_core_instance" "ubuntu" {
count = 2
availability_domain = one(
[
for m in data.oci_core_shapes.this :
m.availability_domain
if contains(m.shapes[*].name, local.instance.ubuntu.shape)
]
)
compartment_id = oci_identity_compartment.this.id
shape = local.instance.ubuntu.shape
display_name = "Ubuntu ${count.index + 1}"
preserve_boot_volume = false
metadata = {
ssh_authorized_keys = var.ssh_public_key
user_data = data.cloudinit_config.this["ubuntu"].rendered
}
agent_config {
are_all_plugins_disabled = true
is_management_disabled = true
is_monitoring_disabled = true
}
create_vnic_details {
display_name = "Ubuntu ${count.index + 1}"
hostname_label = "ubuntu-${count.index + 1}"
nsg_ids = [oci_core_network_security_group.this.id]
subnet_id = oci_core_subnet.this.id
}
source_details {
source_id = data.oci_core_images.this["ubuntu"].images.0.id
source_type = "image"
boot_volume_size_in_gbs = 50
}
lifecycle {
ignore_changes = [source_details.0.source_id]
}
}
resource "oci_core_instance" "oracle" {
availability_domain = random_shuffle.this.result.0
compartment_id = oci_identity_compartment.this.id
shape = local.instance.oracle.shape
display_name = "Oracle Linux"
preserve_boot_volume = false
metadata = {
ssh_authorized_keys = var.ssh_public_key
user_data = data.cloudinit_config.this["oracle"].rendered
}
agent_config {
are_all_plugins_disabled = true
is_management_disabled = true
is_monitoring_disabled = true
}
create_vnic_details {
assign_public_ip = false
display_name = "Oracle Linux"
hostname_label = "oracle-linux"
nsg_ids = [oci_core_network_security_group.this.id]
subnet_id = oci_core_subnet.this.id
}
shape_config {
memory_in_gbs = 24
ocpus = 4
}
source_details {
source_id = data.oci_core_images.this["oracle"].images.0.id
source_type = "image"
boot_volume_size_in_gbs = 100
}
lifecycle {
ignore_changes = [source_details.0.source_id]
}
}
data "oci_core_private_ips" "this" {
ip_address = oci_core_instance.oracle.private_ip
subnet_id = oci_core_subnet.this.id
}
resource "oci_core_public_ip" "this" {
compartment_id = oci_identity_compartment.this.id
lifetime = "RESERVED"
display_name = oci_core_instance.oracle.display_name
private_ip_id = data.oci_core_private_ips.this.private_ips.0.id
}
resource "oci_core_volume_backup_policy" "this" {
compartment_id = oci_identity_compartment.this.id
display_name = "Daily"
schedules {
backup_type = "INCREMENTAL"
hour_of_day = 0
offset_type = "STRUCTURED"
period = "ONE_DAY"
retention_seconds = 86400
time_zone = "REGIONAL_DATA_CENTER_TIME"
}
}
resource "oci_core_volume_backup_policy_assignment" "this" {
count = 3
asset_id = (
count.index < 2 ?
oci_core_instance.ubuntu[count.index].boot_volume_id :
oci_core_instance.oracle.boot_volume_id
)
policy_id = oci_core_volume_backup_policy.this.id
}