Terraform和AWS CodePipeline-在舞台上动态定义操作

时间:2018-07-11 08:14:04

标签: terraform aws-codepipeline

我不确定是否可以,但是我想先检查一下。

我在CodePipeline中定义了一个terraform,其中一个Stage具有多个动作。

StageCodeCommit中提取代码,每个动作定义了我想在Pipeline中使用的存储库。

我想创建一个具有CodeCommit存储库名称列表的变量,然后为该列表中的每个存储库动态创建一个动作。

terraform有可能吗?我知道您可以使用count正常完成此操作,但是我认为那只是在资源级别?

代码段如下:

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"

    ...

    }
  }
}

谢谢您的帮助。

编辑:

与此相关的错误导致此刻为空资源的触发。我也尝试了几种不同的变体:

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 个答案:

答案 0 :(得分:0)

您可以为此使用null_resources并在阶段中传递动作列表。像这样

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"

        ...

    }
}