How to write codepipeline module that supports multiple stages?

439 views
Skip to first unread message

egul...@gmail.com

unread,
Aug 2, 2017, 3:49:19 PM8/2/17
to Terraform
Hello everyone,

I'm writing codepipeline module and I'm trying to figure it out the best way on how to write stages in a module, so they can be expanded as needed.


As of now I have:


# This supports only 1 environment and in the near future I would like to add a lot more stages to codepipeline.

resource "aws_codepipeline" "pipeline" {
  name     = "${var.pipeline_name}"
  role_arn = "${aws_iam_role.pipelineRole.arn}"
  artifact_store {
    location = "${aws_s3_bucket.pipeline_s3_bucket.bucket}"
    type     = "S3"
  }
  stage {
    name = "Source"
    action {
      name             = "DetectCodeChanges"
      category         = "Source"
      owner            = "AWS"
      provider         = "CodeCommit"
      version          = "1"
      output_artifacts = ["MyApp"]
      configuration {
        RepositoryName = "${var.codeCommit_repository}"
        BranchName     = "${var.codeCommit_branch}"
      }
    }
  }
  stage {
    name = "PushToDEV"
    action {
      name            = "Deploy"
      category        = "Deploy"
      owner           = "AWS"
      provider        = "CodeDeploy"
      input_artifacts = ["MyApp"]
      version         = "1"
      configuration {
         ApplicationName     = "${aws_codedeploy_app.codedeploy_configurations_app.name}"
         DeploymentGroupName = "${aws_codedeploy_deployment_group.DeploymentToNewEC2underASG.deployment_group_name}"
      }
    }
  }
}

and I'm thinking of somehow do something like this:


module "codepipeline" {
source "..."
....

stage-1_name = "Source"
stage-1_action_name     = "DetectCodeChanges"
stage-1_category = "Source"
stage-1_owner = "AWS"
stage-1_provider = "CodeCommit"
stage-1_version = "1"
stage-1_artifact = "MyApp"
stage-1_repo_name = "bla_repo"
stage-1_branch_name = "bla_branch"

stage-2_name = "PushToDEV"
stage-2_action_name     = "Deploy"
stage-2_category = "Deploy"
stage-2_owner = "AWS"
stage-2_provider = "CodeDeploy"
stage-2_artifact = "MyApp"
stage-2_version = "1"
stage-2_app_name = "bla_app"
stage-2_d_group = "bla_d_group"

}


but that means I'm limited to only what's hardcoded in the module.



My questions are:

 - Is it possible to write codepipeline module using 'count' to have how many stages we want? 
 
- Have anyone tried this before and wrote codepipeline module using different logic to support multiple stages?


Any ideas/suggestions are really appreciated.

Thank you,
Ernest

egul...@gmail.com

unread,
Aug 4, 2017, 3:28:41 PM8/4/17
to Terraform
I'm trying to use 'count' & interpolation based on vancluevertech post and gruntwork post and here's what I have so far:



resource "aws_codepipeline" "pipeline" {
  name     = "${var.pipeline_name}"
  role_arn = "${aws_iam_role.pipelineRole.arn}"
  artifact_store {
    location = "${aws_s3_bucket.pipeline_s3_bucket.bucket}"
    type     = "S3"
  }
   count = "${var.number_of_stages}"
   stage {
      name = "Stage-${count.index}"
      action {
         name              = "stage-${count.index}-action-name"                                          
         category          = "${lookup(map("CodeCommit", ""), var.provider[0], "Source")}"
         owner             = "AWS"
         provider          = "${lookup(map("CodeCommit", ""), var.codecommit_as_source, "CodeCommit")}"
         version           = "1"
         output_artifacts  = "???"                                                                        
      }
   }
   
}


The problem I have is: "How can I use count & variable lookup to set "output_artifact".



Another question is: Has anyone tried using list to accomplish similar thing in provisioning multiple stages for codepipeline?



Thanks,
Ernest
Reply all
Reply to author
Forward
0 new messages