interpolation - Terraform - 根据变量创建 cloudwatch 计划表达式 - 预期表达式但找到“*”
问题描述
我正在尝试通过 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
主文件
# 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 "*"
解决方案
你不需要这样做。您需要做的是使用本地代替,例如:
variable "AutoStopSchedule" {
default = "5"
}
locals{
schedule_expression= "cron(${var.AutoStopSchedule} * * * ? *)"
}
output "schedule_expression" {
value = "${local.schedule_expression}"
}
如果你 terraform apply 你得到:
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
schedule_expression = cron(5 * * * ? *)
要使用它 ${local.sschedule_expression} 之前有 ${var.schedule_expression} 。
推荐阅读
- openrefine - 初学者:时间和文本方面的问题
- imagemagick - 动画 GIF:反转动画并将其附加到动画的末尾
- javascript - 在 clic 上打开另一个弹出窗口时关闭当前弹出窗口
- javascript - SapUi5 cookie 获取/设置
- .net - AWS SDK .NET Fargate ECS 获取当前任务的 ARN
- python - 如何使用带有编码头的xml文件的beautifulsoup解决问题
- jenkins - 如何在 Jenkins JCasC 插件的 .yaml 配置中定义多个可定位资源标签?
- android - 布局和drawable的居中方式不同
- hadoop - HBase MasterProcWALs 问题
- javascript - 我们可以在不使用上下旋转的情况下增加和减少 html 中数字框(在 forms 中)的值吗?