0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Terraform] TerraformのバックエンドはS3 Nativeに使用できる

0
Posted at

概要

  • 以前、S3をTerraformのバックエンドとして利用するにはDynamoDBを補助ロックシステムとして利用する必要があった
  • しかし、2024年8月S3の条件付き書き込み機能がGAされた後、TerraformでもS3 Nativeなロック機能をリリースした

Terraform

  • backend.tfvars.sampleはこのような構成になっており、「use_lockfile = true」を指定すると、S3 Nativeなtfstate書き込みのロックができる
bucket       = ""
key          = "provisioning/terraform.tfstate"
region       = "ap-northeast-2"
encrypt      = true
use_lockfile = true
  • backend.tfvars.sampleをbackend.tfvarsにコピーして、実際にS3バケットを作成したバケットネームを入力すれば良い
cp backend.tfvars.sample backend.tfvars
  • main.tfではこのように空っぽのterraform backendを指定すれば良い
terraform {
  backend "s3" {}
}
  • TerraformバックエンドをS3にマイグレーションできた
➜  terraform git:(main) ✗ terraform init -backend-config=backend.tfvars -migrate-state
Initializing the backend...
Backend configuration changed!

Terraform has detected that the configuration specified for the backend
has changed. Terraform will now check for existing state in the backends.


Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing provider plugins...
- Reusing previous version of hashicorp/http from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Reusing previous version of hashicorp/local from the dependency lock file
- Using previously-installed hashicorp/http v3.5.0
- Using previously-installed hashicorp/aws v6.43.0
- Using previously-installed hashicorp/local v2.8.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

S3

  • S3にtfstateがマイグレーションされていることを確認できた

スクリーンショット 2026-05-13 午前9.07.51.png

Hashcorp Terraform

  • 2026.05.13現在、DynamoDBはDeprecatedされている
Enabling DynamoDB State Locking (Deprecated)
To enable DynamoDB state locking, use the following optional arguments:

dynamodb_endpoint - (Optional, Deprecated) Custom endpoint URL for the AWS DynamoDB API. Use endpoints.dynamodb instead.
dynamodb_table - (Optional, Deprecated) Name of the DynamoDB Table to use for state locking and consistency. The table must have a partition key named LockID with a type of String.
  • tfstateをロックするための設定、基本はfalseになっている
Enabling S3 State Locking
To enable S3 state locking, use the following optional argument:

use_lockfile - (Optional) Whether to use a lockfile for locking the state file. Defaults to false.

結論

  • DynamoDBをTerraformロックとして使用していた場合、「use_lockfile」を用いてS3 Nativeなロック機能を使用してみよう
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?