Home > Software engineering >  How to specify path parameters when targeting an API Gateway with EventBridge
How to specify path parameters when targeting an API Gateway with EventBridge

Time:06-10

I have an ECS service sat behind an API Gateway, for a subset of paths (/foo/{proxy }) using ANY method, the API Gateway proxies the requests to the ECS service.

I have an EventBridge Bus with a rule attached to it - this rule matches a subset of events that are sent over the bus (and this filtering is important - which is why we aren't using SQS). This rule has a target which points to a path (foo/events/ebevent) on the API Gateway, and we want to POST the event to it.

The infrastructure is defined as follows using Terraform:

resource "aws_cloudwatch_event_bus" "test_bus" {
  name = "test_bus"
}

resource "aws_cloudwatch_event_rule" "test_rule" {
  name        = "my_sample_rule"
  description = "Matches some events."

  event_bus_name = aws_cloudwatch_event_bus.test_bus.name

  event_pattern = <<EOF
{
  "detail-type": ["test_rule"]
}
EOF
}

resource "aws_cloudwatch_event_target" "test_api_gw_proxy_target" {
  #devstage corresponds to the deployed stage on the API Gateway
  #POST corresponds to the desired HTTP method
  arn            = "${var.payment_processor_execution_arn}/devstage/POST/foo/events/ebevent"
  rule           = aws_cloudwatch_event_rule.test_rule.name
  event_bus_name = aws_cloudwatch_event_bus.test_bus.name
  http_target {
    path_parameter_values = ["foo/events/ebevent"]
  }
}

The events are sending through to the API GW and are being proxied to the ECS task as expected. The issue is that the slashes in the URL are encoded as / - this means that when the event gets to the ECS container, the URL is foo/events/ebevent - since the slash is encoded, it doesn't match any patterns defined in the router on the ECS task image, and the request fails out. Is there any way to stop this URL string from being encoded by EventBridge?

The alternative is to alter the routing configuration in the ECS image (in order to handle decoding the slashes), but there's concerns over regression testing such a significant change to the router - as it could potentially impact other requests to the task.

CodePudding user response:

Are you sure you need path_parameter_values since you are already defining that path in the resource arn? Looking at the documentation for path_parameter_values it seems like you would only need to use that if you are trying to fill in path wildcard values, which you do not appear to be doing.

For example if your endpoint arn was: "${var.payment_processor_execution_arn}/devstage/POST/*/*/*" then your path_parameter_values would need to be:

path_parameter_values = ["foo", "events", "ebevent"]

Notice how they are separate list elements, instead of a single string with slashes in it.

  • Related