logo
LearnKeywords
Learn

Terraform keywords

What******data******** means in Terraform:**

data is one of Terraform's core keywords (called a "block type"). It tells Terraform:

"I want tolook up** existing information, not create anything new"

The main Terraform keywords are:

KeywordPurpose
resourceCreate new infrastructure (servers, databases, etc.)
dataLook up existing information
variableDefine input values
outputExport values for use elsewhere
localsCalculate values used within the configuration
moduleUse reusable code packages

Examples to show the difference:

data******** = Looking up existing stuff:

data "oci_core_images" "existing_image" {
  # Find an image that already exists
}

resource******** = Creating new stuff:

resource "oci_core_instance" "my_new_server" {
  # Create a brand new virtual machine
}

Think of it like:

  • data = "Show me what's already there" ๐Ÿ”

  • resource = "Build me something new" ๐Ÿ”จ

So yes, data is a special Terraform word that you must use exactly as written! ๐Ÿ‘


variable** - Define Input Values**

What it does:

variable lets you pass values INTO your Terraform configuration (like function parameters)

Think of it like:

A form field that someone fills out before running Terraform ๐Ÿ“

Example:

variable "server_name" {
  description = "What should we call the server?"
  type        = string
  default     = "my-server"
}

How it works:

  • Someone runs: terraform apply -var="server_name=web-server"

  • Or you put it in a .tfvars file

  • Use it later: name = var.server_name


output** - Export Values**

What it does:

output shows important information AFTER Terraform creates things

Think of it like:

A receipt that shows you important details after shopping ๐Ÿงพ

Example:

output "server_ip_address" {
  description = "The public IP of our new server"
  value       = oci_core_instance.my_server.public_ip
}

How it works:

  • After terraform apply, you'll see: server_ip_address = "123.456.789.0"

  • Other Terraform configs can use this value

  • Useful for showing connection info, URLs, etc.


locals** - Calculate Values**

What it does:

locals creates calculated values that you use multiple times in the same configuration

Think of it like:

A calculator or notepad for doing math once and reusing the answer ๐Ÿงฎ

Example:

locals {
  common_tags = {
    Environment = "production"
    Team        = "web-team"
    CreatedBy   = "terraform"
  }
  
  server_count = var.users_count * 2
}

How it works:

  • Calculate once: local.server_count

  • Use everywhere: count = local.server_count

  • Keeps your config DRY (Don't Repeat Yourself)


module** - Use Reusable Code**

What it does:

module imports pre-written Terraform code (like using a template or recipe)

Think of it like:

Using a cake mix instead of measuring flour, sugar, etc. from scratch ๐Ÿฐ

Example:

module "web_server" {
  source = "./modules/web-server"
  
  server_name = "my-website"
  server_size = "small"
}

How it works:

  • Points to a folder with Terraform code (./modules/web-server)

  • Or downloads from Terraform Registry, GitHub, etc.

  • Passes variables to the module

  • The module creates multiple resources for you

  • Like calling a function that does lots of work!

Quick Summary:

  • variable = "Tell me what you want" โฌ…๏ธ

  • output = "Here's what I made" โžก๏ธ

  • locals = "Let me calculate this once" ๐Ÿงฎ

  • module = "Use this pre-made template" ๐Ÿ“ฆ