地形与AWS CodePipeline - 动态定义阶段上的操作 [英] Terraform & AWS CodePipeline - Dynamically define actions on a stage
问题描述
我不确定这是否可能,但我想我会要求先检查一下.
I'm not sure if this is possible but thought I'd ask to check first.
我在 terraform
中定义了一个 CodePipeline
,其中一个 Stage
具有多个操作.
I have a CodePipeline
defined in terraform
, with a single Stage
that has multiple actions.
Stage
正在从 CodeCommit
中提取代码,每个操作都定义了我要在 Pipeline
中使用的存储库.
The Stage
is pulling code from CodeCommit
, and each action defines what repositories I want to use in the Pipeline
.
我想创建一个包含 CodeCommit
存储库名称列表的变量,然后为该列表中的每个存储库动态创建一个操作.
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.
terraform
有可能吗?我知道您可以使用 count
来正常实现这一点,但我认为这只是在资源级别?
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?
代码片段如下:
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"
...
}
}
}
感谢您的帮助.
此时将 this 作为 null 资源的触发器出现错误.我也尝试了一些不同的变体:
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))}"
}
}
}
}
推荐答案
您可以为此使用 null_resources 并传递阶段中的操作列表.像这样
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"
...
}
}
这篇关于地形与AWS CodePipeline - 动态定义阶段上的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!