4
1

More than 1 year has passed since last update.

【AWS/Terraform】tfstateをS3で安全に管理する

Posted at

はじめに

この記事では、Terraformの状態ファイル(tfstate)をAWS S3バケットで管理する方法を記載します。
S3をバックエンドとして使用することで、チームでの共有やリモートでの管理が容易になり、またtfstateの状態が失われるリスクを減らすことができます。

S3バケットの作成

まず、tfstateを保存するためのS3バケットを作成します。

Terraformのバックエンド設定

main.tf
terraform {
  required_version = ">= 0.13"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0.0"
    }
  }
  backend "s3" {
    bucket  = "バケット名"
    key     = "terraform.tfstate"
    region  = "ap-northeast-1"
    profile = "terraform"
  }
}

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

bucketは先ほど作成したS3バケットの名前、keyはtfstateファイルを保存するS3内のパス、regionはS3バケットを作成したリージョンを指定します。

Terraformの初期化

バックエンド設定を追加したら、Terraformを再初期化します。

terraform init

このコマンドを実行すると、Terraformは設定を読み込み、指定されたバックエンドにtfstateを保存します。

4
1
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
4
1