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


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


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