Open In App

What Is Terraform Lock FIle?

The terraform lock file is named “Terraform. lock.hcl” It will generated by the terraform itself and it will make sure that the same infrastructure will be created if multiple users are working. It serves as a central repository for the particular provider and module versions that you have used in your configuration.

What is Terraform Statefile?

Terraform is the main component of the terraform it maintains the current state of infrastructure which is built using terraform. The language used in the terraform state file was JSON-formatted. The Terraform state file maintains the following details



When we write a new terraform config file and apply it then terraform will generate this file. If there is any previous state file it will update that state file to the current state by updating it. The Terraform state file maintains the updated state of the infrastructure in the cloud.

When you execute the terraform application then the state file will be created by the AWS automatically to maintain the state of the Cloud infrastructure.



What is Terraform Configfile?

Terraform uses configuration files built into the infrastructure in the cloud. The language used to write the terraform configuration file was HashiCrop Configuration Language (HCL). While building the cloud infrastructure terraform will compare the both config file and the state file by which Terraform will build the infrastructure which is not available in the state file.

The file format of the terraform config file was

<name of the file>.tf

It will be like the name of the file and the extension of the file was “.tf”. It is JSON-formatted.terraform

Difference Between Terraform Statefile Vs Terraform Lockfile

Terraform Statefile

Terraform Lockfile

Terraform statefile maintains the current state of the cloud infrastructure which is provisioned using the terraform.

The Terraform lock file is mainly used to lock the versions of the provides and the Terraform if someone is using it on another machine they will not get the version conflicts.

The Terraform state file is formatted in the JSON format which contains all the details related to the resources configured in the cloud.

The Terraform state file is formatted in the JSON format which contains all the details related to the resources configured in the cloud. Records the versions of the providers and terraform which are used to configure the cloud infrastructure.

Terraform statefile format was as follows (.terraform.tfstate)”.

Terraform lockfile format was as follows “(.terraform.lock.hcl)”.

Steps To Create Terraform Lockerfile

Step 1: Create a sample config by using the code below.

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}

provider "aws" {
region = "us-east-1"
access_key = "AKIAWLIWW6OXIMMLZ7LW"
secret_key = "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"

}

resource "aws_instance" "Demo_1" {

ami = "ami-0c7217cdde317cfec"
instance_type = "t2.micro"

By using the above code we are creating the basic ec2 instance and before intilizing the terraform init the terraform directory structure will look as shown the image below.

Terraform Directory Structure befpre intializing the terraform init.

Step 2: Know intilize the terraform by the command “terraform init”. After initializing the terraform init command you can see that the terraform lock file was created. Which will consist of the version of the provider and the terraform.

Step 3: Use the vi or cat command to see the details available in the terraform or if you are using the visual studio code then you can just click on the file to see the content avlible init as shown in the image below.

“Terraform.lock.hcl” in the above you can see provider version “5.38.0” lock file will make sure that any one working will use the same version of the provider.

Use Cases Of Terraform Lockfile

Following are the dome of the use cases of terraform lockfile.

People Also Read

Conclusion

In this article, we have seen how the terraform lock will be created the why it is used when it will be created how to be used into in an efficient way.

Terraform Lockerfile – FAQ’s

How do I lock a state file in Terraform?

Use the “tflock” command in the same directory as the terraform init command to lock your Terraform state.

Can you delete the Terraform lock file?

Terraform init will remove any lock file entry for a provider if you remove the last reliance on that provider from your state and configuration.

Should Terraform lock the HCL file be committed?

Terraform. lock. hcl and should preferably be committed to your version control system repository so that the next time you run terraform init, the same provider versions will be utilised.


Article Tags :