-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.tf
More file actions
340 lines (280 loc) · 13.7 KB
/
instance.tf
File metadata and controls
340 lines (280 loc) · 13.7 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
// Licensed under the Mozilla Public License v2.0
variable "tenancy_ocid" {
}
variable "instance_prefix" {
default = "cpg-oci-"
}
variable "user_ocid" {
}
variable "fingerprint" {
}
variable "private_key" {
}
variable "region" {
}
variable "compartment_ocid" {
}
variable "ssh_public_key" {
}
variable "ssh_private_key" {
}
provider "oci" {
tenancy_ocid = var.tenancy_ocid
user_ocid = var.user_ocid
fingerprint = var.fingerprint
private_key = var.private_key
region = var.region
}
# Defines the number of instances to deploy
variable "num_instances" {
default = "3"
}
# Defines the number of volumes to create and attach to each instance
# NOTE: Changing this value after applying it could result in re-attaching existing volumes to different instances.
# This is a result of using 'count' variables to specify the volume and instance IDs for the volume attachment resource.
variable "num_iscsi_volumes_per_instance" {
default = "1"
}
variable "num_paravirtualized_volumes_per_instance" {
default = "2"
}
variable "instance_shape" {
default = "VM.Standard.E3.Flex"
}
variable "instance_ocpus" {
default = 1
}
variable "instance_shape_config_memory_in_gbs" {
default = 1
}
variable "instance_image_ocid" {
type = map(string)
default = {
us-phoenix-1 = "ocid1.image.oc1.phx.aaaaaaaaoqj42sokaoh42l76wsyhn3k2beuntrh5maj3gmgmzeyr55zzrwwa"
us-ashburn-1 = "ocid1.image.oc1.iad.aaaaaaaageeenzyuxgia726xur4ztaoxbxyjlxogdhreu3ngfj2gji3bayda"
eu-frankfurt-1 = "ocid1.image.oc1.eu-frankfurt-1.aaaaaaaaitzn6tdyjer7jl34h2ujz74jwy5nkbukbh55ekp6oyzwrtfa4zma"
uk-london-1 = "ocid1.image.oc1.uk-london-1.aaaaaaaa32voyikkkzfxyo4xbdmadc2dmvorfxxgdhpnk6dw64fa3l4jh7wa"
}
}
variable "flex_instance_image_ocid" {
type = map(string)
default = {
us-phoenix-1 = "ocid1.image.oc1.phx.aaaaaaaa6hooptnlbfwr5lwemqjbu3uqidntrlhnt45yihfj222zahe7p3wq"
us-ashburn-1 = "ocid1.image.oc1.iad.aaaaaaaa6tp7lhyrcokdtf7vrbmxyp2pctgg4uxvt4jz4vc47qoc2ec4anha"
eu-frankfurt-1 = "ocid1.image.oc1.eu-frankfurt-1.aaaaaaaadvi77prh3vjijhwe5xbd6kjg3n5ndxjcpod6om6qaiqeu3csof7a"
uk-london-1 = "ocid1.image.oc1.uk-london-1.aaaaaaaaw5gvriwzjhzt2tnylrfnpanz5ndztyrv3zpwhlzxdbkqsjfkwxaq"
}
}
variable "db_size" {
default = "50" # size in GBs
}
variable "tag_namespace_description" {
default = "Just a test"
}
variable "tag_namespace_name" {
default = "testexamples-tag-namespace"
}
resource "oci_core_instance" "test_instance" {
count = var.num_instances
availability_domain = data.oci_identity_availability_domain.ad.name
compartment_id = var.compartment_ocid
display_name = "${var.instance_prefix}${count.index}"
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.test_subnet.id
display_name = "Primaryvnic"
assign_public_ip = true
assign_private_dns_record = true
hostname_label = "exampleinstance${count.index}"
}
source_details {
source_type = "image"
source_id = var.flex_instance_image_ocid[var.region]
# Apply this to set the size of the boot volume that is created for this instance.
# Otherwise, the default boot volume size of the image is used.
# This should only be specified when source_type is set to "image".
#boot_volume_size_in_gbs = "60"
}
# Apply the following flag only if you wish to preserve the attached boot volume upon destroying this instance
# Setting this and destroying the instance will result in a boot volume that should be managed outside of this config.
# When changing this value, make sure to run 'terraform apply' so that it takes effect before the resource is destroyed.
#preserve_boot_volume = true
metadata = {
ssh_authorized_keys = var.ssh_public_key
user_data = base64encode(file("./userdata/bootstrap"))
}
defined_tags = {
#"${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag2.name}" = "awesome-app-server"
}
freeform_tags = {
"freeformkey${count.index}" = "freeformvalue${count.index}"
}
preemptible_instance_config {
preemption_action {
type = "TERMINATE"
preserve_boot_volume = false
}
}
timeouts {
create = "60m"
}
}
# Define the volumes that are attached to the compute instances.
resource "oci_core_volume" "test_block_volume" {
count = var.num_instances * var.num_iscsi_volumes_per_instance
availability_domain = data.oci_identity_availability_domain.ad.name
compartment_id = var.compartment_ocid
display_name = "TestBlock${count.index}"
size_in_gbs = var.db_size
}
resource "oci_core_volume_attachment" "test_block_attach" {
count = var.num_instances * var.num_iscsi_volumes_per_instance
attachment_type = "iscsi"
instance_id = oci_core_instance.test_instance[floor(count.index / var.num_iscsi_volumes_per_instance)].id
volume_id = oci_core_volume.test_block_volume[count.index].id
device = count.index == 0 ? "/dev/oracleoci/oraclevdb" : ""
# Set this to enable CHAP authentication for an ISCSI volume attachment. The oci_core_volume_attachment resource will
# contain the CHAP authentication details via the "chap_secret" and "chap_username" attributes.
use_chap = true
# Set this to attach the volume as read-only.
#is_read_only = true
}
resource "oci_core_volume" "test_block_volume_paravirtualized" {
count = var.num_instances * var.num_paravirtualized_volumes_per_instance
availability_domain = data.oci_identity_availability_domain.ad.name
compartment_id = var.compartment_ocid
display_name = "TestBlockParavirtualized${count.index}"
size_in_gbs = var.db_size
}
resource "oci_core_volume_attachment" "test_block_volume_attach_paravirtualized" {
count = var.num_instances * var.num_paravirtualized_volumes_per_instance
attachment_type = "paravirtualized"
instance_id = oci_core_instance.test_instance[floor(count.index / var.num_paravirtualized_volumes_per_instance)].id
volume_id = oci_core_volume.test_block_volume_paravirtualized[count.index].id
# Set this to attach the volume as read-only.
#is_read_only = true
}
resource "oci_core_volume_backup_policy_assignment" "policy" {
count = var.num_instances
asset_id = oci_core_instance.test_instance[count.index].boot_volume_id
policy_id = data.oci_core_volume_backup_policies.test_predefined_volume_backup_policies.volume_backup_policies[0].id
}
resource "null_resource" "remote-exec" {
depends_on = [
oci_core_instance.test_instance,
oci_core_volume_attachment.test_block_attach,
]
count = var.num_instances * var.num_iscsi_volumes_per_instance
provisioner "remote-exec" {
connection {
agent = false
timeout = "30m"
host = oci_core_instance.test_instance[count.index % var.num_instances].public_ip
user = "opc"
private_key = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA06e4Lp+q0qwRFvdGRtmKQZB+10tuSTvw2ZRCJeP8iAvlpi/W\nNCOccSy1PgVrU+mTBdO0nr5Zjc/UB2KnPLkcIkljENF0LwxhObd8SErpOdp1a0UT\nBrxyBkXqqNG04Xv3tdGRfEpY/ytwi1pFO/XsS9A1DUDL7/nlIysl4nevTVNuY5vj\nY6zGEeo8OvSDynv3wR36N85fZzQ+VghM8nZVTFfuxrq2VDJTiafR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0G0bEnxE0GdSTH4drZJJPQ3ePR0Hcv+5k47ysGL\nhm5eU2O7MEfmqHKVmeCHo0lesCy1JM+Q4R4hDTBlAGiHG9HViUXQ30HwE8a6j2Ls\nnpbwuAkCgYADBvOjCqPrC9b96ONG1MM+uAgx2eL8CbOL4jV+aUwFU2CV2qywLCCs\nCYcQg1XA94k61ZEF1umCr9rxpUg1erAQo8bc6Vz/HPrOpIRTvSBMpjwYYsk4iGqz\nFtx6PbolEAVXOlp+eRyvYqIyoTBlkivN5bHCu9yQY5IVeUXsDAG7VQ==\n-----END RSA PRIVATE KEY-----"
}
inline = [
"touch ~/IMadeAFile.Right.Here",
"sudo iscsiadm -m node -o new -T ${oci_core_volume_attachment.test_block_attach[count.index].iqn} -p ${oci_core_volume_attachment.test_block_attach[count.index].ipv4}:${oci_core_volume_attachment.test_block_attach[count.index].port}",
"sudo iscsiadm -m node -o update -T ${oci_core_volume_attachment.test_block_attach[count.index].iqn} -n node.startup -v automatic",
"sudo iscsiadm -m node -T ${oci_core_volume_attachment.test_block_attach[count.index].iqn} -p ${oci_core_volume_attachment.test_block_attach[count.index].ipv4}:${oci_core_volume_attachment.test_block_attach[count.index].port} -o update -n node.session.auth.authmethod -v CHAP",
"sudo iscsiadm -m node -T ${oci_core_volume_attachment.test_block_attach[count.index].iqn} -p ${oci_core_volume_attachment.test_block_attach[count.index].ipv4}:${oci_core_volume_attachment.test_block_attach[count.index].port} -o update -n node.session.auth.username -v ${oci_core_volume_attachment.test_block_attach[count.index].chap_username}",
"sudo iscsiadm -m node -T ${oci_core_volume_attachment.test_block_attach[count.index].iqn} -p ${oci_core_volume_attachment.test_block_attach[count.index].ipv4}:${oci_core_volume_attachment.test_block_attach[count.index].port} -o update -n node.session.auth.password -v ${oci_core_volume_attachment.test_block_attach[count.index].chap_secret}",
"sudo iscsiadm -m node -T ${oci_core_volume_attachment.test_block_attach[count.index].iqn} -p ${oci_core_volume_attachment.test_block_attach[count.index].ipv4}:${oci_core_volume_attachment.test_block_attach[count.index].port} -l",
]
}
}
/*
# Gets the boot volume attachments for each instance
data "oci_core_boot_volume_attachments" "test_boot_volume_attachments" {
depends_on = [oci_core_instance.test_instance]
count = var.num_instances
availability_domain = oci_core_instance.test_instance[count.index].availability_domain
compartment_id = var.compartment_ocid
instance_id = oci_core_instance.test_instance[count.index].id
}
*/
data "oci_core_instance_devices" "test_instance_devices" {
count = var.num_instances
instance_id = oci_core_instance.test_instance[count.index].id
}
data "oci_core_volume_backup_policies" "test_predefined_volume_backup_policies" {
filter {
name = "display_name"
values = [
"silver",
]
}
}
# Output the private and public IPs of the instance
output "instance_private_ips" {
value = [oci_core_instance.test_instance.*.private_ip]
}
output "instance_public_ips" {
value = [oci_core_instance.test_instance.*.public_ip]
}
# Output the boot volume IDs of the instance
output "boot_volume_ids" {
value = [oci_core_instance.test_instance.*.boot_volume_id]
}
# Output all the devices for all instances
output "instance_devices" {
value = [data.oci_core_instance_devices.test_instance_devices.*.devices]
}
# Output the chap secret information for ISCSI volume attachments. This can be used to output
# CHAP information for ISCSI volume attachments that have "use_chap" set to true.
#output "IscsiVolumeAttachmentChapUsernames" {
# value = [oci_core_volume_attachment.test_block_attach.*.chap_username]
#}
#
#output "IscsiVolumeAttachmentChapSecrets" {
# value = [oci_core_volume_attachment.test_block_attach.*.chap_secret]
#}
output "silver_policy_id" {
value = data.oci_core_volume_backup_policies.test_predefined_volume_backup_policies.volume_backup_policies[0].id
}
/*
output "attachment_instance_id" {
value = data.oci_core_boot_volume_attachments.test_boot_volume_attachments.*.instance_id
}
*/
resource "oci_core_vcn" "test_vcn" {
cidr_block = "10.1.0.0/16"
compartment_id = var.compartment_ocid
display_name = "TestVcn"
dns_label = "testvcn"
}
resource "oci_core_internet_gateway" "test_internet_gateway" {
compartment_id = var.compartment_ocid
display_name = "TestInternetGateway"
vcn_id = oci_core_vcn.test_vcn.id
}
resource "oci_core_default_route_table" "default_route_table" {
manage_default_resource_id = oci_core_vcn.test_vcn.default_route_table_id
display_name = "DefaultRouteTable"
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_internet_gateway.test_internet_gateway.id
}
}
resource "oci_core_subnet" "test_subnet" {
availability_domain = data.oci_identity_availability_domain.ad.name
cidr_block = "10.1.20.0/24"
display_name = "TestSubnet"
dns_label = "testsubnet"
security_list_ids = [oci_core_vcn.test_vcn.default_security_list_id]
compartment_id = var.compartment_ocid
vcn_id = oci_core_vcn.test_vcn.id
route_table_id = oci_core_vcn.test_vcn.default_route_table_id
dhcp_options_id = oci_core_vcn.test_vcn.default_dhcp_options_id
}
data "oci_identity_availability_domain" "ad" {
compartment_id = var.tenancy_ocid
ad_number = 1
}