当我尝试运行";Terraform init";,我在这里缺少什么?
│ Error: Variables not allowed
│
│ on ../../resources/s3-bucket/main.tf line 12, in resource "aws_s3_bucket" "s3-bucket":
│ 12: prevent_destroy = var.prevent_destroy
│
│ Variables may not be used here.
╵
╷
│ Error: Unsuitable value type
│
│ on ../../resources/s3-bucket/main.tf line 12, in resource "aws_s3_bucket" "s3-bucket":
│ 12: prevent_destroy = var.prevent_destroy
│
│ Unsuitable value: value must be known
资源:
resource "aws_s3_bucket" "s3-bucket" {
bucket = var.bucket_name
tags = {
Name = var.tags_name
Environment = var.tags_environment
}
versioning { enabled = var.versioning }
lifecycle {
prevent_destroy = var.prevent_destroy
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = var.encryption_algorithm
}
}
}
}
Var:
variable "prevent_destroy" {
type = bool
description = "Turn on or off lifecycle"
}
模块:
module "s3-bucket" {
source = "../../resources/s3-bucket"
region = var.region
access_key = var.access_key
secret_key = var.secret_key
bucket_name = var.bucket_name
tags_name = var.tags_name
tags_environment = var.tags_environment
versioning = var.versioning
prevent_destroy = var.prevent_destroy
encryption_algorithm = var.encryption_algorithm
}
TFVARS:
prevent_destroy = false
不能在lifecycle
块中使用变量[1]:
lifecycle
设置都会影响Terraform构建和遍历依赖关系图的方式。因此,只能使用文字值,因为对于任意表达式求值,处理发生得太早。
[1]https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle#literal-仅值