Skip to content

Commit f49f24a

Browse files
authored
optional sequential pipeline
optional input (`var.sequential`) for sequential pipeline
1 parent ea1e8d2 commit f49f24a

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ module "pipeline" {
6060
"workload2" = "223344556677"
6161
"workload3" = "334455667788"
6262
}
63+
# Optional: For sequential deployment in specific order
64+
# sequential = ["workload1", "workload2", "workload3"]
6365
}
6466
```
6567

@@ -69,15 +71,31 @@ module "pipeline" {
6971

7072
`accounts` is a map of the target AWS accounts.
7173

72-
`connection` is the connection arn of the [connection](https://docs.aws.amazon.com/dtconsole/latest/userguide/welcome-connections.html) to the third-party repo.
74+
`connection` is the connection arn of the [connection](https://docs.aws.amazon.com/dtconsole/latest/userguide/welcome-connections.html) to the third-party repo.
75+
76+
### Sequential Deployment
77+
By default, the pipeline deploys terraform to AWS accounts in parallel. To deploy terraform sequentially (Eg dev -> test -> prod), use the `sequential` input:
78+
79+
```hcl
80+
module "pipeline" {
81+
...
82+
accounts = {
83+
"dev" = "112233445566"
84+
"test" = "223344556677"
85+
"prod" = "334455667788"
86+
}
87+
sequential = ["dev", "test", "prod"]
88+
}
89+
```
90+
91+
`sequential` is an ordered list of the AWS accounts in `accounts`.
7392

7493
### Optional Inputs
7594

7695
```hcl
7796
module "pipeline" {
7897
...
7998
branch = "main"
80-
deployment_type = "parallel"
8199
mode = "SUPERSEDED"
82100
detect_changes = false
83101
kms_key = aws_kms_key.this.arn

codepipeline.tf

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ resource "aws_codepipeline" "this" {
4949
}
5050
}
5151
}
52+
53+
// parallel
5254
dynamic "stage" {
53-
for_each = var.deployment_type == "sequential" ? [] : ["plan"]
55+
for_each = length(var.sequential) == 0 ? ["plan"] : []
5456
content {
5557
name = "Plan"
5658
dynamic "action" {
@@ -102,9 +104,8 @@ resource "aws_codepipeline" "this" {
102104
}
103105
}
104106
}
105-
106107
dynamic "stage" {
107-
for_each = var.deployment_type == "sequential" ? [] : ["apply"]
108+
for_each = length(var.sequential) == 0 ? ["apply"] : []
108109
content {
109110
name = "Apply"
110111
dynamic "action" {
@@ -146,10 +147,11 @@ resource "aws_codepipeline" "this" {
146147
}
147148
}
148149

150+
// sequential
149151
dynamic "stage" {
150-
for_each = var.deployment_type == "sequential" ? var.accounts : {}
152+
for_each = local.ordered_accounts
151153
content {
152-
name = stage.key
154+
name = stage.value.name
153155

154156
action {
155157
name = "Plan"
@@ -164,22 +166,22 @@ resource "aws_codepipeline" "this" {
164166
EnvironmentVariables = jsonencode([
165167
{
166168
name = "WORKSPACE"
167-
value = stage.value
169+
value = stage.value.account_id
168170
type = "PLAINTEXT"
169171
},
170172
{
171173
name = "ACCOUNT_NAME"
172-
value = stage.key
174+
value = stage.value.name
173175
type = "PLAINTEXT"
174176
},
175177
{
176178
name = "TF_VAR_account_id"
177-
value = stage.value
179+
value = stage.value.account_id
178180
type = "PLAINTEXT"
179181
},
180182
{
181183
name = "TF_VAR_account_name"
182-
value = stage.key
184+
value = stage.value.name
183185
type = "PLAINTEXT"
184186
}])
185187
}
@@ -210,22 +212,22 @@ resource "aws_codepipeline" "this" {
210212
EnvironmentVariables = jsonencode([
211213
{
212214
name = "WORKSPACE"
213-
value = stage.value
215+
value = stage.value.account_id
214216
type = "PLAINTEXT"
215217
},
216218
{
217219
name = "ACCOUNT_NAME"
218-
value = stage.key
220+
value = stage.value.name
219221
type = "PLAINTEXT"
220222
},
221223
{
222224
name = "TF_VAR_account_id"
223-
value = stage.value
225+
value = stage.value.account_id
224226
type = "PLAINTEXT"
225227
},
226228
{
227229
name = "TF_VAR_account_name"
228-
value = stage.key
230+
value = stage.value.name
229231
type = "PLAINTEXT"
230232
}])
231233
}

locals.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ locals {
1212
tags = "aws/codebuild/amazonlinux2-x86_64-standard:5.0"
1313
})
1414

15+
ordered_accounts = length(var.sequential) > 0 ? [
16+
for name in var.sequential : {
17+
name = name
18+
account_id = var.accounts[name]
19+
}
20+
] : []
21+
1522
env_var = {
1623
CHECKOV_SKIPS = join(",", "${var.checkov_skip}")
1724
CHECKOV_VERSION = var.checkov_version

variables.tf

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ variable "codebuild_policy" {
7474
default = null
7575
}
7676

77-
variable "deployment_type" {
78-
description = "deployment type, parallel or sequential"
79-
type = string
80-
default = "parallel"
81-
validation {
82-
condition = contains(["parallel", "sequential"], var.deployment_type)
83-
error_message = "The pipeline mode must be 'parallel' or 'sequential'"
84-
}
85-
}
86-
8777
variable "detect_changes" {
8878
description = "allows third-party servicesm like GitHub to invoke the pipeline"
8979
type = bool
@@ -126,6 +116,12 @@ variable "notifications" {
126116
default = null
127117
}
128118

119+
variable "sequential" {
120+
description = "list of account names in sequential deployment order"
121+
type = list(string)
122+
default = []
123+
}
124+
129125
variable "tags" {
130126
description = "tags to check for"
131127
type = string

0 commit comments

Comments
 (0)