Terraform & AWS CodePipeline - Dynamically define actions on a stage

蹲街弑〆低调 提交于 2019-12-10 10:41:51

问题


I'm not sure if this is possible but thought I'd ask to check first.

I have a CodePipeline defined in terraform, with a single Stage that has multiple actions.

The Stage is pulling code from CodeCommit, and each action defines what repositories I want to use in the Pipeline.

I'd like to create a variable that has a list of the CodeCommit repository names, and then dynamically create an action for each repository in that list.

Is that possible with terraform? I know you can use count to achieve this normally, but I think that's only at a resource level?

Code snippet is below:

resource "aws_codepipeline" "Test" {
  name     = "Test"
  role_arn = "${aws_iam_role.Test.arn}"

  "artifact_store" {
    location = "${aws_s3_bucket.Test.bucket}"
    type     = "S3"
  }

  "stage" {
    name = "Source"

    ####LOOP OVER EACH ITEM IN LIST###
    "action" {
        ...
    }
  }

  stage {
    name = "Build"

    ...

    }
  }
}

Thank you for your help.

EDIT:

Getting an error with this as the trigger for the null resource at the moment. I've tried a few different variations of this as well:

resource "null_resource" "CodePipeline" {
  count = "${length(var.repositories)}"

  triggers {
    action = {
      category         = "Source"
      name             = "Repository-${element(keys(var.repositories), count.index)}"
      owner            = "AWS"
      provider         = "CodeCommit"
      version          = "1"
      output_artifacts = ["Repository-${element(keys(var.repositories), count.index)}"]

      configuration {
        RepositoryName = "${element(keys(var.repositories), count.index)}"
        BranchName     = "${lookup(var.repositories, element(keys(var.repositories) ,count.index))}"
      }
    }
  }
}

回答1:


You can use null_resources for this and pass a list of actions in the stage. like this

resource "null_resource" "actions" {
    count = length(<repo_list_var>)
    triggers {
       #### ACTION ITEMS ###
       ...........
    }
}

resource "aws_codepipeline" "Test" {
    name     = "Test"
    role_arn = "${aws_iam_role.Test.arn}"

    "artifact_store" {
        location = "${aws_s3_bucket.Test.bucket}"
        type     = "S3"
    }

    "stage" {
       name = "Source"
       action = null_resource.actions.*.triggers
    }

    stage {
        name = "Build"

        ...

    }
}


来源:https://stackoverflow.com/questions/51280419/terraform-aws-codepipeline-dynamically-define-actions-on-a-stage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!