Search code examples
terraformamazon-rdsterraform-provider-aws

What should the payload for a RDS Data Execute Statement in Terraform look like?


I am trying to setup an AWS EventBridge Scheduler targeting a universal target with the RDS Data Execute Statement API. This cron job should run daily and execute a small SQL script. This is a minimal version of the script in terraform:

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": "..."
  })
  }
}

When trying to apply this I get the following error message:

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.

This error message and the documentation led us to believe that the input does not require any further JSON encoding as it only accepts JSON input.

The new input looked like this:

input = {
        "resourceArn": "...",
        "secretArn": "...",
        "sql": "..."
  }

This produced a new error: Inappropriate value for attribute "input": string required.

I tried sending the input as encoded JSON, raw JSON and could not get it to be formatted as a string. I apparently need a string, but don't know how to format it as such.

I'd appreciate any hints on how the input should be formatted.


Solution

  • Turns out the error message was kind of right. The correct form is

    input = jsonencode({
            "ResourceArn": "...",
            "SecretArn": "...",
            "Sql": "..."
      })
    

    Apparently terraform is case-sensitive.