Terraform state management always assumes Remote State once you operate as a team. Local state hits limits in both conflict handling and confidentiality, so backend choice and operational design determine quality and speed.
Aligned with the stable specifications in the official documentation, this article compares representative backends like S3 + DynamoDB, AzureRM, and Terraform Cloud, and summarizes reliable locking and encryption, least-privilege access control, how to split Workspaces, and pitfalls when referencing remote_state.
Terraform uses the state file as the basis for plan and apply decisions. State contains resource IDs, attributes, dependencies, and often sensitive values. Local storage carries a high risk of edit conflicts and leakage, so teams consolidate state in trusted remote storage and add locking and auditability.
There are 3 key points: 1) Conflict prevention — control concurrent runs with locks; 2) Security — encryption at rest and least-privilege IAM/RBAC; 3) Observability — audit logs, versioning, and recovery options. Terraform itself assumes state is held in plaintext, so encryption and access control are the backend's responsibility.
Remote State and locking flow during collaboration (example: S3 + DynamoDB)
Minimal S3 backend example (locking via DynamoDB)
terraform {
backend "s3" {
bucket = "my-tfstate-bucket"
key = "env/prod/app/terraform.tfstate"
region = "ap-northeast-1"
dynamodb_table = "tf-state-lock"
encrypt = true
}
}
# 実運用ではSSE-KMS、バージョニング、バケットポリシーでの制限を合わせて設定するSelection criteria include locking reliability, ease of encryption and access control, audit and versioning, and operational cost. Cloud-native S3 / AzureRM are easy to introduce, while Terraform Cloud's remote backend bundles RBAC, UI, and team features together.
GCS has constraints on built-in locking (this assumption depends on official support status), so large teams that require strong consistency and locking should prefer S3 + DynamoDB, AzureRM, or Terraform Cloud.
| Backend | Locking | Encryption / Protection | Access Control / Audit |
|---|---|---|---|
| S3 + DynamoDB | Yes (DynamoDB lock) | SSE-S3 / SSE-KMS, versioning supported | IAM + CloudTrail, Bucket Policy |
| AzureRM (Blob) | Yes (Blob lease) | SSE / CMK, Soft Delete / Versioning | AAD RBAC + diagnostic logs |
| Terraform Cloud (remote) | Yes (server-side lock) | Server-side encryption, VCS integration | Org RBAC, audit, policy |
| GCS (Bucket) | Constrained | Server-side encryption / CMEK | IAM + Cloud Audit Logs |
Partial backend configuration and injection at init time (-backend-config)
terraform {
backend "s3" {}
}
# repoには値を固定せず、init時に注入して誤適用を防ぐ
# 例)環境ごとのCI/CDジョブで
# terraform init \
# -backend-config=bucket=my-tfstate-bucket \
# -backend-config=key=env/${ENV}/app/terraform.tfstate \
# -backend-config=region=ap-northeast-1 \
# -backend-config=dynamodb_table=tf-state-lock \
# -reconfigureRemote State security depends on the storage destination. Using S3 as an example, lock down the 4 areas of encryption, least privilege, audit, and recovery. Terraform itself does not encrypt state — backend encryption and access control are the keys.
To avoid state bloat, suppress storing unnecessary data sources and oversized resource attributes, and rigorously avoid leaving secrets in outputs.
Representative S3 bucket settings (partial reference)
# バケットは事前に作成しておき、ポリシーでTerraformのロールのみ許可
# 例のポリシーフラグメント(概念例)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::my-tfstate-bucket",
"arn:aws:s3:::my-tfstate-bucket/*"
],
"Condition": {"Bool": {"aws:SecureTransport": "false"}}
}
]
}
# KMSキーはTerraformロールにEncrypt/Decryptを付与し、外部主体を禁止
# バージョニングは有効化、パブリックアクセスは全面ブロックYou can separate environments by either splitting directories or using Workspaces. From an Associate perspective, the priority is avoiding state conflicts and separating permissions, with clear naming and consistent access boundaries. Workspaces are suited to switching environments while keeping the same configuration.
Embedding the Workspace name in the backend key is safer. With Terraform Cloud's remote backend, RBAC, run queues, and locks are managed per organization and workspace.
Switching state keys by Workspace name (S3)
terraform {
backend "s3" {
bucket = "my-tfstate-bucket"
key = "env/${terraform.workspace}/app/terraform.tfstate"
region = "ap-northeast-1"
dynamodb_table = "tf-state-lock"
encrypt = true
}
}
# 例
# terraform workspace new stage
# terraform workspace select stage
# terraform apply # => env/stage/app/terraform.tfstate に書き込みWhen passing values between modules, data "terraform_remote_state" lets you safely reference another stack's outputs. The cardinal rule, though, is to keep secrets out of outputs. Because coupling between states increases, make key naming and permission boundaries explicit.
Use least-privilege backend credentials and isolate read-only roles (e.g., GetObject only). When referencing between Terraform Cloud remote workspaces, use the appropriate intra-org workspace reference mechanism and RBAC.
Reference a VPC ID from another stack (S3 backend example)
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = "my-tfstate-bucket"
key = "env/prod/network/terraform.tfstate"
region = "ap-northeast-1"
dynamodb_table = "tf-state-lock"
encrypt = true
}
}
output "subnet_ids" {
value = data.terraform_remote_state.network.outputs.subnet_ids
}
# 注意: network側のoutputsに機密を置かない。参照用IAMはGetObject等の読み取り最小権限のみOperational issues — leftover locks, changing backends, recovering state — are inevitable. Define procedures and reserve force-unlock as a last resort. For migrations, it is essential to verify within the planned downtime and secure a rollback option (backup).
Combine plan diffs and audit logs for drift detection. State bloat and overly complex key structures translate directly into higher operational costs, so periodic health checks are recommended.
Representative operational commands
# ロックの強制解除(適用中断が無いことを確認した上で)
terraform force-unlock <LOCK_ID>
# stateのバックアップ
terraform state pull > backup-$(date +%F).tfstate
# バックエンド移行(例:S3 -> Terraform Cloud remote)
terraform init -migrate-state -reconfigure
# state内リソースのパス修正
terraform state mv aws_s3_bucket.old aws_s3_bucket.newAssociate
問題 1
You manage AWS infrastructure as a team. Which Remote State configuration most simply satisfies both conflict prevention during collaboration and secure storage?
正解: A
A satisfies reliable locking against concurrent team runs (DynamoDB), encryption at rest (SSE-KMS), and easy recovery (versioning). B is unsuitable for both conflicts and confidentiality. C and D lack locking and/or encryption.
Does Terraform encrypt state automatically?
No. Terraform itself does not encrypt state. Encryption is the backend's responsibility (S3 SSE-KMS, Azure CMK, Terraform Cloud server-side encryption, etc.).
Should I use Workspaces or split into separate directories?
Workspaces work well when you only need to switch environments on the same configuration. When you need strong separation of permissions and lifecycles, splitting into directories (or repositories) and physically separating state is easier to operate.
A lock is stuck and I cannot apply. What should I do?
First confirm no Terraform process is currently running, then inspect the backend (e.g., the DynamoDB lock record). If everything looks clean, use terraform force-unlock as a last resort. Because forced unlocking carries a risk of conflicts, coordinate with your team before doing it.
Practice with certification-focused question sets
無料で問題を解いてみるNicheeLab Editorial Team
NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.
HCL Syntax: Terraform's Configuration Language (2026)
HCL2 fundamentals for Terraform — blocks, attributes, expres...
Terraform Authoring & Operations Pro: Complete Guide (2026)
Tactics for the Terraform Pro exam — module authoring, works...
Terraform Providers: Plugin Management Fundamentals (2026)
Provider mechanics — required_providers, versions, mirrors, ...
Terraform Resource Blocks: Declarative Infra Units (2026)
Resource block fundamentals — addresses, references, common ...
Terraform Data Sources: Read-Only External Data (2026)
Data source basics — declaration, refresh behavior, dependen...