S3에 Terraform 상태 저장
Source: Dev.to
Configuring S3 as a Terraform Backend
Terraform can store its state in an S3 bucket. Below is a minimal configuration that sets up the S3 backend:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.0"
}
}
required_version = ">= 1.0"
backend "s3" {
bucket = "terraform-state-dev"
key = "my-key"
region = "us-east-1"
shared_credentials_file = "~/.aws/credentials"
}
}
- You can use a single key (the
keyargument) to store all of your Terraform state in one file, or - You can allocate one key per Terraform workspace (or per environment) to keep states isolated.
Migrating Existing State to S3
If you already have a local state file or are using a different backend, you can migrate it to the S3 bucket with:
terraform init -migrate-state
Warning: Losing your Terraform state can cause significant trouble, as Terraform relies on that state to track resources. Ensure the S3 bucket is versioned and backed up to prevent accidental loss.