2017-10-10 1 views
1

세 가지 환경에서 나는 환경에 따라 변수를 동적으로 설정할 수 있기를 원합니다. 아래 예제에서 인스턴스 유형이 dev와 prod 사이에 다르다고 가정 해 보겠습니다. 나는 vars.tf 파일을 내 terraform.tfvars과 함께 가지고 있지 않으면 모듈 내에서 instance_type을 참조 할 수 없습니다.Terraform - 모듈에 tfvars를 사용하는 방법

내가 오류는 다음과 같습니다

unknown variable referenced: 'instance_type'. define it with 'variable' blocks

즉,이 파일이 modules/apollo/vars.tf에서 똑같은 파일이 아닐 것이다 다음, 그렇다면?

나는 modules/apollo/vars.tf이 모듈에 필요한 필요한 변수를 정의했다고 생각했다. env-dev/services/apollo/ 아래의 "루트"수준에서 필요하다고 생각하지 않았습니다. 이렇게하는 "더 나은"방법이 있다면, 나는 모두 귀입니다.

├── env-dev 
│   └── services 
│    └── apollo 
│     ├── main.tf 
│     ├── terraform.tfvars  
│     └── vars.tf # Do i need this? 
├── env-test 
├── global 
├── mgmt 
└── modules 
    ├── apollo 
    │   ├── main.tf 
    │   ├── user_data.tpl 
    │   └── vars.tf 
    └── defaults 
     └── main.tf 

ENV 데브/서비스/아폴로/terraform.tfvars

instance_type = "t2.medium" 

ENV-자극/서비스/아폴로/terraform.tfvars

instance_type = "t2.large" 

모듈/아폴로/vars.tf

variable "instance_type" { 
    description = "EC2 Instance Type" 
} 

modules/apollo/main.tf

resource "aws_instance" "instance" {  
    ... 
    instance_type   = "${var.instance_type}" 
    ... 
} 

답변

1

구조를 조정하십시오. 이것은 애플리케이션에 대한 이해입니다.

├── dev 
│ └── apollo_terraform.tfvars  
├── test 
│ └── apollo_terraform.tfvars 
├── global 
│ └── apollo_terraform.tfvars 
├── mgmt 
│ └── apollo_terraform.tfvars 
├── main.tf, vars.tf, output.tf, apollo.tf, default.tf, etc 
└── modules 
    ├── apollo 
    │ ├── main.tf 
    │ ├── user_data.tpl 
    │ └── vars.tf 
    └── defaults 
     └── main.tf 

apollo.tf은 주 모듈 apollo를 사용하는 소스 모듈 코드를가집니다. default.tf

계획에 대한 동일한 설정/다음과 같이해야합니다 명령을 적용

terraform plan -var-file=${env}/apollo_terraform.tfvars