TheKoguryo's Tech Blog

 Version 2024.05.05

Warning

This content has been generated by machine translation. The translations are automated and have not undergone human review or validation.

14.2.2 Writing Terraform Configurations

To write Terraform, you need to express your OCI infrastructure resources in HashiCorp Configuration Language (HCL) format in a Terraform configuration file (.tf). JSON format is also supported, but we will use HCL format here.

The following are some of the most basic contents of Terraform. For complete information, please refer to the Terraform and Terraform OCI Provider documentation.

Let’s test it by creating a VCN through Terraform, just like we first created the VCN in the OCI console. Of course, this is an example of creating only a VCN, unlike making it with a Wizard on the console.

OCI Provider Definition

The types and specifics of the resources expressed in the Terraform configuration file differ depending on the provider. Various companies have defined their resources as Terraform Providers and are providing them. With OCI, the Provider can be set up as follows.

  • provider.tf

    variable "tenancy_ocid" {}
    variable "user_ocid" {}
    variable "fingerprint" {}
    variable "private_key_path" {}
    variable "region" {}
    
    provider "oci" {
      tenancy_ocid     = "${var.tenancy_ocid}"
      user_ocid        = "${var.user_ocid}"
      fingerprint      = "${var.fingerprint}"
      private_key_path = "${var.private_key_path}"
      region           = "${var.region}"
    }
    

Variable Definition

As you can see in the Provider definition, Variable is used to define the parameters needed for the Terraform module. You can define it like variable "tenancy_ocid" {} and use it wherever you need it like ${var.tenancy_ocid} . The declaration is declared in the form of variable "variable name" {}, and additional settings can be made as in the example below.

  • type (optional): Defines the type of variable

    • One of string, list, or map
    • If not indicated, it is based on the value format used for default
    • If not displayed and there is no default, string is assumed.
  • default (optional): Default value definition, if there is no default value, it must be passed through environment variable setting when calling.

  • description (optional): Enter a description for the variable.

  • example

    variable "tenancy_ocid" {}
    variable "user_ocid" {}
    variable "fingerprint" {}
    variable "private_key_path" {}
    variable "region" {}
    
    variable "AD" {
      default     = "1"
      description = "Availability Domain"
    }
    
    variable "CPUCoreCount" {
      type    = "string"
      default = "2"
    }
    

Set Variable Value

  1. Before executing Terraform, variable values ​​can be specified by assigning them to environment variables in the form of TF_VAR_ variable name as shown below.

    export TF_VAR_tenancy_ocid="<tenancy OCID>"
    
  2. It can also be done with the -var option when running terraform CLI.

  3. You can also create a separate variable file (terraform.tfvars).

    • If the file name is different to change the variable according to the environment, the file name can be specified with the -var-file option when running terraform.

    • example

      ## terraform.tfvars file
      tenancy_ocid="<tenancy OCID>"
      

Resource settings

Resource represents the OCI resource to be created. If you set the resource as shown below and run terraform, the resource is actually created in OCI. A list of OCI resources that support Terraform can be found in the Terraform OCI Provider documentation.

  • example

    variable "compartment_ocid" {}
    
    resource "oci_core_vcn" "example_vcn1" {
      cidr_block     = "10.0.0.0/16"
      dns_label      = "vcn1"
      compartment_id = "${var.compartment_ocid}"
      display_name   = "vcn1"
    }
    

Data Source Settings

The Data source represents the OCI resource to query. Unlike Resource, it is used only for lookup purposes. The OCI Data sources that support Terraform can be found in the Terraform OCI Provider documentation.

  • Example, oci_identity_availability_domains

    # Gets a list of Availability Domains
    data "oci_identity_availability_domains" "ADs" {
      #Required
      compartment_id = "${var.tenancy_ocid}"
    }
    

Set Output

The output variable is outputted as a log when terraform is executed for the purpose of inquiring a specific value through a query among a lot of data, and it can also be viewed with the terraform output variable name command.

  • Example of id of example_vcn1 created

    output "vcn1_ocid" {
      value = ["${oci_core_vcn.example_vcn1.id}"]
    }
    

Terraform Configurations Example

  • provider.tf

    variable "tenancy_ocid" {}
    variable "user_ocid" {}
    variable "fingerprint" {}
    variable "private_key_path" {}
    variable "region" {}
    
    provider "oci" {
      tenancy_ocid     = "${var.tenancy_ocid}"
      user_ocid        = "${var.user_ocid}"
      fingerprint      = "${var.fingerprint}"
      private_key_path = "${var.private_key_path}"
      region           = "${var.region}"
    }
    
  • vcn.tf

    variable "compartment_ocid" {}
    
    resource "oci_core_vcn" "vcn1" {
      cidr_block     = "10.0.0.0/16"
      dns_label      = "vcn1"
      compartment_id = "${var.compartment_ocid}"
      display_name   = "vcn1"
    }
    
    output "vcn1_ocid" {
      value = ["${oci_core_vcn.vcn1.id}"]
    }
    


As an individual, this article was written with my personal time. There may be errors in the content of the article, and the opinions in the article are personal opinions.

Last updated on 29 Mar 2019