Skip to content

Commit ea1e8d2

Browse files
authored
Deployment type (#28)
Added optional variable to run pipeline sequentially.
1 parent 7069a40 commit ea1e8d2

File tree

3 files changed

+139
-34
lines changed

3 files changed

+139
-34
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ AWS CodeCommit:
3737
```hcl
3838
module "pipeline" {
3939
source = "aws-samples/multi-account-pipeline/aws"
40-
version = "1.5.x"
40+
version = "1.6.x"
4141
pipeline_name = "pipeline"
4242
repo = aws_repository.this.repository_name
4343
accounts = {
@@ -51,7 +51,7 @@ Third-party service:
5151
```hcl
5252
module "pipeline" {
5353
source = "aws-samples/multi-account-pipeline/aws"
54-
version = "1.5.x"
54+
version = "1.6.x"
5555
pipeline_name = "pipeline"
5656
repo = "organization/repo"
5757
connection = aws_codestarconnections_connection.this.arn
@@ -77,6 +77,7 @@ module "pipeline" {
7777
module "pipeline" {
7878
...
7979
branch = "main"
80+
deployment_type = "parallel"
8081
mode = "SUPERSEDED"
8182
detect_changes = false
8283
kms_key = aws_kms_key.this.arn

codepipeline.tf

Lines changed: 126 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,110 @@ resource "aws_codepipeline" "this" {
4949
}
5050
}
5151
}
52+
dynamic "stage" {
53+
for_each = var.deployment_type == "sequential" ? [] : ["plan"]
54+
content {
55+
name = "Plan"
56+
dynamic "action" {
57+
for_each = var.accounts
58+
content {
59+
name = action.key
60+
category = "Build"
61+
owner = "AWS"
62+
provider = "CodeBuild"
63+
input_artifacts = ["source_output"]
64+
version = "1"
65+
run_order = 1
66+
configuration = {
67+
ProjectName = module.plan.codebuild_project.name
68+
EnvironmentVariables = jsonencode([
69+
{
70+
name = "WORKSPACE"
71+
value = action.value
72+
type = "PLAINTEXT"
73+
},
74+
{
75+
name = "ACCOUNT_NAME"
76+
value = action.key
77+
type = "PLAINTEXT"
78+
},
79+
{
80+
name = "TF_VAR_account_id"
81+
value = action.value
82+
type = "PLAINTEXT"
83+
},
84+
{
85+
name = "TF_VAR_account_name"
86+
value = action.key
87+
type = "PLAINTEXT"
88+
}])
89+
}
90+
}
91+
}
92+
action {
93+
name = "Approval"
94+
category = "Approval"
95+
owner = "AWS"
96+
provider = "Manual"
97+
version = "1"
98+
run_order = 2
99+
configuration = {
100+
CustomData = "This action will approve the deployment of resources in ${var.pipeline_name}. Please review the plan action before approving."
101+
}
102+
}
103+
}
104+
}
52105

53-
stage {
54-
name = "Plan"
55-
dynamic "action" {
56-
for_each = var.accounts
57-
content {
58-
name = action.key
106+
dynamic "stage" {
107+
for_each = var.deployment_type == "sequential" ? [] : ["apply"]
108+
content {
109+
name = "Apply"
110+
dynamic "action" {
111+
for_each = var.accounts
112+
content {
113+
name = action.key
114+
category = "Build"
115+
owner = "AWS"
116+
provider = "CodeBuild"
117+
input_artifacts = ["source_output"]
118+
version = "1"
119+
run_order = 1
120+
configuration = {
121+
ProjectName = module.apply.codebuild_project.name
122+
EnvironmentVariables = jsonencode([
123+
{
124+
name = "WORKSPACE"
125+
value = action.value
126+
type = "PLAINTEXT"
127+
},
128+
{
129+
name = "ACCOUNT_NAME"
130+
value = action.key
131+
type = "PLAINTEXT"
132+
},
133+
{
134+
name = "TF_VAR_account_id"
135+
value = action.value
136+
type = "PLAINTEXT"
137+
},
138+
{
139+
name = "TF_VAR_account_name"
140+
value = action.key
141+
type = "PLAINTEXT"
142+
}])
143+
}
144+
}
145+
}
146+
}
147+
}
148+
149+
dynamic "stage" {
150+
for_each = var.deployment_type == "sequential" ? var.accounts : {}
151+
content {
152+
name = stage.key
153+
154+
action {
155+
name = "Plan"
59156
category = "Build"
60157
owner = "AWS"
61158
provider = "CodeBuild"
@@ -67,78 +164,75 @@ resource "aws_codepipeline" "this" {
67164
EnvironmentVariables = jsonencode([
68165
{
69166
name = "WORKSPACE"
70-
value = action.value
167+
value = stage.value
71168
type = "PLAINTEXT"
72169
},
73170
{
74171
name = "ACCOUNT_NAME"
75-
value = action.key
172+
value = stage.key
76173
type = "PLAINTEXT"
77174
},
78175
{
79176
name = "TF_VAR_account_id"
80-
value = action.value
177+
value = stage.value
81178
type = "PLAINTEXT"
82179
},
83180
{
84181
name = "TF_VAR_account_name"
85-
value = action.key
182+
value = stage.key
86183
type = "PLAINTEXT"
87184
}])
88185
}
89186
}
90-
}
91-
action {
92-
name = "Approval"
93-
category = "Approval"
94-
owner = "AWS"
95-
provider = "Manual"
96-
version = "1"
97-
run_order = 2
98-
configuration = {
99-
CustomData = "This action will approve the deployment of resources in ${var.pipeline_name}. Please review the plan action before approving."
187+
188+
action {
189+
name = "Approval"
190+
category = "Approval"
191+
owner = "AWS"
192+
provider = "Manual"
193+
version = "1"
194+
run_order = 2
195+
configuration = {
196+
CustomData = "This action will approve the deployment of resources in ${var.pipeline_name}. Please review the plan action before approving."
197+
}
100198
}
101-
}
102-
}
103199

104-
stage {
105-
name = "Apply"
106-
dynamic "action" {
107-
for_each = var.accounts
108-
content {
109-
name = action.key
200+
action {
201+
name = "Apply"
110202
category = "Build"
111203
owner = "AWS"
112204
provider = "CodeBuild"
113205
input_artifacts = ["source_output"]
114206
version = "1"
207+
run_order = 3
115208
configuration = {
116209
ProjectName = module.apply.codebuild_project.name
117210
EnvironmentVariables = jsonencode([
118211
{
119212
name = "WORKSPACE"
120-
value = action.value
213+
value = stage.value
121214
type = "PLAINTEXT"
122215
},
123216
{
124217
name = "ACCOUNT_NAME"
125-
value = action.key
218+
value = stage.key
126219
type = "PLAINTEXT"
127220
},
128221
{
129222
name = "TF_VAR_account_id"
130-
value = action.value
223+
value = stage.value
131224
type = "PLAINTEXT"
132225
},
133226
{
134227
name = "TF_VAR_account_name"
135-
value = action.key
228+
value = stage.key
136229
type = "PLAINTEXT"
137230
}])
138231
}
139232
}
140233
}
141234
}
235+
142236
}
143237

144238
resource "aws_iam_role" "codepipeline" {

variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ 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+
7787
variable "detect_changes" {
7888
description = "allows third-party servicesm like GitHub to invoke the pipeline"
7989
type = bool

0 commit comments

Comments
 (0)