How to fix Error: Error creating CodePipeline: PipelineNameInUseException in Terraform

TerraformINTERMEDIATEMEDIUMUpdated Dec 31, 2025ยท 90 views

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.

Quick fix

Fixes PipelineNameInUseException

aws codepipeline list-pipelines --region us-east-1
  1. Check if the pipeline already exists in AWS
  2. Import the existing pipeline into Terraform state
  3. Delete the existing pipeline if you don't need it
  4. Use a different pipeline name in Terraform
terraform โ€” error
aws codepipeline list-pipelines --region us-east-1
Error: Error creating CodePipeline: PipelineNameInUseException

What this error means

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.

Typical symptoms

  • Terraform apply fails with 'PipelineNameInUseException' error
  • Error message includes 'the specified pipeline name is already in use'
  • The pipeline exists in the AWS CodePipeline console but not in your Terraform state
  • Terraform plans to create a pipeline that already exists in AWS
  • Running terraform apply repeatedly tries to create the same pipeline

Common causes

  1. A pipeline with the same name was created manually via AWS Console or AWS CLI outside of Terraform
  2. Your Terraform state file was lost, deleted, or reset, causing Terraform to lose knowledge of existing pipelines
  3. You are using a local Terraform state backend and ran terraform apply from a different machine or location
  4. The pipeline was created by another Terraform configuration or deployment tool
  5. Terraform state is not properly persisted in a remote backend (S3, Terraform Cloud, etc.)

How to fix "Error: Error creating CodePipeline: PipelineNameInUseException"

1Check if the pipeline already exists in AWS

First, verify that the pipeline exists in AWS CodePipeline:

bash
aws codepipeline list-pipelines --region us-east-1

Look 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.

2Import the existing pipeline into Terraform state

If the pipeline already exists in AWS and you want Terraform to manage it, import it into your Terraform state:

bash
terraform import aws_codepipeline.your_resource_name your-pipeline-name

Replace your_resource_name with the logical name of your aws_codepipeline resource in Terraform (the part after the dot in your .tf file).

Example:

bash
terraform import aws_codepipeline.main my-ci-pipeline

After a successful import, Terraform will update your state file to track the existing pipeline. Verify the import:

bash
terraform state show aws_codepipeline.your_resource_name
3Delete the existing pipeline if you don't need it

If the existing pipeline is orphaned or no longer needed, delete it from AWS:

bash
aws codepipeline delete-pipeline --name your-pipeline-name --region us-east-1

Warning: 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:

bash
terraform plan
terraform apply
4Use a different pipeline name in Terraform

If you want both pipelines to coexist, rename your pipeline in the Terraform configuration:

hcl
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:

bash
terraform plan
terraform apply

Make sure the new name is unique within your AWS account and region.

5Verify Terraform state consistency

To avoid future state sync issues, verify your Terraform state matches AWS reality:

bash
terraform refresh

This 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:

bash
terraform plan

If terraform plan shows no changes, your state is consistent with AWS.

6Use a remote state backend for production

To prevent future state loss, store your Terraform state in a remote backend rather than locally:

For AWS S3 + DynamoDB:

hcl
terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "codepipeline/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-locks"
    encrypt        = true
  }
}

For Terraform Cloud:

hcl
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.

Advanced notes

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.

Frequently asked questions

What does "PipelineNameInUseException" mean?
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.
How do I fix "PipelineNameInUseException"?
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.
What causes "PipelineNameInUseException"?
A pipeline with the same name was created manually via AWS Console or AWS CLI outside of Terraform Your Terraform state file was lost, deleted, or reset, causing Terraform to lose knowledge of existing pipelines You are using a local Terraform state backend and ran terraform apply from a different machine or location
Is the "PipelineNameInUseException" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading