Terraform state file on s3 backend

Terraform state file on s3 backend

Terraform state file -

  1. When you are building an infrastructure with terraform config, a state file gets generated locally in the directory called "terraform.tfstate" .
  2. The state file contains information of the infrastructure and configuration which are created.
  3. It also helps to modify or manage the infrastructure.
  4. We can also say that , it acts as a memory for the infrastructure created , so that we can make the changes afterwards.

When working on a team, it is better to store this state file remotely so that more folks can access it to make changes to the infrastructure.

When working with Terraform in a team, use of a local file makes Terraform usage complicated because each user must make sure they always have the latest state data before running Terraform and make sure that nobody else runs Terraform at the same time.

With remote state, Terraform writes the state data to a remote data store, which can then be shared between all members of a team. Terraform supports storing state in Terraform Cloud, HashiCorp Consul, Amazon S3, Azure Blob Storage, Google Cloud Storage, Alibaba Cloud OSS, and more.

Prerequisite-

  1. s3 storage
  2. DynamoDB Table

To setup the terraform state file in s3 backend , first we need to create the s3 bucket and then s3_backend.

Lets starts form creating s3 bucket through terraform -

No alt text provided for this image

Locking state file -

DynamoDB table provide the mechanism of "lock" for our state file.

Why do we need Locking.....??

No alt text provided for this image

As we store our state file in remote location (s3 Bucket) so that many people can access it , which create a risk for multiple people attempting to make change to same file which can cause damage or data loss, this condition known as Race Condition.

That is why we need to lock our state file.

So now create a DynamoDB table for locking the state file -

No alt text provided for this image

After creating both the resources (i.e. s3 bucket and DynamoDB table) , we have to modify our terraform s3 backend while adding bucket_name and table_name .

No alt text provided for this image

Note:- Before running the s3 backend script , we have to first create the two resources (i.e. s3 bucket and DynamoDB table)

Now we can initialize the script -

terraform init
No alt text provided for this image
terraform validate
No alt text provided for this image
terraform plan
No alt text provided for this image
No alt text provided for this image
terraform apply
No alt text provided for this image

At last we can initialize our s3 backend script -

terraform init
No alt text provided for this image

So lets first checkout our s3 bucket -

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Here you can see our state file is working fine in s3 and if you check your terraform.tfstate file locally, you will find that it is now become empty.

Now lets check our DynamoDB table -

No alt text provided for this image

SETUP COMPLETE !!

For the code , refer to my GitHub repo

Thank you for reading......

Md. Mehedi Hasan Rumi

Lecturer | DevOps Engineer

3 年

Thanks for writing such a great article

Aditya Kishore

Solutions Consultant | Pre-Sales

4 年

Great article Sanjay!

要查看或添加评论,请登录

Sanjay De的更多文章

  • AWS VPC Peering [Terraform]

    AWS VPC Peering [Terraform]

    What is VPC Peering ? A VPC peering connection is a networking connection between two VPCs that enables you to route…

    2 条评论
  • Dynamic CIDR & AZ [Terraform]

    Dynamic CIDR & AZ [Terraform]

    Classless Inter-Domain Routing (CIDR) - Classless Inter-Domain Routing (CIDR) blocks are for specifying a range to IP…

    4 条评论
  • EC2 Dynamic Inventory [Ansible]

    EC2 Dynamic Inventory [Ansible]

    External or Dynamic Inventory - These inventories are the python scripts , to run these scripts a user have to provide…

  • VPC with NAT gateway

    VPC with NAT gateway

    NAT gateway- NAT Gateway is a highly available AWS managed service that makes it easy to connect to the Internet from…

    2 条评论
  • EC2-instance with EFS

    EC2-instance with EFS

    First lets discuss about EBS !!!! Amazon Elastic Block Store (EBS) Amazon Elastic Block Store (EBS) is an easy to use…

  • VPC with Internet Gateway

    VPC with Internet Gateway

    Amazon Virtual Private Cloud Amazon VPC lets you provision a logically isolated section of the AWS Cloud where you can…

    2 条评论
  • Amazon EKS

    Amazon EKS

    What is Amazon EKS ? Amazon EKS (Elastic Container Service for Kubernetes) is a managed Kubernetes service that allows…

  • Face_Mask_counter

    Face_Mask_counter

    About this Project - This project we created to achieve some helpful solution from this present pandemic situation , so…

    8 条评论
  • Configure Jenkins [Groovy]

    Configure Jenkins [Groovy]

    Jenkins DSL(Domain Specific Language)- Jenkins DSL job is a job which will create another job(s)/pipeline(s)/etc but…

  • Prometheus & Grafana [Integration]

    Prometheus & Grafana [Integration]

    Prometheus:- It is a free software application used for event monitoring and alerting. It records real-time metrics in…

社区洞察

其他会员也浏览了