-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.tf
248 lines (201 loc) · 6.86 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
locals {
resource_name = "${var.environment}-${var.resource_tag_name}"
tags = {
Environment = var.environment
Name = var.resource_tag_name
}
}
# -----------------------------------------------------------------------------
# Resources: Random string
# -----------------------------------------------------------------------------
resource "random_string" "postfix" {
count = var.codepipeline_sam_module_enabled ? 1 : 0
length = 6
number = false
upper = false
special = false
lower = true
}
# -----------------------------------------------------------------------------
# Resources: CodePipeline
# -----------------------------------------------------------------------------
resource "aws_s3_bucket" "artifact_store" {
count = var.codepipeline_sam_module_enabled ? 1 : 0
bucket = "${local.resource_name}-codepipeline-artifacts-${one(random_string.postfix.*.result)}"
acl = "private"
force_destroy = true
lifecycle_rule {
enabled = true
expiration {
days = 5
}
}
}
module "iam_codepipeline" {
source = "github.com/rpstreef/tf-iam?ref=v1.2"
environment = var.environment
region = var.region
resource_tag_name = var.resource_tag_name
iam_module_enabled = var.codepipeline_sam_module_enabled
assume_role_policy = file("${path.module}/policies/codepipeline-assume-role.json")
template = file("${path.module}/policies/codepipeline-policy.json")
role_name = "codepipeline-role"
policy_name = "codepipeline-policy"
role_vars = {
codebuild_project_arn = try(one(aws_codebuild_project._.*.arn), "")
s3_bucket_arn = try(one(aws_s3_bucket.artifact_store.*.arn), "")
}
}
module "iam_cloudformation" {
source = "github.com/rpstreef/tf-iam?ref=v1.2"
environment = var.environment
region = var.region
resource_tag_name = var.resource_tag_name
iam_module_enabled = var.codepipeline_sam_module_enabled
assume_role_policy = file("${path.module}/policies/cloudformation-assume-role.json")
template = file("${path.module}/policies/cloudformation-policy.json")
role_name = "cloudformation-role"
policy_name = "cloudformation-policy"
role_vars = {
s3_bucket_arn = try(one(aws_s3_bucket.artifact_store.*.arn), "")
codepipeline_role_arn = try(module.iam_codepipeline.role_arn, "")
}
}
resource "aws_codepipeline" "_" {
count = var.codepipeline_sam_module_enabled ? 1 : 0
name = "${local.resource_name}-codepipeline"
role_arn = try(module.iam_codepipeline.role_arn, "")
artifact_store {
location = one(aws_s3_bucket.artifact_store.*.bucket)
type = "S3"
}
stage {
name = "Source"
action {
name = "Source"
category = "Source"
owner = "ThirdParty"
provider = "GitHub"
version = "1"
output_artifacts = ["source"]
configuration = {
OAuthToken = var.github_token
Owner = var.github_owner
Repo = var.github_repo
Branch = var.github_branch
PollForSourceChanges = var.poll_source_changes
}
}
}
stage {
name = "Build"
action {
name = "Build"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
version = "1"
input_artifacts = ["source"]
output_artifacts = ["build"]
configuration = {
ProjectName = one(aws_codebuild_project._.*.name)
}
}
}
stage {
name = "Deploy"
action {
name = "CreateChangeSet"
category = "Deploy"
owner = "AWS"
provider = "CloudFormation"
input_artifacts = ["build"]
role_arn = try(module.iam_cloudformation.role_arn, "")
version = 1
run_order = 1
configuration = {
ActionMode = "CHANGE_SET_REPLACE"
Capabilities = "CAPABILITY_IAM,CAPABILITY_AUTO_EXPAND"
OutputFileName = "ChangeSetOutput.json"
RoleArn = try(module.iam_cloudformation.role_arn, "")
StackName = var.stack_name
TemplatePath = "build::packaged.yaml"
ChangeSetName = "${var.stack_name}-deploy"
TemplateConfiguration = "build::configuration.json"
}
}
action {
name = "Deploy"
category = "Deploy"
owner = "AWS"
provider = "CloudFormation"
input_artifacts = ["build"]
version = 1
run_order = 2
configuration = {
ActionMode = "CHANGE_SET_EXECUTE"
Capabilities = "CAPABILITY_IAM,CAPABILITY_AUTO_EXPAND"
OutputFileName = "ChangeSetExecuteOutput.json"
StackName = var.stack_name
ChangeSetName = "${var.stack_name}-deploy"
}
}
}
tags = local.tags
lifecycle {
ignore_changes = [stage[0].action[0].configuration]
}
}
# -----------------------------------------------------------------------------
# Resources: CodeBuild
# -----------------------------------------------------------------------------
module "iam_codebuild" {
source = "github.com/rpstreef/tf-iam?ref=v1.2"
environment = var.environment
region = var.region
resource_tag_name = var.resource_tag_name
iam_module_enabled = var.codepipeline_sam_module_enabled
assume_role_policy = file("${path.module}/policies/codebuild-assume-role.json")
template = file("${path.module}/policies/codebuild-policy.json")
role_name = "codebuild-role"
policy_name = "codebuild-policy"
role_vars = {
s3_bucket_arn = try(one(aws_s3_bucket.artifact_store.*.arn), "")
}
}
resource "aws_codebuild_project" "_" {
count = var.codepipeline_sam_module_enabled ? 1 : 0
name = "${local.resource_name}-codebuild"
description = "${local.resource_name}_codebuild_project"
build_timeout = var.build_timeout
badge_enabled = var.badge_enabled
service_role = try(module.iam_codebuild.role_arn, "")
artifacts {
type = "CODEPIPELINE"
namespace_type = "BUILD_ID"
packaging = "ZIP"
}
environment {
compute_type = var.build_compute_type
image = var.build_image
type = "LINUX_CONTAINER"
privileged_mode = var.privileged_mode
environment_variable {
name = "ARTIFACT_BUCKET"
value = one(aws_s3_bucket.artifact_store.*.bucket)
}
dynamic "environment_variable" {
for_each = var.environment_variable_map
content {
name = environment_variable.value.name
value = environment_variable.value.value
type = environment_variable.value.type
}
}
}
source {
type = "CODEPIPELINE"
buildspec = var.buildspec
}
tags = local.tags
}