Define input variables
You now have enough Terraform knowledge to create useful configurations, but the examples so far have used hard-coded values. Terraform configurations can include variables to make your configuration more dynamic and flexible.
Prerequisites
This tutorial assumes that you are continuing from the previous tutorial, which we highly recommend. If not, we've summarized the prerequisites here for your convenience. To follow this tutorial you will need:
OCI Tenancy. Note your region, you will use it throughout the tutorial.
The Terraform CLI installed.
The OCI CLI installed. Configure the CLI with a token by following the session authentication flow.
Create a directory named
learn-terraform-oci
and paste the following configuration into a file namedmain.tf
.Customize the
region
and both instances ofcompartment_id
to match the ones listed on your OCI tenancy page in the webInitialize the configuration.
Once you have successfully initialized the configuration, you can continue with the rest of this tutorial.
Declare variables
The configuration includes a number of hard-coded values. Terraform variables allow you to write configuration that is flexible and easier to re-use.
Declare variable to define your compartment ID and region so that you don't have to repeat these values in the configuration.
Create a new file called variables.tf
containing the following configuration blocks.
Tip
Terraform loads all files in the current directory ending in .tf
.
Notice that you defined a default for region, since you may want to suggest a region to other users of this configuration, especially if they collaborate with you on this infrastructure. You did not set a default for compartment ID.
Define the variables
Create a file called terraform.tfvars
containing the following contents. This file will define values for the variables you just declared.
Customize your file with your own OCID and region, which you can copy from one of the places they are listed in main.tf
. Then save the file. Although you have defined a default for the region variable, setting the variable in a .tfvars
file, or with another method, will override the default.
As a best practice, Terraform configuration directories usually contain a version control configuration file that excludes files with .tfvars
extensions from version control. Do not include sensitive values in Terraform configuration files that you could accidentally commit to version control.
Reference the variables
Open main.tf
, and update the configuration to use the new variables.
Apply the configuration
Apply the configuration. Respond to the confirmation prompt with a yes
.
Respond to the confirmation prompt with yes
.
Next Steps
You defined some of the values in your configuration as variables, which made your configuration more flexible and less repetitive. To deploy the same infrastructure in a different compartment or region, the only configuration changes you would need to make are in terraform.tfvars
.
To learn more about Terraform variables, and other methods for defining them, refer to the variables documentation. Continue learning about the Terraform basics on OCI in the next tutorial.