根据变量创建cloudwatch计划表达式 - 预期表达式但发现“*”

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

我正在尝试通过 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"]
}

得到

expected expression but found "*"

interpolation terraform amazon-cloudwatch
2个回答
2
投票

感谢您的链接@ydaetskcoR,它很有帮助!!

变量.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 * * ? *)"
  } 
}

主.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。无法接受我自己在接下来的两天内的答案


1
投票

你不需要这样做。你需要做的是使用本地的,比如:

variable "AutoStopSchedule" {
   default = "5"
}


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

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

如果你应用地形,你会得到:

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

Outputs:

schedule_expression = cron(5 * * * ? *)

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

© www.soinside.com 2019 - 2024. All rights reserved.