TerraformPilot

DevOps

Fix Terraform Error - Error Creating CodePipeline - InvalidStructure

Fix InvalidStructure errors when creating AWS CodePipeline in Terraform. Handle action configuration, stage ordering, artifact naming, and IAM permission...

LLuca Berton1 min read

Quick Answer

#

The CodePipeline definition has structural errors — missing required fields, invalid action configurations, wrong artifact names, or incorrect stage ordering. Source must be the first stage, and every action's input/output artifacts must chain correctly.

The Error

#
Error: error creating CodePipeline: InvalidStructureException: 
Pipeline has only 1 stage(s). There should be a minimum of 2 stages.
Error: error creating CodePipeline: InvalidStructureException:
Invalid action configuration: No Branch specified

What Causes This

#
  • Fewer than 2 stages (minimum: Source + at least one more)
  • Missing required action configuration fields
  • Artifact names that don't chain between stages
  • Invalid action provider or category combinations

How to Fix It

#

Solution 1: Ensure Minimum 2 Stages

#
resource "aws_codepipeline" "main" {
  name     = "${var.project}-pipeline"
  role_arn = aws_iam_role.codepipeline.arn
 
  artifact_store {
    location = aws_s3_bucket.artifacts.id
    type     = "S3"
  }
 
  # Stage 1: Source (REQUIRED)
  stage {
    name = "Source"
    action {
      name             = "Source"
      category         = "Source"
      owner            = "AWS"
      provider         = "CodeStarSourceConnection"
      version          = "1"
      output_artifacts = ["source_output"]
 
      configuration = {
        ConnectionArn    = aws_codestarconnections_connection.github.arn
        FullRepositoryId = "myorg/myrepo"
        BranchName       = "main"  # Required!
      }
    }
  }
 
  # Stage 2: Build
  stage {
    name = "Build"
    action {
      name             = "Build"
      category         = "Build"
      owner            = "AWS"
      provider         = "CodeBuild"
      version          = "1"
      input_artifacts  = ["source_output"]   # Must match Source output
      output_artifacts = ["build_output"]
 
      configuration = {
        ProjectName = aws_codebuild_project.main.name
      }
    }
  }
 
  # Stage 3: Deploy (optional but common)
  stage {
    name = "Deploy"
    action {
      name            = "Deploy"
      category        = "Deploy"
      owner           = "AWS"
      provider        = "ECS"
      version         = "1"
      input_artifacts = ["build_output"]  # Must match Build output
 
      configuration = {
        ClusterName = aws_ecs_cluster.main.name
        ServiceName = aws_ecs_service.app.name
      }
    }
  }
}

Solution 2: Fix Artifact Chaining

#
# ❌ Broken — artifact names don't match
stage {
  name = "Source"
  action {
    output_artifacts = ["src"]  # Named "src"
  }
}
stage {
  name = "Build"
  action {
    input_artifacts = ["source_output"]  # Looking for "source_output" — MISMATCH!
  }
}
 
# ✅ Fixed — names match
stage {
  name = "Source"
  action {
    output_artifacts = ["source_output"]
  }
}
stage {
  name = "Build"
  action {
    input_artifacts = ["source_output"]  # Matches!
  }
}

Common InvalidStructure Causes

#
Error DetailFix
"Only 1 stage"Add at least Source + Build/Deploy
"No Branch specified"Add BranchName to Source config
"Invalid action configuration"Check required fields for your action provider
Artifact not foundMatch output_artifactsinput_artifacts names
Wrong categorySource actions must be category = "Source"

Troubleshooting Checklist

#
  1. ✅ At least 2 stages defined?
  2. ✅ First stage is category "Source"?
  3. ✅ All required configuration fields present?
  4. ✅ Artifact names chain correctly between stages?
  5. ✅ IAM role has correct permissions?
#

Conclusion

#

InvalidStructureException means your pipeline definition has structural errors. Ensure minimum 2 stages, Source first, all required configuration fields present, and artifact names chaining correctly between stages. Validate the pipeline structure with terraform plan before applying.

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

Share this article