terraform - 如果 Terraform 的条件
问题描述
我正在尝试根据环境创建不同的规则
对于资源
资源“aws_lb_listener_rule”
如果 env == prod 那么 action { type = "redirect"
redirect {
host = "${var.}"
path = "/path"
port = "443"
protocol = "HTTPS"
status_code = "HTTP_302"
}
否则动作{类型=“固定响应”
fixed_response {
content_type = "path"
status_code = 503
message_body = "${data.template_file.xxx_html.rendered}"
}
}
如何使用 terraform 实现这样的目标?
解决方案
中的一般方法terraform
是使用资源count
作为 if 语句来创建或不创建该场景中的资源。
aws_lb_listener_rule
您可以将保留的资源的计数设置redirect
为1
仅当environment
变量设置为时的值prod
。
下面的例子有你想要的效果。
resource "aws_lb_listener_rule" "production_redirect_listener" {
# Create this rule only if the environment variable is set to production
count = var.environment == "prod" ? 1 : 0
listener_arn = aws_lb_listener.arn
action {
type = "redirect"
redirect {
host = var.hostname
path = "/path"
port = "443"
protocol = "HTTPS"
status_code = "HTTP_302"
}
}
}
resource "aws_lb_listener_rule" "generic_fixed_response" {
# If the environment variable is set to production do NOT create this rule
count = var.environment == "prod" ? 0 : 1
listener_arn = aws_lb_listener.arn
action {
type = "fixed-response"
fixed_response {
content_type = "path"
status_code = 503
message_body = data.template_file.xxx_html.rendered
}
}
}
推荐阅读
- mongodb - 一个 MongoDB 深度索引会支持该深度的所有查询吗?
- python - Django:找不到这样的文件或目录
- windows - Windows Batch - 查找一行并替换为文本
- python - Django 2.2:如何在 Django Admin 中修改特定页面?
- java - 使用 printf 打印列
- jenkins - Jenkins Job 在 24 小时内检查失败的工作
- python - 如何减少循环
- ruby-on-rails - 将环境变量分离到不同的环境中
- angular - Maximum call stack size exceeded (Angular8)
- django - Django:为当前用户过滤数据库