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.


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, …


File. terraform.tfstate có cả password, ip address, …
và nếu chỉ sử dụng github thì cũng chưa tôi ưu.

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



2) Remote Backends with S3

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



2.1) Prepare.
2.1.1) S3 – Bucket
Bạn có thể tạo thủ công bằng tay.


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

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


Giờ kiểm tra 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

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

3.1) List command

3.2) Show command

3.3) Move Command.

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

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

3.4) Pull command.

3.5) Remove command
