Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create cloudwatch scheduled expression based on a variable - expected expression but found "*"

I'm trying to create AWS Clouwatch event rule via 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"
}

I want to specify variable instead of 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"
}

but getting:

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

i want to create schedule_expression based on AutoStopSchedule variable, how to do it ?

Tried following:

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

getting expected expression but found "*"


1 Answers

Thanks for the link @ydaetskcoR, it was helpful !!

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. Can't acept my own answer in next 2 days


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!