Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Terraform] – Terraform Beginner – Lesson 4: Remote State

Posted on May 3, 2022May 3, 2022 By nim No Comments on [Terraform] – Terraform Beginner – Lesson 4: Remote State

Contents

Toggle
  • 1) What are Remote State and State Locking.
  • 2) Remote Backends with S3
    • 2.1) Prepare.
      • 2.1.1) S3 – Bucket
      • 2.1.2) DynamoDB
    • 2.2) Practice.
      • 2.2.1) Appear A few mistakes
    • 2.3) Running terraform apply and checking
  • 3) Terraform State Commands
    • 3.1) List command
  • 3.2) Show command
  • 3.3) Move Command.
  • 3.4) Pull command.
  • 3.5) Remove command

1) What are Remote State and State Locking.

Như ở các bài trườc mình đã mô tả, khi bạn chạy terraform thì nó cũng sinh ra file terraform.tfstate.
và terraform.tfstate sẽ có các mapping với hệ thông thật trên cloud.

Bạn nhìn tự trên xuống dưới thấy các bước tương tác của terraform với hệ thông cloud.
Chúng ta sẽ có các suggest như sau:
các file .tf thì sẽ lưu trên version control system như là github, gitlab, …
terraform.tfstate thì lưu trên s3, cloud storage – GCP, terraform cloud, …
Mỗi lần ai thực hiện sẽ pull code về và khi đã thực hiện xong thực hiện push lên github.
với cách trên thì bạn sẽ lưu cả terraform.tfstate lên github
File. terraform.tfstate có cả password, ip address, …
và nếu chỉ sử dụng github thì cũng chưa tôi ưu.
Chúng ta có case như sau.
Nếu bạn run 2 lệnh terraform apply giống nhau –> cùng acction lên s3 và trên cùng 1 local
thì 1 terminal đang running thì terminal kia sẽ bị lock.
Chúng ta thiết kết thêm remote state backend.
và Backend này phải đảm bảo các tiêu trí trên.

2) Remote Backends with S3

https://www.terraform.io/language/settings/backends/s3

Bạn có thể tham khảo video trên

2.1) Prepare.

2.1.1) S3 – Bucket

Bạn có thể tạo thủ công bằng tay.

xong rồi create.

Hoặc create s3 bằng terraform:
https://nimtechnology.com/2022/05/02/terraform-terraform-with-aws/#8_S3_with_Terraform

resource "aws_s3_bucket" "finance" {
  bucket = "nimtechnology-terrform-state-bucket01"

  tags = {
    Description = "Remote state for terraform"
  }
}

2.1.2) DynamoDB

https://www.terraform.io/language/settings/backends/s3#dynamodb-state-locking

Bạn sẽ cần tạo Dynamotb với table có primary key là LockID với style string.

Cách bạn tạo bằng tay trên web console.

Cách khác là bạn dùng terraform.
Bạn có thể tham khảo DynamoDB with Terraform

resource "aws_dynamodb_table" "remote-state" {
    name = "state-locking"
    billing_mode = "PAY_PER_REQUEST"
    hash_key       = "LockID"
    attribute {
        name = "LockID"
        type = "S"
    }
}

2.2) Practice.

Đầu tiên chúng ta sẽ có file main.tf

resource local_file sample_res {
  content = "Nimtechnology Love Terraform"
  filename = "sample.txt"
}

Bạn thực hiện chạy terraform init và terraform apply.
Xong bạn thực hiện tạo file là terraform.tf

terraform {
  backend "s3" {
    bucket = "nimtechnology-terrform-state-bucket01"
    key    = "terraform.tfstate"
    region = "us-west-2"
    dynamodb_table = "state-locking"
  }
}

Tiếp đó là bạn run terraform init để terraform cập nhât backend “s3”

Tiếp điên là gõ terraform apply.

Nếu bạn lỗi thì coi mục bên dưới

2.2.1) Appear A few mistakes

Error: error configuring S3 Backend: no valid credential sources for S3 Backend found.

│ Please see https://www.terraform.io/docs/language/settings/backends/s3.html 
│ for more information about providing credentials. 
│
│ Error: NoCredentialProviders: no valid providers in chain. Deprecated. 
│ For verbose messaging see aws.Config.CredentialsChainVerboseErrors

Nếu bị lỗi trên thì bạn nên kiểm tra local đã có aws cli chưa?
đã chạy câu lệnh configure chưa? như anh bên dưới

https://jhooq.com/terraform-error-config-s3/

2.3) Running terraform apply and checking

Chạy lại terraform init.

Giờ kiểm tra S3

file terraform.tfstate đã được upload lên s3

Kiểm tra trên dynamoDB.

Nếu giờ mình xoá file terraform.tfstate

mình luôn xoá file terraform.tfstate.backup

ta thấy terraform tạo file khác terraform.tfstate.backup.
Và có content như file cũ.

Bạn sẽ thấy điều thú vị bạn sẽ vẫn run terraform apply. và file sẽ được cập nhật lên s3, dynamodb.
Không sử dụng terraform.tfstate dưới local.

3) Terraform State Commands

Lúc này bạn sẽ kiểm tra states dưới local nữa mà phải sử dụng các state command

3.1) List command

3.2) Show command

3.3) Move Command.

Trước đó chúng ta tạo ta 1 dynamoDB với name là “state-locking”

Sau đó chúng ta sử dụng comand state move

Sau đó bạn thay name của dynamodb trong file main.tf

Khi bạn run terraform apply thì sẽ thấy không có thay đổi nào.

3.4) Pull command.

3.5) Remove command

AWS - Amazon Web Service

Post navigation

Previous Post: [GCP] How to delete payments, the visa card, the master cards come out of the Google Cloud flatform so easily
Next Post: [Terraform] – Terraform Beginner – Lesson 5: Terraform Provisioners and creating EC2

More Related Articles

[Redis] ElastiCache-Redis Cross-Region Replication|Global DataStore AWS - Amazon Web Service
[EKS] Upgrade eks cluster by eksctl AWS - Amazon Web Service
[Terraform] Solve “Redundant ignore_changes element” on EKSmodule AWS - Amazon Web Service
[AWS/EKS] Unlocking Simplicity and Security of Amazon EKS Pod Identity. AWS - Amazon Web Service
[AWS] Solutions Architect Professional: Lesson 2 – Security AWS - Amazon Web Service
[Terraform] – Terraform Beginner – Lesson 5: Terraform Provisioners and creating EC2 AWS - Amazon Web Service

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [AWS/EKS] Cache Docker image to accelerate EKS container deployment. July 10, 2025
  • [Laravel] Laravel Helpful June 26, 2025
  • [VScode] Hướng dẫn điều chỉnh font cho terminal June 20, 2025
  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.