Backend 구성하기
Backend용 S3 버킷과 lock을 위한 dynamoDB 생성
S3 bucket as backend
테라폼의 상태를 저장하기 위해 S3 버킷을 생성합니다.
S3 버킷에 최신 상태를 유지할 수 있기 때문에 협업이 가능합니다.
DynamoDB Table for Lock
동시에 같은 파일을 수정하지 못하도록 하기 위해 DynamoDB에 작업에 대한 Lock을 생성합니다.
실습
terraform/init/art-id
디렉터리를 복사해서 새로운 디렉터리를 만듭니다. 저는 devart-preprod 로 변경해서 진행합니다 .
preprod는 개발/스테이지/QA 환경과 같이 실제 운영환경에 나가기 전에 필요한 검증을 위해 만든 인프라용 계정으로 쓰입니다.
$ cp -R art-id devart-preprod
1. terraform/init/devart-preprod/init.tf
파일을 수정합니다.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.17.0"
}
}
}
provider "aws" {
region = "ap-northeast-2"
}
# S3 bucket for backend
resource "aws_s3_bucket" "tfstate" {
bucket = "${var.account_id}-apnortheast2-tfstate"
versioning {
enabled = true # 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"
}
}
variable "account_id" {
default = "devart-preprod" # Please use the account alias for id
}
2. Terraform init
을 진행합니다.
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.17.0"...
- Installing hashicorp/aws v5.17.0...
- Installed hashicorp/aws v5.17.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
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.
3. 생성 리소스를 확인합니다.
$ terraform plan -parallelism=30
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_dynamodb_table.terraform_state_lock will be created
+ resource "aws_dynamodb_table" "terraform_state_lock" {
+ arn = (known after apply)
+ billing_mode = "PAY_PER_REQUEST"
+ hash_key = "LockID"
+ id = (known after apply)
+ name = "terraform-lock"
+ read_capacity = (known after apply)
+ stream_arn = (known after apply)
+ stream_label = (known after apply)
+ stream_view_type = (known after apply)
+ tags_all = (known after apply)
+ write_capacity = (known after apply)
+ attribute {
+ name = "LockID"
+ type = "S"
}
}
# aws_s3_bucket.tfstate will be created
+ resource "aws_s3_bucket" "tfstate" {
+ acceleration_status = (known after apply)
+ acl = (known after apply)
+ arn = (known after apply)
+ bucket = "devart-preprod-apnortheast2-tfstate"
+ bucket_domain_name = (known after apply)
+ bucket_prefix = (known after apply)
+ bucket_regional_domain_name = (known after apply)
+ force_destroy = false
+ hosted_zone_id = (known after apply)
+ id = (known after apply)
+ object_lock_enabled = (known after apply)
+ policy = (known after apply)
+ region = (known after apply)
+ request_payer = (known after apply)
+ tags_all = (known after apply)
+ website_domain = (known after apply)
+ website_endpoint = (known after apply)
+ versioning {
+ enabled = true
+ mfa_delete = false
}
}
Plan: 2 to add, 0 to change, 0 to destroy.
Plan: 2 to add, 0 to change, 0 to destroy.
이 나오면 정상입니다.
4. 이제 리소를 생성합니다.
$ terraform apply -parallelism=30
(... 중략 ...)
Plan: 2 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes <------ 정확히 yes라고 입력하셔야 합니다.
aws_dynamodb_table.terraform_state_lock: Creating...
aws_s3_bucket.tfstate: Creating...
aws_s3_bucket.tfstate: Creation complete after 2s [id=devart-preprod-apnortheast2-tfstate]
aws_dynamodb_table.terraform_state_lock: Creation complete after 7s [id=terraform-lock]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
문구가 나오면 성공입니다.
결과 스크린샷
S3 버킷

DynamoDB Table

(Optional)Multi Account 세팅
위의 동일한 절차를 다른 계정에도 적용해야 합니다. 해당 계정의 리소스 생성 정보는 생성하신 버킷에 저장됩니다.
여러 account를 구성하는 경우에는
terraform/init/art-id
폴더를 그대로 복사하셔서terraform/init/<계정 이름>
으로 만드신 후에,init.tf
에서 account_id 만 변경하시면 됩니다. 당연히 해당 내용을 적용하실 때는 AWS Credential 정보를 다른 계정으로 세팅하신 후에 진행하셔야 합니다.
먼저, 사용할 계정의 IAM 초기화 사용자를 생성하셔야 합니다. (첫 세팅만 사용)
초기화 IAM 사용자 생성위의 실습 단계를 다시 진행합니다.
terraform/init/devart-prod
폴더를 생성하고,art-id
에 있는 내용을 복사합니다.
$ cp -R art-id devart-prod
init.tf
파일에서account_id
이름을 수정합니다.
(... 생략 ...)
variable "account_id" {
default = "devart-prod" # Please change this value!!
}
이후 작업은 동일합니다.
terraform init
terraform plan -parallelism=30
terraform apply -parallelism=30
초기화 IAM 사용자는 IAM 세팅 때도 필요합니다. 해당 작업이 끝난 후 삭제하시면 됩니다.
Last updated
Was this helpful?