gpt4 book ai didi

interpolation - Terraform - 根据变量创建 cloudwatch 计划表达式 - 预期表达式但找到 "*"

转载 作者:行者123 更新时间:2023-12-05 05:12:59 26 4
gpt4 key购买 nike

我正在尝试通过 Terraform 创建 AWS Clouwatch 事件规则

variable "schedule_expression" {
default = "cron(5 * * * ? *)"
description = "the aws cloudwatch event rule scheule expression that specifies when the scheduler runs. Default is 5 minuts past the hour. for debugging use 'rate(5 minutes)'. See https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html"
}

我想指定变量而不是 5

variable "AutoStopSchedule" {
default = "5"
}

variable "schedule_expression" {
default = "cron(${var.AutoStopSchedule} * * * ? *)"
description = "the aws cloudwatch event rule scheule expression that specifies when the scheduler runs. Default is 5 minuts past the hour. for debugging use 'rate(5 minutes)'. See https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html"
}

但得到:

Error: variable "schedule_expression": default may not contain interpolations

主要.tf

# Cloudwatch event rule
resource "aws_cloudwatch_event_rule" "check-scheduler-event" {
name = "check-scheduler-event"
description = "check-scheduler-event"
schedule_expression = "${var.schedule_expression}"
depends_on = ["aws_lambda_function.demo_lambda"]
}

我想根据 AutoStopSchedule 变量创建 schedule_expression,该怎么做?

尝试以下:

resource "aws_cloudwatch_event_rule" "check-scheduler-event" {
name = "check-scheduler-event"
description = "check-scheduler-event"

#schedule_expression = "cron(15 * * * ? *)"
schedule_expression = "${var.AutoStopSchedule == "5" ? cron(5 * * * ? *) : cron(15 * * * ? *)}"
depends_on = ["aws_lambda_function.demo_lambda"]
}

正在获取预期的表达式但找到了“*”

最佳答案

你不需要那样做。您需要做的是改用本地的,例如:

variable "AutoStopSchedule" {
default = "5"
}


locals{
schedule_expression= "cron(${var.AutoStopSchedule} * * * ? *)"
}

output "schedule_expression" {
value = "${local.schedule_expression}"
}

如果你应用 terraform 你会得到:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

schedule_expression = cron(5 * * * ? *)

使用它${local.sschedule_expression} 之前有 ${var.schedule_expression}。

关于interpolation - Terraform - 根据变量创建 cloudwatch 计划表达式 - 预期表达式但找到 "*",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54092865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com