Terraform - создать запланированное выражение cloudwatch на основе переменной - ожидаемое выражение, но найденное "*" - PullRequest
0 голосов
/ 08 января 2019

Я пытаюсь создать правило события AWS Clouwatch через terraform

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"]
}

Я хочу создать выражение schedule_expression на основе переменной AutoStopSchedule, как это сделать?

Попробовал следующее:

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 "*"

Ответы [ 2 ]

0 голосов
/ 08 января 2019

Вам не нужно этого делать. Что вам нужно сделать, это использовать вместо этого локальный, например:

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} ранее.

0 голосов
/ 08 января 2019

Спасибо за ссылку @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. Не могу принять свой ответ в ближайшие 2 дня

...