TerraformPilot

DevOps

Fix Terraform Error - Error Creating API Gateway - ConflictException

Fix ConflictException when creating AWS API Gateway resources in Terraform. Handle duplicate REST APIs, stages, and resource path conflicts.

LLuca Berton1 min read

Quick Answer

#

An API Gateway resource with the same name or path already exists. Import the existing resource, use a unique name, or delete the orphaned API before re-creating.

The Error

#
Error: error creating API Gateway Rest API: ConflictException: 
REST API with this name already exists
Error: error creating API Gateway Stage: ConflictException: 
Stage already exists

What Causes This

#
  • REST API or stage created outside Terraform (Console, CLI, another config)
  • Previous apply failed after creating the resource but before recording state
  • Duplicate resource paths in the same API

How to Fix It

#

Solution 1: Import Existing API

#
# Find the API ID
aws apigateway get-rest-apis --query 'items[?name==`my-api`].id' --output text
 
# Import the REST API
terraform import aws_api_gateway_rest_api.main abc123def4
 
# Import a stage
terraform import aws_api_gateway_stage.prod abc123def4/prod

Solution 2: Use Unique Names

#
resource "aws_api_gateway_rest_api" "main" {
  name        = "${var.project}-${var.environment}-api"
  description = "API for ${var.project}"
 
  endpoint_configuration {
    types = ["REGIONAL"]
  }
}
 
resource "aws_api_gateway_stage" "prod" {
  deployment_id = aws_api_gateway_deployment.main.id
  rest_api_id   = aws_api_gateway_rest_api.main.id
  stage_name    = var.environment
}

Solution 3: Delete Orphaned API

#
# List all APIs
aws apigateway get-rest-apis
 
# Delete the conflicting one
aws apigateway delete-rest-api --rest-api-id abc123def4

Troubleshooting Checklist

#
  1. ✅ Does the API already exist in AWS Console?
  2. ✅ Is the API in Terraform state? (terraform state list | grep api_gateway)
  3. ✅ Are resource paths unique within the API?
  4. ✅ Did a previous apply fail mid-creation?
#

Conclusion

#

API Gateway ConflictException means the resource exists. Import it, use unique names with environment prefixes, or delete the orphaned resource. Check both AWS Console and Terraform state to find the duplicate.

#Terraform#Troubleshooting#DevOps#Error Fix#Infrastructure as Code

Share this article