Terraform配置EventBridge调度RDS Data Execute Statement的负载格式问题
解决Terraform配置EventBridge Scheduler调用RDS Data API的Input格式问题
问题场景
尝试通过Terraform配置AWS EventBridge Scheduler,以通用目标调用RDS Data Execute Statement API,实现每日自动执行SQL脚本的定时任务。初始简化脚本如下:
resource "aws_scheduler_schedule" "some_sql_script" { name = "..." description = "..." schedule_expression = "cron(30 0 * * ? *)" schedule_expression_timezone = "Europe/Berlin" state = "ENABLED" flexible_time_window { mode = "OFF" } target { arn = "arn:aws:scheduler:::aws-sdk:rdsdata:executeStatement" role_arn = ... input = jsonencode({ "resourceArn": "...", "secretArn": "...", "sql": "..." }) } }
执行terraform apply时触发错误:
creating Amazon EventBridge Scheduler Schedule (...): operation error Scheduler: CreateSchedule, https response error StatusCode: 400, RequestID: ..., ValidationException: Invalid RequestJson provided. Reason Request payload is missing the following field(s): ResourceArn, SecretArn, Sql.
去掉jsonencode直接传入对象配置时,又出现新错误:
Inappropriate value for attribute "input": string required.
解决方法
问题核心在于AWS EventBridge Scheduler调用AWS SDK目标时,要求请求参数的键名采用首字母大写的驼峰格式(而非Terraform常用的小驼峰)。修正后的配置需要保留jsonencode(因为input字段强制要求字符串类型),同时调整参数键名的大小写:
resource "aws_scheduler_schedule" "some_sql_script" { name = "..." description = "..." schedule_expression = "cron(30 0 * * ? *)" schedule_expression_timezone = "Europe/Berlin" state = "ENABLED" flexible_time_window { mode = "OFF" } target { arn = "arn:aws:scheduler:::aws-sdk:rdsdata:executeStatement" role_arn = ... input = jsonencode({ "ResourceArn": "...", # 首字母大写匹配API要求 "SecretArn": "...", # 首字母大写匹配API要求 "Sql": "..." # 首字母大写匹配API要求 }) } }
关键说明
input字段必须是JSON字符串,因此必须用jsonencode将Terraform对象转换为字符串格式,否则会触发"string required"错误。- AWS SDK的
executeStatement接口要求参数名首字母大写,和Terraform常规的小驼峰命名规则不同,这是导致字段缺失错误的根本原因。
内容的提问来源于stack exchange,提问作者Leonard Buhl
相关产品推荐
相关产品推荐

