Skip to content

Commit 7c9cab1

Browse files
committed
Update instance examples to show interpolation syntax
This shows usage of count variable and math operations to set-up instances that can be attached to multiple block storage volumes.
1 parent 2cf466d commit 7c9cab1

File tree

7 files changed

+42
-34
lines changed

7 files changed

+42
-34
lines changed

docs/examples/compute/instance/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
# | |_| | _ < / ___ | |___| |___| |___
55
# \___/|_| \_/_/ \_\____|_____|_____|
66
***
7-
## Manage an instance
8-
This example launches an instance, includes a user-data script in the instance launch, remote executes a command, and outputs the public and private IP address of the instance.
7+
## Manage instances with multiple attached volumes
8+
This example launches 3 instances and attaches 2 volumes per instance.
9+
10+
This is done using Terraform's [interpolation syntax](https://www.terraform.io/docs/configuration/interpolation.html) for `count` variables and `math` operations.
11+
12+
The example also includes a user-data script in the instance launch, remote executes a command on each instance to setup the volumes, and outputs the public and private IP address of the instances.
913

1014
### Using this example
1115
* Update env-vars with the required information. Most examples use the same set of environment variables so you only need to do this once.
1216
* Source env-vars
1317
* `$ . env-vars`
14-
* Update `variables.tf` with your instance options.
18+
* Update `variables.tf` with your instance options. You may also modify the `NumInstances` and `NumVolumesPerInstance` variables to change the number of instances and volumes that are launched.
1519

1620
### Files in the configuration
1721

@@ -21,6 +25,9 @@ Is used to export the environmental variables used in the configuration. These a
2125
Before you plan, apply, or destroy the configuration source the file -
2226
`$ . env-vars`
2327

