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.
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.
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.
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.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform