Skip to content

Instantly share code, notes, and snippets.

@KerberosMorphy
Created May 26, 2020 16:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KerberosMorphy/189990985cdc9d6e304a44fcd17813bf to your computer and use it in GitHub Desktop.
Save KerberosMorphy/189990985cdc9d6e304a44fcd17813bf to your computer and use it in GitHub Desktop.
Terraform CodePipeline Dynamic Stages and Actions
${jsonencode([
{
"name": "Build",
"category": "Build",
"owner": "AWS",
"provider": "CodeBuild",
"input_artifacts": ["SourceArtifact"],
"output_artifacts": ["BuildArtifact"],
"version": "1",
"configuration": {
"keys": ["ProjectName"],
"values": ["${client}-${stage}-build"]
}
}
])}
locals {
build_actions = fileexists("./build_actions.tmpl") ? jsondecode(templatefile("./build_actions.tmpl", { client = var.client, stage = var.stage })) : null
}
resource "aws_codepipeline" "codepipeline" {
name = "${var.client}-${var.stage}-codepipeline"
role_arn = aws_iam_role.codepipeline_role.arn
dynamic "stage" {
for_each = fileexists("./build_actions.json") ? [""] : []
content {
name = "Build"
dynamic "action" {
for_each = local.build_actions
content {
name = action.value.name
category = action.value.category
owner = action.value.owner
provider = action.value.provider
input_artifacts = contains(keys(action.value), "input_artifacts") ? action.value.input_artifacts : null
output_artifacts = contains(keys(action.value), "output_artifacts") ? action.value.output_artifacts : null
version = action.value.version
configuration = zipmap(
action.value.configuration.keys,
action.value.configuration.values
)
}
}
}
}
}
variable "stage" {
description = "Environment Stage Name i.e.: ['dev', 'staging', 'prod']"
type = string
default = "dev"
}
variable "client" {
description = "Client identifier"
type = string
default = "xyz"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment