58_Terraform State Management: Understanding State, Lock Files, and Versioning (With OCI Support) - Nirvan-Pandey/OCI_DOC GitHub Wiki
58_1: Introduction
Terraform is an Infrastructure as Code (IaC) tool that allows users to define and provision infrastructure using a declarative configuration language. Terraform maintains state files and lock files to track infrastructure and manage provider versions effectively. This document considers Oracle Cloud Infrastructure (OCI) in its explanations.
58_2: Terraform State File (terraform.tfstate)
Purpose
The Terraform state file records the current state of managed infrastructure, mapping configurations to real-world resources. This helps Terraform understand what exists, how it is configured, and how changes should be applied.
Location
By default, the state file is stored locally in the root directory of the Terraform configuration as terraform.tfstate. However, for better collaboration and security, it can be stored remotely using backends like:
-
AWS S3
-
Azure Blob Storage
-
HashiCorp Consul
-
Oracle Cloud Infrastructure (OCI) Object Storage
Structure
The state file is a JSON file that contains:
-
Resource details (ID, attributes, metadata)
-
Configuration mappings
-
Dependencies between resources
Example:
{
"version": 4,
"terraform_version": "1.0.0",
"resources": [
{
"mode": "managed",
"type": "oci_core_instance",
"name": "example",
"provider": "provider[\"registry.terraform.io/oracle/oci\"]",
"instances": [
{
"schema_version": 1,
"attributes": {
"id": "ocid1.instance.oc1.iad.abcdefghijk",
"shape": "VM.Standard2.1",
"image_id": "ocid1.image.oc1.iad.123456789abcdef"
}
}
]
}
]
}
Usage
Terraform uses the state file to:
-
Plan and apply infrastructure changes
-
Track existing resources
-
Identify dependencies
58_3: Terraform Lock File (.terraform.lock.hcl)
Purpose
The Terraform lock file ensures consistent provider versions across different runs, preventing unexpected changes due to provider updates.
Location
Stored in the root of the Terraform configuration directory as .terraform.lock.hcl.
Structure
The lock file is written in HashiCorp Configuration Language (HCL) and contains:
-
Provider names
-
Version constraints
-
Checksums for verification
Example:
provider "registry.terraform.io/oracle/oci" {
version = "4.40.0"
constraints = ">= 4.0.0, < 5.0.0"
hashes = [
"h1:abc123...",
"h1:def456..."
]
}
Usage
-
Generated or updated when running terraform init
-
Ensures consistent provider versions in future runs
58_4: Terraform Version
Purpose
Terraform versions determine available features, syntax, and behaviors. Using a consistent version helps avoid compatibility issues.
Checking Version
Run the following command to check the installed Terraform version:
$ terraform version
Terraform v1.0.0
on darwin_amd64
+ provider registry.terraform.io/oracle/oci v4.40.0
Specifying Version Constraints
To enforce version consistency, define constraints in the terraform block:
terraform {
required_version = ">= 1.0.0, < 2.0.0"
}