처음 시작하는 Infrastructure as Code: AWS & 테라폼을 수강하며 정리한 내용입니다.
terraform.tfstate
라는 파일명을 가짐apply
의 결과를 저장해놓은 상태apply
로 적용한 시점의 상태iam
관련 state
파일을 Backend
로 옮기기init.tf
파일을 다음과 같이 작성provider "aws" {
region = "ap-northeast-2"
}
# S3 Bucket for Backend
resource "aws_s3_bucket" "tfstate" {
bucket = "tf101-chori-apne2-tfstate"
}
resource "aws_s3_bucket_versioning" "versioning_tfstate" {
bucket = aws_s3_bucket.tfstate.id
versioning_configuration {
status = "Enabled" # Prevent from deleting tfstate file
}
}
# DynamoDB for terraform state lock
resource "aws_dynamodb_table" "terraform_state_lock" {
name = "terraform-lock"
hash_key = "LockID"
billing_mode = "PAY_PER_REQUEST"
attribute {
name = "LockID"
type = "S"
}
}
terraform init
명령어 실행terraform plan
을 입력하면 두 가지의 리소스가 만들어질 것이라고 안내됨iam
디렉토리에 backend.tf
파일을 생성하고 아래와 같이 작성terraform {
backend "s3" {
bucket = "tf101-chori-apne2-tfstate"
key = "workspace/iam/terraform.tfstate"
region = "ap-northeast-2"
encrypt = true
dynamodb_table = "terraform-lock"
}
}
- backend: Backend의 타입을
s3
로 지정- bucket:
s3
버킷 이름- key:
s3
내에서 저장되는 경로, 실제 경로와 같으면 좋음- region:
s3
의 region- encrypt: 암호화 여부
- dynamodb_table: DynamoDB 테이블 지정
terraform init
명령어 실행yes
입력s3
버킷에 파일이 업로드된 것을 확인