[Terraform] S3 Backend 사용.

Cloud&Container/IaC 2020. 7. 31. 14:21

공식 문서는 아래 참고 하세요.

https://www.terraform.io/docs/backends/types/s3.html

 

terraform {
  backend "s3" {
    bucket = "버킷이름"
    key    = "폴더/파일"
    region = "ap-northeast-2"
  }
}
  • s3 backend 를 사용하기 위해서 먼저 s3 bucket 을 생성 합니다.
  • key 부분은 미리 생성해 놓지 않아도 terraform 실행을 하시면 생성이 됩니다.
  • 만약 bucket 을 만들어 놓지 않고 실행을 하게 되면 아래와 같은 에러가 발생 합니다.
Error refreshing state: BucketRegionError: incorrect region, 
the bucket is not in 'ap-northeast-2' region at endpoint ''
	status code: 301, request id: , host id:
  • bucket 을 생성 하고  $ terraform init 을 해도 같은 에러가 계속 발생 합니다.
  • 이 경우 .terraform 폴더를 삭제 하고 다시 시도 하시면 정상적으로 동작 합니다.

Terraform 기본 명령어)

$ terraform init

$ terraform plan

$ terraform apply

$ terraform destroy

.aws 에 aws configure 를 통한 설정이 되어 있어야 합니다.

 

 

: