Terraform in OCI

What is Terraform

Terraform is an infrastructure as code software by HashiCorp.

It allows users to define a datacenter infrastructure in a high-level configuration language, from which it can create an execution plan to build the infrastructure.

Infrastructure is defined in a HCL Terraform syntax or JSON format.

Can be used with the following cloud providers. (This is not an all inclusive list.)

  • AWS
  • IBM Cloud
  • Google Cloud Platform
  • Microsoft Azure
  • Oracle Cloud Infrastructure

Example Provisioning of Linux Instance in OCI-Terraform

OCI Pre-requisites

  • IAM User
  • IAM API Fingerprint

Terraform Pre-requisites

  • Linux or other supported OS
  • Install Terraform and OCI-Provider

Set Environment

$ cat env-vars
# ####################################################################
# # Public/private keys used on the instance
# ####################################################################
export TF_VAR_ssh_public_key=$(cat /home/user/.ssh/my_opc_key.pub)
export TF_VAR_ssh_private_key=/home/user/.ssh/my_opc_key

# ####################################################################
# # Authentication
# ####################################################################
export TF_VAR_user_ocid="ocid1.user.oc1..xxxxxxxxxxx"
export TF_VAR_fingerprint="xxxxxxxxxxx"
export TF_VAR_private_key_path=/home/user/.ssh/my_api_key

Configure TF Files for Cloud Server Provisioning

In this example, an OL 7.5 VM will be provisioned.

$ ls -1
block.tf
compute.tf
datasources.tf
links.sh
outputs.tf
terraform.tfstate
userdata
variables.tf
vars-compartment.tf
vars-images.tf
vars-provider.tf
vars-region.tf
vars-ssh.tf
vars-subnets.tf
vars-tenancy.tf
vars-user.tf
vars-vcn-cidr.tf

The variables.tf is one the file that has the variable values will need to be set.
The other files contain the structure of the instance, where it is to be provisioned and the mappings for the variables being used.

$ cat variables.tf
# ######################################
# instance details
# ######################################
variable "AD" { default = "2" }
variable "privateIP" { default = "10.10.10.74" }
variable "subnet" { default = "SN2" }

variable "InstanceShape" { default = "VM.Standard2.1" }
variable "InstanceName" { default = "test01phx" }
variable "image_id" { default = "ol75-phoenix" }
# boot volume
variable "bv_size" { default = "100" }

# block volume
variable "BV1Name" { default = "test01phxBV1" }
variable "BV1Size" { default = "50" }

variable "BootStrapFile" { default = "./userdata/bootstrap" }

The variables.tf file above defines the following:

  • OL 7.5 operating system
  • Boot volume of 100G (the defualt is 50G)
  • A block volume of 50G
  • The contents of the bootstrap file will install the required packages to resize the boot volume.

In order to change the image version, subnet or other variable, please reference the vars*.tf files for the appropriate values.

Provisioning the Virtual Machine

Execute “terraform plan” to verify the tf files and the placement of the VM.

$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.oci_identity_availability_domains.ADs: Refreshing state...

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
~ update in-place
<= read (data resources)

Terraform will perform the following actions:

~ data.oci_core_vnic.InstanceVnic
id: "" => <computed>
availability_domain: "" => <computed>
compartment_id: "" => <computed>
display_name: "" => <computed>
hostname_label: "" => <computed>
is_primary: "" => <computed>
mac_address: "" => <computed>
private_ip_address: "" => <computed>
public_ip_address: "" => <computed>
skip_source_dest_check: "" => <computed>
state: "" => <computed>
subnet_id: "" => <computed>
time_created: "" => <computed>
vnic_id: "" => "${lookup(data.oci_core_vnic_attachments.InstanceVnics.vnic_attachments[0],\"vnic_id\")}"

<= data.oci_core_vnic_attachments.InstanceVnics
id: <computed>
availability_domain: "PHX-AD-2"
compartment_id: "ocid1.compartment.oc1..xxxxxxxxxxx"
instance_id: "${oci_core_instance.TFInstance.id}"
vnic_attachments.#: <computed>

+ oci_core_instance.TFInstance
id: <computed>
availability_domain: "PHX-AD-2"
boot_volume_id: <computed>
compartment_id: "ocid1.compartment.oc1..xxxxxxxxxxx"
create_vnic_details.#: "1"
create_vnic_details.0.assign_public_ip: "false"
create_vnic_details.0.display_name: "primaryvnic"
create_vnic_details.0.hostname_label: "test01phx"
create_vnic_details.0.private_ip: "10.10.10.74"
create_vnic_details.0.skip_source_dest_check: <computed>
create_vnic_details.0.subnet_id: "ocid1.subnet.oc1.phx.xxxxxxxxxxx"
display_name: "test01phx"
image: <computed>
ipxe_script: <computed>
launch_mode: <computed>
launch_options.#: <computed>
metadata.%: "2"
metadata.ssh_authorized_keys: "ssh-rsaxxxxxxxxxxx"
metadata.user_data: "xxxxxxxxxxx"
private_ip: <computed>
public_ip: <computed>
region: <computed>
shape: "VM.Standard2.1"
source_details.#: "1"
source_details.0.boot_volume_size_in_gbs: "100"
source_details.0.source_id: "ocid1.image.oc1.phx.xxxxxxxxxxx"
source_details.0.source_type: "image"
state: <computed>
subnet_id: <computed>
time_created: <computed>

+ oci_core_volume.TFBlock1
id: <computed>
availability_domain: "PHX-AD-2"
compartment_id: "ocid1.compartment.oc1..xxxxxxxxxxx"
display_name: "test01phxBV1"
is_hydrated: <computed>
size_in_gbs: "50"
size_in_mbs: <computed>
source_details.#: <computed>
state: <computed>
time_created: <computed>
volume_backup_id: <computed>

+ oci_core_volume_attachment.TFBlock1Attach
id: <computed>
attachment_type: "iscsi"
availability_domain: <computed>
chap_secret: <computed>
chap_username: <computed>
compartment_id: "ocid1.compartment.oc1..xxxxxxxxxxx"
display_name: <computed>
instance_id: "${oci_core_instance.TFInstance.id}"
ipv4: <computed>
iqn: <computed>
is_read_only: <computed>
port: <computed>
state: <computed>
time_created: <computed>
use_chap: <computed>
volume_id: "${oci_core_volume.TFBlock1.id}"


Plan: 3 to add, 1 to change, 0 to destroy.

------------------------------------------------------------------------

Once verified all settings are correct, execute “terraform apply” to provision the VM.
The output will display the progress of the provisioning and it can also be viewed in the OCI console on the Instances page.

References

Terraform: https://www.terraform.io/
Terraform OCI-Provider: https://www.terraform.io/docs/providers/oci/index.html
Terraform/OCI-Provider GitHub: https://github.com/terraform-providers/terraform-provider-oci

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s