28+
#### `block.tf`
29+
Defines the volumes that are attached to the compute instances.
30+
2431
#### `compute.tf`
2532
Defines the compute resource. This demo connects to the running instance
2633
so you will need to supply public/private keys to create an ssh connection.
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
resource "oci_core_volume" "TFBlock0" {
1+
resource "oci_core_volume" "TFBlock" {
2+
count = "${var.NumInstances * var.NumVolumesPerInstance}"
23
availability_domain = "${lookup(data.oci_identity_availability_domains.ADs.availability_domains[var.AD - 1],"name")}"
34
compartment_id = "${var.compartment_ocid}"
4-
display_name = "TFBlock0"
5+
display_name = "TFBlock${count.index}"
56
size_in_gbs = "${var.DBSize}"
67
}
78

8-
resource "oci_core_volume_attachment" "TFBlock0Attach" {
9+
resource "oci_core_volume_attachment" "TFBlockAttach" {
10+
count = "${var.NumInstances * var.NumVolumesPerInstance}"
911
attachment_type = "iscsi"
1012
compartment_id = "${var.compartment_ocid}"
11-
instance_id = "${oci_core_instance.TFInstance.id}"
12-
volume_id = "${oci_core_volume.TFBlock0.id}"
13+
instance_id = "${oci_core_instance.TFInstance.*.id[count.index / var.NumVolumesPerInstance]}"
14+
volume_id = "${oci_core_volume.TFBlock.*.id[count.index]}"
1315
}
1416

docs/examples/compute/instance/compute.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
resource "oci_core_instance" "TFInstance" {
2+
count = "${var.NumInstances}"
23
availability_domain = "${lookup(data.oci_identity_availability_domains.ADs.availability_domains[var.AD - 1],"name")}"
34
compartment_id = "${var.compartment_ocid}"
4-
display_name = "TFInstance"
5+
display_name = "TFInstance${count.index}"
56
image = "${var.InstanceImageOCID[var.region]}"
67
shape = "${var.InstanceShape}"
78

89
create_vnic_details {
910
subnet_id = "${oci_core_subnet.ExampleSubnet.id}"
1011
display_name = "primaryvnic"
1112
assign_public_ip = true
12-
hostname_label = "tfexampleinstance"
13+
hostname_label = "tfexampleinstance${count.index}"
1314
},
1415

1516
metadata {

docs/examples/compute/instance/datasources.tf

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,3 @@
22
data "oci_identity_availability_domains" "ADs" {
33
compartment_id = "${var.tenancy_ocid}"
44
}
5-
6-
# Gets a list of vNIC attachments on the instance
7-
data "oci_core_vnic_attachments" "InstanceVnics" {
8-
compartment_id = "${var.compartment_ocid}"
9-
availability_domain = "${lookup(data.oci_identity_availability_domains.ADs.availability_domains[var.AD - 1],"name")}"
10-
instance_id = "${oci_core_instance.TFInstance.id}"
11-
}
12-
13-
# Gets the OCID of the first (default) vNIC
14-
data "oci_core_vnic" "InstanceVnic" {
15-
vnic_id = "${lookup(data.oci_core_vnic_attachments.InstanceVnics.vnic_attachments[0],"vnic_id")}"
16-
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Output the private and public IPs of the instance
2-
3-
output "InstancePrivateIP" {
4-
value = ["${data.oci_core_vnic.InstanceVnic.private_ip_address}"]
2+
output "InstancePrivateIPs" {
3+
value = ["${oci_core_instance.TFInstance.*.private_ip}"]
54
}
65

7-
output "InstancePublicIP" {
8-
value = ["${data.oci_core_vnic.InstanceVnic.public_ip_address}"]
6+
output "InstancePublicIPs" {
7+
value = ["${oci_core_instance.TFInstance.*.public_ip}"]
98
}
10-
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
resource "null_resource" "remote-exec" {
2-
depends_on = ["oci_core_instance.TFInstance","oci_core_volume_attachment.TFBlock0Attach"]
2+
depends_on = ["oci_core_instance.TFInstance","oci_core_volume_attachment.TFBlockAttach"]
3+
count = "${var.NumInstances * var.NumVolumesPerInstance}"
34
provisioner "remote-exec" {
45
connection {
56
agent = false
67
timeout = "30m"
7-
host = "${data.oci_core_vnic.InstanceVnic.public_ip_address}"
8+
host = "${oci_core_instance.TFInstance.*.public_ip[count.index % var.NumInstances]}"
89
user = "opc"
910
private_key = "${var.ssh_private_key}"
1011
}
1112
inline = [
1213
"touch ~/IMadeAFile.Right.Here",
13-
"sudo iscsiadm -m node -o new -T ${oci_core_volume_attachment.TFBlock0Attach.iqn} -p ${oci_core_volume_attachment.TFBlock0Attach.ipv4}:${oci_core_volume_attachment.TFBlock0Attach.port}",
14-
"sudo iscsiadm -m node -o update -T ${oci_core_volume_attachment.TFBlock0Attach.iqn} -n node.startup -v automatic",
15-
"echo sudo iscsiadm -m node -T ${oci_core_volume_attachment.TFBlock0Attach.iqn} -p ${oci_core_volume_attachment.TFBlock0Attach.ipv4}:${oci_core_volume_attachment.TFBlock0Attach.port} -l >> ~/.bashrc"
14+
"sudo iscsiadm -m node -o new -T ${oci_core_volume_attachment.TFBlockAttach.*.iqn[count.index]} -p ${oci_core_volume_attachment.TFBlockAttach.*.ipv4[count.index]}:${oci_core_volume_attachment.TFBlockAttach.*.port[count.index]}",
15+
"sudo iscsiadm -m node -o update -T ${oci_core_volume_attachment.TFBlockAttach.*.iqn[count.index]} -n node.startup -v automatic",
16+
"echo sudo iscsiadm -m node -T ${oci_core_volume_attachment.TFBlockAttach.*.iqn[count.index]} -p ${oci_core_volume_attachment.TFBlockAttach.*.ipv4[count.index]}:${oci_core_volume_attachment.TFBlockAttach.*.port[count.index]} -l >> ~/.bashrc"
1617
]
1718
}
1819
}
19-

docs/examples/compute/instance/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ variable "compartment_ocid" {}
88
variable "ssh_public_key" {}
99
variable "ssh_private_key" {}
1010

11+
# Defines the number of instances to deploy
12+
variable "NumInstances" {
13+
default = "3"
14+
}
15+
16+
# Defines the number of volumes to create and attach to each instance
17+
# NOTE: Changing this value after applying it could result in re-attaching existing volumes to different instances.
18+
# This is a result of using 'count' variables to specify the volume and instance IDs for the volume attachment resource.
19+
variable "NumVolumesPerInstance" {
20+
default = "2"
21+
}
22+
1123
# Choose an Availability Domain
1224
variable "AD" {
1325
default = "1"

0 commit comments

Comments
 (0)