DBA Hub

📋Steps in this guide1/5

Terraform : Oracle Cloud Infrastructure (OCI) Volume

This article describes how to create a volume and attach it to a compute instance on Oracle Cloud Infrastructure (OCI) using Terraform.

oracle miscconfigurationintermediate
by OracleDba
12 views
1

Create Working Directory

Create a new working directory and switch to that directory. In a previous article ( here ) we discussed the creation of an OCI provider. Copy the OCI provider information into this new working directory.

Code/Command (click line numbers to comment):

1
2
3
4
5
mkdir \git\oraclebase\terraform\oci\oci_volume
cd \git\oraclebase\terraform\oci\oci_volume

copy \git\oraclebase\terraform\oci\oci_provider\*.tf .
copy \git\oraclebase\terraform\oci\oci_provider\*.tfvars .
2

oci_volume.tf

Create a file called "oci_volume.tf" with the following contents. The file begins with variable definitions. We could set default values for these variables, or use literal values directly in the provider definition, but we don't want sensitive information checked into version control, so it makes sense to separate out variable values from the script. The resources section defines the volume and attachment using the input variables. The outputs section allows us to see information about the volume that's been created, including the ID of the volume and the attachment. The variables, resources and outputs sections can be split into separate files if you find that organisation easier. It may help for more complex definitions. The full list of parameters and outputs available can be found here ( oci_core_volume , oci_core_volume_attachment ).

Code/Command (click line numbers to comment):

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
# Variables
variable "compartment_id"              { type = string }
variable "volume_display_name"         { type = string }
variable "size_in_gbs"                 { type = number }
variable "attachment_display_name"     { type = string }
variable "attachment_device"           { type = string }
variable "compute_availability_domain" { type = string }
variable "compute_instance_id"         { type = string }


resource "oci_core_volume" "tf_volume" {
  # Required
  compartment_id      = var.compartment_id

  # Optional
  display_name        = var.volume_display_name
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[var.availability_domain_number].name
  size_in_gbs         = var.size_in_gbs
}

resource "oci_core_volume_attachment" "tf_volume_attachment" {
    #Required
    attachment_type = "iscsi"
    instance_id     = var.compute_instance_id
    volume_id       = oci_core_volume.tf_volume.id

    #Optional
    device                            = var.attachment_device
    is_agent_auto_iscsi_login_enabled = true
    display_name                      = var.attachment_display_name
}


# Outputs
output "volume_id" {
  value = oci_core_volume.tf_volume.id
}

output "volume_attachement_id" {
  value = oci_core_volume_attachment.tf_volume_attachment.id
}
3

oci_volumne_variables.auto.tfvars

There are a number of ways to supply values for input variables ( see here ). In this example we'll use a ".auto.tfvars" file. We won't check this script into version control as it contains sensitive information. Create a file called "oci_volume_variables.auto.tfvars" with the following contents. Adjust the values to match your desired volume details. The compartment_id is the OCID of the compartment that will house the volume. You must adjust it with a valid value from your Oracle Cloud account. You would not normally use the root compartment for this. You can get the ID of a compartment from your Oracle Cloud account as follows. - Top-Left Hamburger > Identity & Security > Compartments - Click on the compartment of interest. - Click the "Copy" link next to "OCID". The availability_domain_number is whatever availability domain you used when creating your computer instance. The compute_instance_id can be found as follows. - Top-Left Hamburger > Computer > Instances - Click on the instance of interest. - Click the "Copy" link next to "OCID". The volume_display_name is the text name displayed in the OCI portal. It doesn't affect the function. The size_in_gbs must be a minimum of 50G. The attachment_display_name is the text name displayed in the OCI portal. It doesn't affect the function. The attachment_device is a device name used on the VM. The "/dev/oracleoci/oraclevda" device name is used by the main image of the compute instance, so you must alter the last letter to any value from "b" onward. In this case we are using "/dev/oracleoci/oraclevdb".

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
compartment_id              = "ocid1.compartment.oc1..aaaaaaaave..."
availability_domain_number  = 0
compute_instance_id         = "ocid1.instance.oc1.uk-london-1.anwgiljrwam..."
volume_display_name         = "website-u01-volume"
size_in_gbs                 = 50
attachment_display_name     = "website-u01-volume-attachement"
attachment_device           = "/dev/oracleoci/oraclevdb"
4

Build and Attach the OCI Volume

Initialize the working directory using the command. Use the command to test the execution plan. Use the command to create the OCI compartment. Check the Oracle Cloud account to see the new volume, and see it is attached to the compute instance.

Code/Command (click line numbers to comment):

1
2
3
4
5
terraform init

terraform plan

terraform apply
5

Compute Instance Configuration

Provided the associated compute instance has block volume management enabled, we will see the device appear on our compute instance. Block volume management can be turned on for the compute instance in the web interface, or by including the following agent configuration in the oci_core_instance definition. Assuming everything has worked, the device will be visible. It can be used like any other disk. For example, we might partition it, create a file system on it and mount it as follows. Notice the "_netdev" mount option in the "/etc/fstab". The iscsi attachment may happen after the OS attempts to mount the disks, so it will see the device as being unavailable. Adding the "_netdev" options makes sure it gets picked up, even if the attachment is delayed. Without this your disks may not be present after a reboot, and you would have to manually mount them. The potential delay in mounting the disc may mean they are not present when other services attempt to start. To counter this we may want to add a delay to those services. If we wanted to delay the startup of the Apache HTTP Server service, we would edit the "/usr/lib/systemd/system/httpd.service" file, and add this to the "[Service]" section. The following command makes sure the service change takes effect. For more information see: - oci_core_volume - oci_core_volume_attachment - Terraform : All Articles Hope this helps. Regards Tim...

Code/Command (click line numbers to comment):

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
agent_config {
    #Optional
    is_management_disabled = false
    plugins_config {
      #Required
      desired_state = "ENABLED"
      name          = "Block Volume Management"
    }
  }

# ls /dev/oracleoci/oraclevdb
/dev/oracleoci/oraclevdb
#

# Partition the disk and make a XFS file system in the partition.
echo -e "n\np\n1\n\n\nw" | fdisk /dev/oracleoci/oraclevdb
mkfs.xfs -f /dev/oracleoci/oraclevdb1

# Add it to /etc/fstab.
PARTUUID=`blkid -o export /dev/oracleoci/oraclevdb11 | grep PARTUUID`
mkdir /u01
echo "${PARTUUID}  /u01    xfs    defaults,_netdev,nofail 0 2" >> /etc/fstab
systemctl daemon-reload

# Mount it.
mount /u01

ExecStartPre=/bin/sleep 60

systemctl daemon-reload

Comments (0)

Please to add comments

No comments yet. Be the first to comment!