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 で S3 + DynamoDB を使った状態管理のシンプルな手順

Last updated at Posted at 2025-02-09

📌 概要

Terraform の状態を S3 + DynamoDB で安全に管理する方法をまとめます😊

これを使えば、ローカルからでも、 どこか他のサーバーからでも同じ状態を共有して Terraform を実行できるようになります👌🏻

必須ではありませんが、チーム開発や複数環境での運用を考えるなら導入をおすすめします🔝

📌 事前に準備するもの

main.tf の作成がまだの方は以下の記事を参考にファイルを作成してください。

📌 AWSソースの作成

Terraform の backend "s3" を使うために、まずは S3 & DynamoDB を手動で作成しよう。

✅ S3 バケットを作る

aws s3api create-bucket --bucket sandbox-terraform-mory --region ap-northeast-1 --create-bucket-configuration LocationConstraint=ap-northeast-1

--bucket の後ろのバケット名は任意で変更してください

S3 のバージョン管理をONにする(バックアップ用)

aws s3api put-bucket-versioning --bucket sandbox-terraform-mory --versioning-configuration Status=Enabled

--bucket の後ろのバケット名は任意で変更してください

✅ DynamoDB を作る(ロック管理用)

aws dynamodb create-table --table-name sandbox-terraform-mory --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH   --billing-mode PAY_PER_REQUEST

--table-name の後ろのテーブル名は任意で変更してください

📌 コーディング

S3 & DynamoDB を作ったら、Terraform の maiin.tf の冒頭に以下のコードを追加しよう。

main.tfbackend 設定を追加する

terraform {
  backend "s3" {
    bucket         = "バケット名"
    key            = "terraform.tfstate"
    region         = "ap-northeast-1"
    encrypt        = true
    dynamodb_table = "テーブル名"
  }
}

provider "aws" {
  region = "ap-northeast-1"
}

[参考リポジトリ]

📌 コマンドの実行

✅ Terraform を初期化しよう!

terraform init

実行するとこんなログが出力されます。

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v5.87.0...
- Installed hashicorp/aws v5.87.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

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.

このとき、以下のメッセージが出るので yes と入力しよう。

Do you want to copy existing state to the new backend?
Enter "yes" to copy and "no" to start with an empty state.
Enter a value: yes

✅ S3 に .tfstate が保存されたか確認しよう

aws s3 ls s3://sandbox-terraform-mory

成功するとこんな感じで表示されるよ!

2024-02-09 12:00:00   5.4K terraform.tfstate
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?