You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform HTTP数据源动态生成EventBridge规则的可行性与实现问询

技术问询:Terraform动态生成EventBridge规则方案

背景

目前我通过env.tfvars中的cron_schedule_info硬编码变量,在eventbridge.tf中配置AWS EventBridge规则,系统运行正常,但每次新增规则都需要手动修改硬编码内容。相关代码如下:

variables.tf

variable "cron_schedule_info" {
  type = map(list(object({
    property_id         = string
    schedule_expression = string
  })))
}

env.tfvars

cron_schedule_info = {
  AAA = [
    {
      property_id         = "property1",
      schedule_expression = "cron(0 * * * ? *)"
    },
    {
      property_id         = "property2",
      schedule_expression = "cron(5 * * * ? *)"
    },
    ...........
    ...........
    {
      property_id         = "property100",
      schedule_expression = "cron(55 * * * ? *)"
    }
  ]
  BBB = [
    {
      property_id         = "property1",
      schedule_expression = "cron(0 * * * ? *)"
    }
  ]
}

eventbridge.tf

locals {
  rule_list = flatten([
    for i, p in var.cron_schedule_info : [
      for index, data in p :
      merge(data, {
        "i" = i
      })
    ]
  ])

  rule = {
    for index, rule in local.rule_list :
    "prule_${rule.i}_${index}" => {
      description         = "Trigger cron"
      schedule_expression = rule.schedule_expression
    }
  }
}

计划将配置信息存储至数据库(表结构如下),通过自建API接口返回指定JSON格式数据,在Terraform plan/apply阶段动态生成EventBridge规则:

id | integration_type | property_id |   schedule_expression   | cron_type | active
----+-------------------+-------------+-------------------------+-----------+-------
  1 | AAA               | property1   | cron(0 * * * ? *)       | type1    | false
  2 | AAA               | property2   | cron(5 * * * ? *)       | type2    | true
  3 | AAA               | property20  | cron(55 * * * ? *)      | type3    | true
  4 | BBB               | property1   | cron(0 * * * ? *)       | type1    | true

预期API JSON响应

{
  "cron_schedule_info": {
    "AAA": [
      {
        "property_id": "property1",
        "schedule_expression": "cron(0 * * * ? *)"
      },
      {
        "property_id": "property2",
        "schedule_expression": "cron(5 * * * ? *)"
      },
      {
        "property_id": "property100",
        "schedule_expression": "cron(55 * * * ? *)"
      }
    ],
    "BBB": [
      {
        "property_id": "property1",
        "schedule_expression": "cron(0 * * * ? *)"
      }
    ]
  }
}

拟采用Terraform HTTP数据源实现该需求,现提出以下技术问询:

  1. 该方案是否合理?有无更优替代方案?
  2. 若采用该方案,如何处理API响应并改造现有代码以适配?

内容的提问来源于stack exchange,提问作者A l w a y s S u n n y

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 16:55:39