How to fix Error: Error creating CodeBuild Project: ResourceAlreadyExistsException in Terraform
This error occurs when Terraform attempts to create a CodeBuild project that already exists in your AWS account. It typically happens when there's a mismatch between Terraform state and actual AWS resources.
Quick fix
Fixes Error: Error creating CodeBuild Project: ResourceAlreadyExistsException
aws codebuild batch-get-projects --names your-project-name --region us-east-1- Check if the CodeBuild project exists in AWS
- Import the existing resource into Terraform state
- Verify the imported resource matches your configuration
- Delete the existing AWS resource if you want to recreate it
aws codebuild batch-get-projects --names your-project-name --region us-east-1Error: Error creating CodeBuild Project: ResourceAlreadyExistsException
What this error means
The ResourceAlreadyExistsException error indicates that AWS CodeBuild is rejecting the creation of a project because a project with that name already exists in the region. This commonly happens in three scenarios: 1. **State mismatch**: The CodeBuild project was created outside of Terraform (manually via AWS Console, AWS CLI, or another tool), but Terraform's state file doesn't know about it. When Terraform tries to create what it thinks is a new resource, AWS rejects it because the name is taken. 2. **Orphaned state**: The resource exists in AWS but Terraform's state file was lost, deleted, or corrupted. Terraform sees no record of it and attempts to recreate it. 3. **Multiple Terraform configurations**: Different Terraform configurations or workspaces are trying to manage the same CodeBuild project, causing a naming conflict.
Typical symptoms
- Terraform apply fails with ResourceAlreadyExistsException during aws_codebuild_project creation
- The CodeBuild project actually exists in the AWS Console, but Terraform doesn't recognize it
- terraform plan shows '+' (create) for a resource that already exists in AWS
- The error message includes the CodeBuild project name that's causing the conflict
Common causes
- The CodeBuild project was created manually or by another process outside of this Terraform configuration
- Terraform state file was lost, deleted, or reset, causing Terraform to lose track of previously managed resources
- Multiple Terraform configurations or workspaces are attempting to manage the same CodeBuild project name
- The project name is hardcoded without unique identifiers, causing conflicts when scaling or managing multiple environments
- A previous terraform apply succeeded but wasn't recorded in the state file correctly
How to fix "Error: Error creating CodeBuild Project: ResourceAlreadyExistsException"
First, verify that the project actually exists in your AWS account and region using the AWS CLI:
aws codebuild batch-get-projects --names your-project-name --region us-east-1If the project is listed in the output, it exists. If not, the issue may be a different error. Note the exact project name from the error message and use that in the command.
If the project exists and you want to manage it with Terraform, import it into your Terraform state:
terraform import aws_codebuild_project.my_project your-project-nameReplace my_project with the local resource name in your Terraform configuration and your-project-name with the actual AWS CodeBuild project name. After importing, Terraform will track this resource and won't try to recreate it.
After importing, run terraform plan to see if there are any differences:
terraform planIf Terraform wants to modify the imported resource to match your configuration, review the changes carefully. You may need to update your .tf files to match the existing AWS configuration, or adjust the AWS resource to match your Terraform code.
If the existing CodeBuild project is orphaned or no longer needed, delete it from AWS and let Terraform recreate it:
aws codebuild delete-project --name your-project-name --region us-east-1After deletion, run terraform apply to create the project through Terraform. Only do this if you're sure the project isn't in use by other services.
Update your Terraform configuration to use unique project names, especially if managing multiple environments or projects:
resource "aws_codebuild_project" "example" {
name = "${var.project_prefix}-${var.environment}-codebuild"
service_role = aws_iam_role.codebuild_role.arn
artifacts {
type = "NO_ARTIFACTS"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/standard:5.0"
type = "LINUX_CONTAINER"
image_pull_credentials_type = "CODEBUILD"
}
source {
type = "GITHUB"
location = "https://github.com/owner/repo.git"
}
}Use variables for naming to avoid hardcoded conflicts across different deployments.
If you've verified the project exists but Terraform still sees it as missing, refresh your Terraform state:
terraform refreshThis reads the current state of all resources in AWS and updates your local state file. After refreshing, check terraform plan again to see if the conflict is resolved.
Advanced notes
Remote state management: Using remote state (S3, Terraform Cloud) with state locking prevents state file corruption and concurrent modifications that can cause this error.
Multiple environments: If managing multiple AWS accounts or regions, use separate Terraform workspaces or state files per environment to avoid naming collisions.
AWS resource cleanup: CloudFormation stacks or other IaC tools may also create CodeBuild projects. Ensure only one management tool (Terraform) is responsible for a given resource.
Project naming patterns: Consider using a naming convention like {app}-{environment}-{component} (e.g., myapp-prod-cicd) to minimize conflicts in shared AWS accounts.
Frequently asked questions
- What does "Error: Error creating CodeBuild Project: ResourceAlreadyExistsExceptio" mean?
- The ResourceAlreadyExistsException error indicates that AWS CodeBuild is rejecting the creation of a project because a project with that name already exists in the region. This commonly happens in three scenarios: 1. State mismatch: The CodeBuild project was created outside of Terraform (manually via AWS Console, AWS CLI, or another tool), but Terraform's state file doesn't know about it.
- How do I fix "Error: Error creating CodeBuild Project: ResourceAlreadyExistsExceptio"?
- This error occurs when Terraform attempts to create a CodeBuild project that already exists in your AWS account. It typically happens when there's a mismatch between Terraform state and actual AWS resources.
- What causes "Error: Error creating CodeBuild Project: ResourceAlreadyExistsExceptio"?
- The CodeBuild project was created manually or by another process outside of this Terraform configuration Terraform state file was lost, deleted, or reset, causing Terraform to lose track of previously managed resources Multiple Terraform configurations or workspaces are attempting to manage the same CodeBuild project name
- Is the "Error: Error creating CodeBuild Project: ResourceAlreadyExistsExceptio" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐Terraform Official Documentation
- ๐Terraform AWS Provider: aws_codebuild_project(Official Docs)
- ๐AWS CodeBuild: Creating a Build Project(Official Docs)
- ๐
- ๐Terraform: Import command documentation(Official Docs)