terraform v. 0.14.8
I am creating a reusable module to deploy 6+ unique event triggers (aadding more in the future)
Using Terraform workspace - default(for developers), dev, test and prod
The module should detect the workspace and deploy only the values for that environment. The module needs a unique name and a cron schedule. I have tried, unsuccessfully to use a map variable and a for_each loop. Has anyone tackled a similar scenario? Need help with the for_each loop and/or the variable layout (should i be using locals{})?
variables file:
variable "event_triggers" {
type = map
default = {
"default" = {
"name1" = "cron(8 * ? * * *)"
"2name" = "cron(2 * ? * * *)"
"process" = "cron(7 * ? * * *)"
}
"dev" = {
"name1" = "cron(4 * ? * * *)"
"2name" = "cron(1 * ? * * *)"
"process" = "cron(5 * ? * * *)"
}
"test" = {
"name1" = "cron(1 * ? * * *)"
"2name" = "cron(1 * ? * * *)"
"process" = "cron(5 * ? * * *)"
}
}
"prod" = {
"name1" = "cron(14 * ? * * *)"
"2name" = "cron(11 * ? * * *)"
"process" = "cron(15 * ? * * *)"
}
}
}
create-eventbridege.tf
module "event_trigger" {
count = “${lookup[terraform.workspace] != ““}
for_each = var.event_triggers
source = "../../../modules/aws/eventbridge_event"
name = "${each.key}-event-trigger"
schedule = each.value
}
…/…/…/modules/aws/eventbridge_event/main.tf
resource "aws_cloudwatch_event_rule" "deploy_event_rule" {
name = var.name
schedule_expression = var.schedule
}