API Gateway v2 /HTTP API - Setup different CORS for different stagings

Hello,

We are trying to setup CORS settings for AWS HTTP API Gateway.

Different CORS settings for different stagings:

prod:    prod.domain.tld
staging: staging.domain.tld
dev:     dev.domain.tld

We can create one API Gateway using Terraform, with the same CORS settings for all stages but not sure how change it for stagings.
It can be done manually by updating CORS of the Gateway and deploy modifications.

But how can we do the same stuff using Terraform?
When we use count for Gateway creation with different CORS we get 3 different Gateway but not one.

Code for Gateway creation using we use aws_apigatewayv2_api with the following code:

resource "aws_apigatewayv2_api" "test" {
  name          = "test-http-api"
  protocol_type = "HTTP"
  cors_configuration {
    allow_origins = ["*"]
    allow_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST" ,"PUT"]
    allow_headers = ["Content-Type", "Authorization", "X-Amz-Date", "X-Api-Key", "X-Amz-Security-Token"]
  }

  provider = aws.aws2
}

And we have 3 different stagings using aws_apigatewayv2_stage:

resource "aws_apigatewayv2_stage" "test" {
  count  = length(var.environments)
  api_id = aws_apigatewayv2_api.test[count.index].id
  name   = var.environments[count.index]

  deployment_id = aws_apigatewayv2_deployment.test[count.index].id

  stage_variables = {
    environment = var.environments[count.index]
    cachetime   = var.environments[count.index] == "prod" ? "public, max-age=60" : "public, max-age=0"
  }

  provider = aws.aws2
}

And we also deploy all this stuff using aws_apigatewayv2_deployment:

resource "aws_apigatewayv2_deployment" "test" {
  count       = length(var.environments)
  api_id      = aws_apigatewayv2_api.test.id
  description = var.environments[count.index]

  lifecycle {
    create_before_destroy = true
  }

  provider = aws.aws2
}

Variables:

variable "environments" {
  type    = list
  default = ["staging", "prod", "dev"]
}

One more similar case - how to set different URL for Proxy integration for different stages?

resource "aws_apigatewayv2_integration" "report" {
  count            = length(var.environments)
  api_id           = aws_apigatewayv2_api.current.id
  integration_type = "HTTP_PROXY"

  connection_type      = "INTERNET"
  description          = "report"
  integration_method   = "GET"
  integration_uri      = var.environments[count.index] == "prod" ? "https://domain.tld/{proxy}" : "https://staging.domain.tld/{proxy}"
  passthrough_behavior = "WHEN_NO_MATCH"
  provider             = aws.region
}

resource "aws_apigatewayv2_route" "report" {
  count     = length(var.environments)
  api_id    = aws_apigatewayv2_api.current.id
  route_key = "GET /report/{proxy+}"
  target    = "integrations/${aws_apigatewayv2_integration.report[count.index].id}"
  provider  = aws.region
}

│ Error: error creating API Gateway v2 route: ConflictException: A route with the same routeKey already exists.

│ with module.api-gateway-primary.aws_apigatewayv2_route.report[0],
│ on modules/apig_report.tf line 14, in resource “aws_apigatewayv2_route” “report”:
│ 14: resource “aws_apigatewayv2_route” “report” {