# Terraform tree 구조
.
├── _variables_
│ ├── common-dev.yaml
│ ├── common-prod.yaml
│ ├── common-staging.yaml
│ └── common-test.yaml
├── backend
│ ├── backend-dev.conf
│ ├── backend-prod.conf
│ ├── backend-staging.conf
│ └── backend-test.conf
├── dynamodb
│ ├── dynamodb.tf
│ └── variables.tf
├── main.tf
├── output.tf
├── provider.tf
├── s3
│ ├── s3.tf
│ └── variables.tf
├── variables.tf
└── vpc
├── igw.tf
├── nat.tf
├── route.tf
├── subnet.tf
├── varialbes.tf
└── vpc.tf
기본적인 구조는 위와 같으며 backend 디렉토리의 conf 파일을 통해 Terraform 의 Backend 동적 처리를 할 것이다.
# _variables_/common-test.yaml
---
env: test
service_name: service
role_arn: arn:aws:iam::Account:role/Role
account: 'Account'
tf_state_s3: s3
tf_state_dynamo: dynamoDB
common_tags:
Service: service
Environment: test
Project: IaC-Terraform
위는 참고용이며 본 포스팅의 내용과는 무관하다.
# backend/backend-test.conf
region = "<region>"
bucket = "<bucket>"
key = "<prefix>/terraform.tfstate"
profile = "<profile>"
assume_role = {
role_arn = "arn:aws:iam::<Account>:role/<Role>"
}
dynamodb_table = "dev-dolfin-global-lock"
encrypt = true
conf 파일에는 위와 같이 환경 별로 필요한 정보를 기입을 하면 된다.
# provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 0.12"
backend "s3" {}
}
provider "aws" {
region = var.region
assume_role {
role_arn = local.common_info.role_arn
}
}
# CLI 에서 값 확인
% terraform init -backend-config=backend/backend-dev.conf
Initializing the backend...
Initializing modules...
╷
│ Error: Backend configuration changed
│
│ A change in the backend configuration has been detected, which may require migrating existing state.
│
│ If you wish to attempt automatic migration of the state, use "terraform init -migrate-state".
│ If you wish to store the current configuration with no changes to the state, use "terraform init -reconfigure".
backend 블록이 변경이 되었기에 위와 같이 init 을 통해 초기화하며 -backend-config 옵션을 추가하여 conf 파일을 경로를 지정해주면 된다.
하지만 위와 같은 에러가 발생을 하였다.
에러 내용
상태의 자동 마이그레이션을 시도하려면 "terraform init -migrate-state"를 사용한다.
상태를 변경하지 않고 현재 구성을 저장하려면 "terraform init -reconfigure"를 사용한다.
# CLI 에서 값 확인
% terraform init -migrate-state -backend-config=backend/backend-dev.conf
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 modules...
Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v5.38.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.
작성자의 경우 -migrate-state 옵션을 추가하여 init 을 하였고, 정상적으로 초기화 된 것을 확인할 수 있었다.