Skip to content

Commit c95af6a

Browse files
committed
use sequential var to order accounts
1 parent ea1e8d2 commit c95af6a

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ resource "aws_codepipeline" "this" {
5050
}
5151
}
5252
dynamic "stage" {
53-
for_each = var.deployment_type == "sequential" ? [] : ["plan"]
53+
for_each = local.is_sequential ? [] : ["plan"]
5454
content {
5555
name = "Plan"
5656
dynamic "action" {
57-
for_each = var.accounts
57+
for_each = local.ordered_accounts
5858
content {
5959
name = action.key
6060
category = "Build"
@@ -104,11 +104,11 @@ resource "aws_codepipeline" "this" {
104104
}
105105

106106
dynamic "stage" {
107-
for_each = var.deployment_type == "sequential" ? [] : ["apply"]
107+
for_each = local.is_sequential ? [] : ["apply"]
108108
content {
109109
name = "Apply"
110110
dynamic "action" {
111-
for_each = var.accounts
111+
for_each = local.ordered_accounts
112112
content {
113113
name = action.key
114114
category = "Build"
@@ -147,7 +147,7 @@ resource "aws_codepipeline" "this" {
147147
}
148148

149149
dynamic "stage" {
150-
for_each = var.deployment_type == "sequential" ? var.accounts : {}
150+
for_each = local.is_sequential ? local.ordered_accounts : {}
151151
content {
152152
name = stage.key
153153

locals.tf

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

15+
is_sequential = var.sequential != []
16+
ordered_accounts = local.is_sequential ? {
17+
for name in var.sequential : name => var.accounts[name]
18+
} : var.accounts
19+
1520
env_var = {
1621
CHECKOV_SKIPS = join(",", "${var.checkov_skip}")
1722
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)