The PipelineNameInUseException error occurs when you try to create a new CodePipeline with a name that already exists in your AWS account and region. This happens most often with Terraform when infrastructure is created outside of Terraform or when state is out of sync. Resolve it by importing the existing pipeline, using a different name, or deleting the existing pipeline first.
AWS CodePipeline enforces unique pipeline names per AWS account and region. When you attempt to create a new pipeline using the CreatePipeline API (which Terraform does), AWS checks if a pipeline with that name already exists. If it does, the API returns a PipelineNameInUseException error and the resource creation fails. This error is particularly common in Terraform workflows because: 1. **Out-of-sync state**: Someone created the pipeline manually or through another deployment tool, but Terraform doesn't know about it 2. **Lost state file**: If your local Terraform state file was deleted or reset, Terraform loses track of already-created pipelines 3. **Duplicate apply**: Running Terraform with a local state backend multiple times after state loss causes duplicate creation attempts 4. **State corruption**: State file becomes corrupted or doesn't properly persist the pipeline resource The error message indicates that AWS already has a pipeline with the requested name, so Terraform cannot proceed.
First, verify that the pipeline exists in AWS CodePipeline:
aws codepipeline list-pipelines --region us-east-1Look for your pipeline name in the output. You can also check the AWS Console by navigating to CodePipeline and searching for the pipeline name.
If the pipeline exists, proceed to the next step. If it doesn't exist, check for typos in your Terraform configuration.
If the pipeline already exists in AWS and you want Terraform to manage it, import it into your Terraform state:
terraform import aws_codepipeline.your_resource_name your-pipeline-nameReplace your_resource_name with the logical name of your aws_codepipeline resource in Terraform (the part after the dot in your .tf file).
Example:
terraform import aws_codepipeline.main my-ci-pipelineAfter a successful import, Terraform will update your state file to track the existing pipeline. Verify the import:
terraform state show aws_codepipeline.your_resource_nameIf the existing pipeline is orphaned or no longer needed, delete it from AWS:
aws codepipeline delete-pipeline --name your-pipeline-name --region us-east-1Warning: Deleting a pipeline also deletes its history and any stored artifacts. Be sure you don't need it before proceeding.
After deletion, Terraform can create the new pipeline:
terraform plan
terraform applyIf you want both pipelines to coexist, rename your pipeline in the Terraform configuration:
resource "aws_codepipeline" "main" {
name = "my-new-pipeline-name" # Use a unique name
role_arn = aws_iam_role.codepipeline.arn
# ... rest of configuration
}Then apply the changes:
terraform plan
terraform applyMake sure the new name is unique within your AWS account and region.
To avoid future state sync issues, verify your Terraform state matches AWS reality:
terraform refreshThis command updates your local state to reflect the current state of resources in AWS without making any changes.
Then check if there are any differences:
terraform planIf terraform plan shows no changes, your state is consistent with AWS.
To prevent future state loss, store your Terraform state in a remote backend rather than locally:
For AWS S3 + DynamoDB:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "codepipeline/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}For Terraform Cloud:
terraform {
cloud {
organization = "my-org"
workspaces {
name = "production"
}
}
}A remote backend ensures your state is durable, shared across team members, and protected from accidental deletion.
Why local state causes problems: When Terraform state is stored locally (terraform.tfstate in your project directory), only your machine has knowledge of created resources. If you switch machines, delete your project folder, or use a different clone of the repository, that knowledge is lost. Team members have no way to know what resources were created. Always use a remote backend in production.
Region-specific pipeline names: Pipeline names must be unique per AWS account and region. You can have a pipeline named "my-pipeline" in us-east-1 and another named "my-pipeline" in eu-west-1. However, each name within a region must still be unique.
Import limitations: When you import a pipeline into Terraform state, Terraform only tracks the pipeline resource itself. Any artifacts, execution history, or related resources (IAM roles, S3 buckets) are not automatically imported and must be manually configured or imported separately.
CI/CD implications: If this error occurs in your CI/CD pipeline, it usually means the pipeline's Terraform state is not being stored or retrieved correctly. Ensure your CI/CD system has access to the remote state backend and proper AWS credentials. Check that the S3 bucket or Terraform Cloud workspace exists and has the correct permissions.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform