我正在尝试通过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
main.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"]
}
获取expected expression but found "*"
答案 0 :(得分:1)
您不需要这样做。您需要使用的是本地变量,例如:
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
schedule_expression = cron(5 * * * ? *)
如果您使用Terraform,您将获得:
> x.shape
(2,)
要使用它 $ {local.sschedule_expression},您之前有$ {var.schedule_expression}。
答案 1 :(得分:0)
感谢@ydaetskcoR的链接,它很有帮助!
variables.tf:
variable "schedule_expression" {
default = "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"
}
variable "AutoStopSchedule" {
default = {
"1" = "cron(30 * * * ? *)"
"2" = "cron(0 */1 * * ? *)"
"3" = "cron(0 */1 * * ? *)"
"4" = "cron(0 */12 * * ? *)"
"5" = "cron(0 10 * * ? *)"
}
}
main.tf
# Cloudwatch event rule
resource "aws_cloudwatch_event_rule" "check-scheduler-event" {
name = "check-scheduler-event"
description = "check-scheduler-event"
schedule_expression = "${lookup(var.AutoStopSchedule, var.schedule_expression)}"
depends_on = ["aws_lambda_function.demo_lambda"]
}
PS。接下来的两天不能接受我自己的答案