Lesson 2 of 6 · Infrastructure as Code with Terraform

The Terraform Language

HCL blocks, providers, resources, data sources, variables, and outputs, with a complete Autonomous Database example.

Beginner 4 sections 4 min read

HCL in a nutshell

Terraform code is written in HCL, a language designed to be easy for people to read. It's made of blocks: providers say which platform to use, resources describe what should exist, variables make the code reusable, and outputs share results. Learn these four and you can read most Terraform code.

A block has a type, one or two labels, and arguments inside braces:

# type       resource type                         local name
resource "oci_database_autonomous_database" "sales" {
  db_name       = "SALESATP"    # argument = value
  compute_count = 2
}

Strings go in double quotes, numbers and true/false don't, lists use [ ], and comments start with #. Terraform reads every .tf file in a folder together, so you can split code across files however you like.

The main building blocks

TERRAFORM AND PROVIDER which platform, which versions, which region VARIABLESDATA SOURCES inputs: name, size,environmentlook up what exists:compartment, subnet Resources what should exist: databases, servers OUTPUTS results to share: IDs,connection strings
terraform and provider
Which providers the code needs, from where, and which versions, plus provider settings such as the region.
resource
Something Terraform creates and manages, such as a database, a server, or a network. Each resource type comes from a provider: oci_database_autonomous_database, aws_db_instance, azurerm_mssql_database.
data
A data source looks up something that already exists, managed elsewhere, such as an existing network, so you can refer to it without managing it.
variable
An input, so the same code can build different databases: the name, size, or environment.
output
A value Terraform prints after applying, and shares with other code or tools, such as a database's connection details.
locals
Named values calculated inside the code, such as a standard set of tags, to avoid repeating yourself.
module
A reusable package of Terraform code, covered in Environments and Modules.

A complete example

Here's a small project that creates an Autonomous Database on OCI for Kofi's sales application. Most projects split code into a few standard files:

sales-db/
├── versions.tf      # Terraform and provider versions
├── variables.tf     # inputs
├── main.tf          # resources
├── outputs.tf       # results
└── dev.tfvars       # values for the development environment
# versions.tf: pin versions, so everyone gets the same behavior
terraform {
  required_version = ">= 1.6"
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = ">= 6.0"
    }
  }
}

provider "oci" {
  region = var.region
}
# variables.tf: the inputs, with types, descriptions, and defaults
variable "region"         { type = string }
variable "compartment_id" { type = string }
variable "environment"    { type = string }

variable "ecpu_count" {
  type        = number
  description = "Base ECPUs for the database"
  default     = 2
}

variable "admin_password" {
  type      = string
  sensitive = true    # hidden in plan and apply output
}
# main.tf: the database itself
resource "oci_database_autonomous_database" "sales" {
  compartment_id           = var.compartment_id
  db_name                  = "SALES${upper(var.environment)}"
  display_name             = "sales-${var.environment}"
  db_workload              = "OLTP"
  compute_model            = "ECPU"
  compute_count            = var.ecpu_count
  data_storage_size_in_tbs = 1
  is_auto_scaling_enabled  = true
  admin_password           = var.admin_password   # from a vault, never hard-coded

  freeform_tags = {
    app         = "sales"
    environment = var.environment
    owner       = "dba-team"
  }
}
# outputs.tf: what to share once it's built
output "database_id" {
  value = oci_database_autonomous_database.sales.id
}
# dev.tfvars: the values for one environment
region         = "us-ashburn-1"
compartment_id = "ocid1.compartment.oc1..aaaa"
environment    = "dev"
ecpu_count     = 2

A few things to notice:

  • References connect blocks: var.ecpu_count reads a variable, and oci_database_autonomous_database.sales.id reads an attribute of a resource. Terraform uses these references to work out the order to create things in.
  • Interpolation, such as "sales-${var.environment}", builds strings from values, and functions such as upper() transform them.
  • Tags are set in code, so every database is labeled for cost reports and ownership, every time.
  • The password isn't in the code. The Terraform for Databases lesson covers how to handle secrets.

Key takeaways

  • HCL is made of blocks with labels and argument = value pairs; every .tf file in a folder is read together.
  • Providers say which platform to use, resources describe what should exist, data sources look up what already exists, variables are inputs, and outputs share results.
  • A typical project splits code into versions.tf, variables.tf, main.tf, outputs.tf, and a .tfvars file per environment.
  • References such as var.name and resource.name.id connect blocks and decide the order Terraform builds things in.
  • Pin provider versions, tag everything, and keep secrets out of the code.