Terraform │ 错误:不允许使用变量 |随机所需提供商

问题描述 投票:0回答:1

**当我尝试验证此 Terraform 配置时 ** 显示错误

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.58.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "3.6.2"
    }
  }
}
provider "aws" {
  region = "eu-north-1"
}
provider "random" {}

resource "random_id" "rand" {
byte_length = 8
}

variable "random_num" {
  description = "random number for S3 Bucket"
  type        = string
  default     = random_id.rand.hex

}
output "balti" {
  value = var.random_num
}

错误:不允许使用变量 │ │ 在 main.tf 第 25 行,变量“randoms”中: │ 25: 默认 = random_id.rand.hex │ │ 此处不能使用变量。

我期待输出的随机数。

terraform devops terraform-provider-aws
1个回答
0
投票

variable
声明块
default
值中确实不允许存在可能变化的值(在这种情况下导出资源属性)。您可以使用
local
coalesce
函数来代替:

variable "random_num" {
  description = "random number for S3 Bucket"
  type        = string
}

locals {
  random_num = coalesce(var.random_num, random_id.rand.hex)
}

output "balti" {
  value = local.random_num
}
© www.soinside.com 2019 - 2024. All rights reserved.