본문 바로가기

개발하자

Terraform은 계획을 수행할 때 'Error: Variables not allowed'를 출력합니다

반응형

Terraform은 계획을 수행할 때 'Error: Variables not allowed'를 출력합니다

변수가 다음과 같이 선언되었습니다:

variable "MyAmi" {
  type = map(string)
}

하지만 내가 할 때:

terraform plan -var 'MyAmi=xxxx'

이해합니다:

Error: Variables not allowed

  on <value for var.MyAmi> line 1:
  (source code not available)

Variables may not be used here.

최소 코드 예제:

test.tf

provider "aws" {
}

# S3
module "my-s3" {
  source = "terraform-aws-modules/s3-bucket/aws"

  bucket = "${var.MyAmi}-bucket"
}

변수들.tf

variable "MyAmi" {
  type = map(string)
}

terraform plan -var 'MyAmi=test'

Error: Variables not allowed

  on <value for var.MyAmi> line 1:
  (source code not available)

Variables may not be used here.

좋은 의견이라도 있나?




나는 당신이 보고 있는 오류의 원인이 될 수 있는 두 가지를 보았다. 에 연결합니다.

  1. 를 실행하면 현재 디렉터리에 있는 모든 파일이 자동으로 로드됩니다. 파일이 다른 디렉터리에 있는 경우 매개 변수로 제공해야 합니다. 당신은 당신의 질문에서 당신의 변수가 파일에 있다고 말했는데, 이것은 명령이 자동으로 그 파일을 로드하지 않는다는 것을 의미한다. 픽스: 이름 변경

  2. 매개 변수를 사용할 때는 전달하는 내용이 HCL에 의해 올바르게 해석되는지 확인해야 합니다. 전달하려는 변수가 맵인 경우에는 맵으로 구문 분석할 수 있어야 합니다.

대신에 나는 더 비슷한 것을 기대할 것이다.

변수를 선언하고 명령줄을 통해 전달하는 방법에 대한 자세한 내용은 이 항목을 참조하십시오.




이 오류는 동적 리소스(예: 하위 모듈의 출력)에서 변수 값을 설정할 때도 발생할 수 있습니다:

variable  "some_arn" {
  description = "Some description"
  default     = module.some_module.some_output # <--- Error: Variables not allowed
}

변수 대신 블록을 사용하면 다음과 같은 문제가 해결됩니다:

locals {
  some_arn = module.some_module.some_output
}



같은 오류가 발생했지만, 저의 경우 terraform.tfvars 파일에서 변수 값을 따옴표(" ") 안에 포함하는 것을 잊었습니다.

이 문제는 공식 테라폼 저장소에 다음과 같이 기록됩니다:




나도 같은 문제가 있었지만, 내 문제는 변수의 기본값 주위에 인용문이 누락된 것이었다

variable "environment_name" {
  description = "Enter Environment name"
  default= test
}

이것이 제가 이 문제를 해결한 방법입니다,

 variable "environment_name" {
      description = "Enter Environment name"
      default= "test"
    }



테라폼 버전을 확인하십시오. 나는 비슷한 것을 가지고 있었는데, 모듈은 버전 1.0으로 작성되었고 나는 테라폼 버전 0.12를 사용하고 있었다.




내 데이터 소스를 포함한 목록을 모듈에 전달하려고 할 때 테라폼에서 다음 오류가 발생했습니다:

...

제 경우 모듈에 잘못된 것을 전달하고 있었습니다:

security_groups_allow_to_msk_on_port_2181 = concat(var.security_groups_allow_to_msk_2181, [data.aws_security_group.client-vpn-sg])

전체 개체가 아닌 ID만 예상했습니다. 그래서 대신 이것은 나에게 효과가 있었다:

security_groups_allow_to_msk_on_port_2181 = concat(var.security_groups_allow_to_msk_2181, [data.aws_security_group.client-vpn-sg.id])

또한 어떤 유형의 객체를 수신하는지 확인하십시오. 목록입니까? 유형을 주의하십시오. 첫 번째 인수도 이미 목록이었기 때문에 [](괄호)에 포함되었을 때 동일한 오류 메시지가 표시되었습니다.




## For my Map variable created in variables.tf file as :

variable "tags" {
  type = map(string)
  default = {`enter code here`
   "Owner_tag" = "check"
   "Name_tag" = "Name"
   "Platform_tag" = "For which platform is it"
   "Role_tag" = "What is the role assigned to this bucket"
   "Environment_tag" = "For which Environment is this"
   "Application_tag" = "For which Application is this"
   "Resource_tag" = "Resource being used"
   "Createdby_tag" = "Who created this bucket"
}
  description = "Additional resource tags"
}


## I tried the below and it worked for me on jenkins groovy passing dynamic value from parameters in jenkins :


                "-var 'tags={\"Name_tag\"=\"${Name_tag}\",\"Platform_tag\"=\"${Platform_tag}\",\"Role_tag\"=\"${Role_tag}\",\"Environment_tag\"=\"${Environment_tag}\",\"Application_tag\"=\"${Application_tag}\",\"Resource_tag\"=\"${Resource_tag}\",\"Createdby_tag\"=\"${Createdby_tag}\" }' " +
               



나에게 그것은 일치하지 않는 변수 유형이었다


반응형