-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcodepipeline.tf
189 lines (159 loc) · 4.8 KB
/
codepipeline.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
resource "aws_codepipeline" "this" {
name = var.pipeline_name
pipeline_type = "V2"
role_arn = aws_iam_role.codepipeline_role.arn
execution_mode = "QUEUED"
artifact_store {
location = aws_s3_bucket.this.id
type = "S3"
}
stage {
name = "Source"
action {
name = "Source"
category = "Source"
owner = "AWS"
provider = var.connection == null ? "CodeCommit" : "CodeStarSourceConnection"
version = "1"
output_artifacts = ["source_output"]
configuration = {
RepositoryName = var.connection == null ? var.repo : null
FullRepositoryId = var.connection == null ? null : var.repo
ConnectionArn = var.connection
BranchName = var.branch
PollForSourceChanges = var.connection == null ? false : null
DetectChanges = var.connection == null ? null : var.detect_changes
}
}
}
stage {
name = "Validation"
dynamic "action" {
for_each = local.validation_stages
content {
name = action.key
category = "Test"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["source_output"]
version = "1"
configuration = {
ProjectName = module.validation[action.key].codebuild_project.name
}
}
}
}
stage {
name = "Plan"
action {
name = "Plan"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["source_output"]
version = "1"
configuration = {
ProjectName = module.plan.codebuild_project.name
}
}
}
stage {
name = "Approve"
action {
name = "Approval"
category = "Approval"
owner = "AWS"
provider = "Manual"
version = "1"
configuration = {
CustomData = "This action will approve the deployment of resources in ${var.pipeline_name}. Please ensure that you review the build logs of the plan stage before approving."
ExternalEntityLink = "https://${data.aws_region.current.name}.console.aws.amazon.com/codesuite/codebuild/${data.aws_caller_identity.current.account_id}/projects/${var.pipeline_name}-plan/"
}
}
}
stage {
name = "Apply"
action {
name = "Apply"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["source_output"]
version = "1"
configuration = {
ProjectName = module.apply.codebuild_project.name
}
}
}
}
resource "aws_iam_role" "codepipeline_role" {
name = "${var.pipeline_name}-role"
assume_role_policy = data.aws_iam_policy_document.codepipeline-assume-role.json
}
data "aws_iam_policy_document" "codepipeline-assume-role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["codepipeline.amazonaws.com"]
}
condition {
test = "StringLike"
variable = "aws:SourceArn"
values = [
"arn:aws:codepipeline:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${var.pipeline_name}"
]
}
}
}
resource "aws_iam_role_policy_attachment" "codepipeline" {
role = aws_iam_role.codepipeline_role.name
policy_arn = aws_iam_policy.codepipeline.arn
}
resource "aws_iam_policy" "codepipeline" {
name = "${var.pipeline_name}-policy"
policy = data.aws_iam_policy_document.codepipeline.json
}
data "aws_iam_policy_document" "codepipeline" {
statement {
effect = "Allow"
actions = [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:GetBucketVersioning",
"s3:PutObjectAcl",
"s3:PutObject"
]
resources = [
"${aws_s3_bucket.this.arn}",
"${aws_s3_bucket.this.arn}/*"
]
}
statement {
effect = "Allow"
actions = [
"codebuild:BatchGetBuilds",
"codebuild:StartBuild"
]
resources = [
"arn:aws:codebuild:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:project/${var.pipeline_name}-*"
]
}
statement {
effect = "Allow"
actions = [
"codecommit:GetBranch",
"codecommit:GetCommit",
"codecommit:UploadArchive",
"codecommit:GetUploadArchiveStatus",
"codecommit:CancelUploadArchive",
"codestar-connections:UseConnection"
]
resources = [
var.connection == null ? "arn:aws:codecommit:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${var.repo}" : var.connection
]
}
}