Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Terraform Backend Tutorial

Terraform is a popular open-source infrastructure as code tool used to create and manage infrastructure resources. The state of the infrastructure resources managed by Terraform is stored in a backend, which can be a remote storage location such as Amazon S3.

To configure Terraform to use an S3 backend, you will need to perform the following steps:

  1. Create an S3 bucket – create a new S3 bucket or use an existing one for storing the Terraform state file.
  2. Create an S3 access key – generate an access key and secret key with the appropriate permissions for accessing the S3 bucket.
  3. Configure Terraform backend – add the following code block to your Terraform configuration file, replacing the values with your specific S3 bucket and access key information:

List of Terraform Backends Options

Terraform uses backends to determine how the state is loaded and how an operation such as apply is executed. The backend configuration can control where the state is stored and how the operations are executed.

Here’s a list of commonly used Terraform backends:

  1. Local:
    • Stores state on the local filesystem.
    • Executes operations locally.
  2. Remote:
    • Uses the Terraform Cloud to store state and optionally execute operations.
    • It’s the default backend for Terraform Cloud workspaces.
  3. Consul:
    • Stores the state in Consul’s key/value store.
    • Executes operations locally.
  4. AzureRM:
    • Stores the state in an Azure Storage Blob.
    • Executes operations locally.
  5. S3:
    • Stores state in an Amazon S3 bucket.
    • It can use DynamoDB for state locking and consistency.
    • Executes operations locally.
  6. Google Cloud Storage (GCS):
    • Stores state in a Google Cloud Storage bucket.
    • Executes operations locally.
  7. Swift:
    • Stores state in OpenStack Swift.
    • Executes operations locally.
  8. Artifactory:
    • Stores state in JFrog Artifactory.
    • Executes operations locally.
  9. Etcd V2 & V3:
    • Store the state in Etcd’s key/value store.
    • Executes operations locally.
  10. Manta:
  • Stores the state in Joyent’s Manta service.
  • Executes operations locally.
  1. OSS:
  • Stores state in Alibaba Cloud Object Storage Service.
  • Executes operations locally.
  1. Pg:
  • Stores state within a configured PostgreSQL database.
  • Executes operations locally.
  1. Kubernetes:
  • Stores state within a Kubernetes Secret.
  • Executes operations locally.

terraform {
  backend "s3" {
    bucket = "your-bucket-name"
    key    = "path/to/terraform.tfstate"
    region = "your-aws-region"
    access_key = "your-access-key"
    secret_key = "your-secret-key"
  }
}

Initialize the backend – run the terraform init command to initialize the backend configuration and create the state file in the S3 bucket.


terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  backend "s3" {
    bucket = "art54654654546546456"
    key    = "aug"
    region = "ap-south-1"
	access_key = ""
    secret_key = ""
  }
}


# Configure the AWS Provider
provider "aws" {
  region = "ap-south-1"
  access_key = ""
  secret_key = ""
}

Terraform Backend Credentail Pass Through Command line

$ terraform init -backend-config=”access_key=ASSSSSSSSSSSSSY” -backend-config=”secret_key=VddddddddddddddG” -reconfigure

How to Setup Terraform Backend with Azure Blob Container?


terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.75.0"
    }
	aws = {
      source = "hashicorp/aws"
      version = "5.20.0"
    }
	github = {
      source = "integrations/github"
      version = "5.39.0"
    }
  }
  backend "azurerm" {
    resource_group_name  = "eksuw01-rg"
    storage_account_name = "eksuw01sa"
    container_name       = "rajesh"
    key                  = "prod.terraform.tfstate"
	subscription_id = "b3dbe884-c3dc"
    client_id       = "6046fe74-dfd2-4"
    client_secret   = "3XJ8Q~uvlwVL"
    tenant_id       = "f34c8fc4-16b"
  }
}
provider "azurerm" {
  features {}
  subscription_id = "b3dbe884c"
  client_id       = "6046fe74-d"
  client_secret   = "3XJ8Q~uvlwV"
  tenant_id       = "f34c8fc4-1"
}

Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x