Terraform Override

Terraform Override

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed terraform provisioner.

https://cloudaffaire.com/terraform-provisioner/

In this blog post, we will discuss terraform override.

What is Terraform Override?

Terraform loads all configuration files within a directory and appends them together. Terraform also has a concept of overrides, a way to create files that are loaded last and merged into your configuration, rather than appended. Overrides names must be override or end in _override, excluding the extension. Examples of valid override files are override.tf, override.tf.json, temp_override.tf.

Override demo:

#create a directory and get inside it
mkdir terraform && cd terraform

#create main.tf
vi main.tf
---------------
#Create a VPC
provider "aws" {
access_key = "<your_access_key>"
secret_key = "<your_secret_key>"
region = "ap-south-1"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.0.0.0/16"
tags {
Name = "myvpc"
}
}
---------------
:wq

#format code
terraform fmt

#initialized terraform
terraform init

#create the resource
terraform apply

#view state file
terraform show

#create an override file
vi override.tf
---------------
#Overrides the VPC configuration
resource "aws_vpc" "myvpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
tags {
Name = "mynewvpc"
}
}
---------------
:wq

#format code
terraform fmt

#plan the changes
terraform plan

#apply the changes
terraform apply

#view state file
terraform show

#destroy the resources
terraform destroy

#cleanup
cd .. && rm -rf terraform

Hope you have enjoyed this article. In the next blog post, we will create a LAMP stack using terraform.

To get more details on terraform, please refer below terraform documentation

https://www.terraform.io/docs/index.html

 

Leave a Reply

Close Menu