-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
374 lines (325 loc) · 12.7 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# -----------------------------------------------------------------------------
# DEPLOY A CI/CD PIPELINE USING AWS CODESUITE SERVICES.
# This Terraform module deploys the resources necessary to host a CI/CD
# pipeline on AWS. It includes the following:
# * Source control via AWS CodeCommit
# * Pipeline execution on updates to master
# * Custom build and deploy Docker images
# * Multi-account deployments
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# REQUIRE A SPECIFIC TERRAFORM VERSION OR HIGHER
# This module has been updated with 0.12 syntax, which means it is no longer
# compatible with any versions below 0.12.
# -----------------------------------------------------------------------------
terraform {
required_version = ">= 0.12"
}
data "aws_caller_identity" "current" {}
# -----------------------------------------------------------------------------
# CODECOMMIT REPOSITORY
# This CodeCommit repository contains the code for the project.
# -----------------------------------------------------------------------------
resource "aws_codecommit_repository" "codecommit_repository" {
repository_name = var.project_name
default_branch = "master"
}
# -----------------------------------------------------------------------------
# ECR REPOSITORY (BUILD)
# This ECR repository contains the Docker images for the 'Build' stage of the
# CodePipeline pipeline. Docker images pushed to this repository are built
# during the 'Docker' stage of the CodePipeline pipeline.
# -----------------------------------------------------------------------------
resource "aws_ecr_repository" "build_ecr_repository" {
name = "${var.project_name}/build"
image_tag_mutability = "MUTABLE"
}
# -----------------------------------------------------------------------------
# ECR REPOSITORY (DEPLOY)
# This ECR repository contains the Docker images for the 'Deploy' stage of the
# CodePipeline pipeline. Docker images pushed to this repository are built
# during the 'Docker' stage of the CodePipeline pipeline.
# -----------------------------------------------------------------------------
resource "aws_ecr_repository" "deploy_ecr_repository" {
name = "${var.project_name}/deploy"
image_tag_mutability = "MUTABLE"
}
# -----------------------------------------------------------------------------
# CLOUDWATCH EVENTS RULE
# This CloudWatch Events rule will trigger the CodePipeline pipeline to execute
# when the event pattern is matched to an event. In this case, the pipeline is
# triggered when the master branch of the CodeCommit repository is created or
# updated.
# -----------------------------------------------------------------------------
resource "aws_cloudwatch_event_rule" "cloudwatch_events_rule" {
name = "${var.project_name}-cloudwatch-events-rule"
event_pattern = <<PATTERN
{
"source": ["aws.codecommit"],
"detail-type": ["CodeCommit Repository State Change"],
"resources": ["${aws_codecommit_repository.codecommit_repository.arn}"],
"detail": {
"event": ["referenceCreated", "referenceUpdated"],
"referenceType": ["branch"],
"referenceName": ["master"]
}
}
PATTERN
}
# -----------------------------------------------------------------------------
# CLOUDWATCH EVENTS TARGET
# This CloudWatch Events target is associated with the above CloudWatch Events
# rule.
# -----------------------------------------------------------------------------
resource "aws_cloudwatch_event_target" "cloudwatch_event_target" {
target_id = "${var.project_name}-cloudwatch-events-rule-target"
rule = aws_cloudwatch_event_rule.cloudwatch_events_rule.name
arn = aws_codepipeline.codepipeline_pipeline.arn
role_arn = var.cloudwatch_role_arn
}
# -----------------------------------------------------------------------------
# S3 BUCKET (CODEPIPELINE)
# This S3 bucket contains the stored artifacts for the CodePipeline pipeline.
# NOTE: The bucket name must contain only lowercase letters, numbers, periods
# (.), and dashes (-).
# -----------------------------------------------------------------------------
resource "aws_s3_bucket" "artifact_store_s3_bucket" {
bucket = "${var.project_name}-s3-artifact-store"
}
# -----------------------------------------------------------------------------
# CODEPIPELINE
# This CodePipeline pipline defines 4 stage types:
# * Source
# * Docker
# * Build
# * Deploy
#
# SOURCE
# The Source stage simply pulls in the Git repository from CodeCommit.
#
# DOCKER
# The Docker stage builds the Docker images that are to be used for the
# subsequent Build and Deploy stages.
#
# BUILD
# The Build stage tests and builds the contents of the CodeCommit repository.
# This stage may also produce a build artifact that may be used in the Deploy
# stage.
#
# DEPLOY
# The Deploy stage (or stages) deploys the contents of the CodeCommit
# repository or the build artifact to one or more AWS environments. The AWS
# environments (AWS accounts) to which to deploy are specified by the
# `destination_account_ids` variable. A Deploy stage will be created for each
# account.
# -----------------------------------------------------------------------------
resource "aws_codepipeline" "codepipeline_pipeline" {
name = "${var.project_name}-codepipeline-pipeline"
role_arn = var.codepipeline_role_arn
artifact_store {
location = aws_s3_bucket.artifact_store_s3_bucket.bucket
type = "S3"
}
stage {
name = "Source"
action {
name = "Source"
category = "Source"
owner = "AWS"
provider = "CodeCommit"
output_artifacts = ["SOURCE"]
version = "1"
configuration = {
BranchName = "master"
PollForSourceChanges = false
RepositoryName = aws_codecommit_repository.codecommit_repository.repository_name
}
}
}
stage {
name = "Docker"
action {
name = "Docker"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["SOURCE"]
version = "1"
configuration = {
ProjectName = aws_codebuild_project.docker_codebuild_project.name
}
}
}
stage {
name = "Build"
action {
name = "Build"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["SOURCE"]
output_artifacts = ["BUILD_ARTIFACT"]
version = "1"
configuration = {
ProjectName = aws_codebuild_project.build_codebuild_project.name
}
}
}
dynamic "stage" {
for_each = var.destination_account_ids
content {
# If destination_account_ids = {"current" : ""}, a deployment is only to
# be made to the current account.
name = stage.key == "current" ? "Deploy" : "Deploy-${title(stage.key)}"
action {
name = "Deploy"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["SOURCE", "BUILD_ARTIFACT"]
version = "1"
configuration = {
# WARNING: The cleanest way to associate this dynamic block with the
# correct CodeBuild project is by name. If the CodeBuild project
# naming convention changes, this will need to be updated.
ProjectName = stage.key == "current" ? "${var.project_name}-deploy" : "${var.project_name}-deploy-${lower(stage.key)}"
PrimarySource = "SOURCE"
}
}
}
}
}
# -----------------------------------------------------------------------------
# CODEBUILD (DOCKER)
# This CodeBuild project builds the Docker images that are to be used for the
# subsequent Build and Deploy stages of the CodePipeline pipeline.
# -----------------------------------------------------------------------------
resource "aws_codebuild_project" "docker_codebuild_project" {
name = "${var.project_name}-docker"
service_role = var.codebuild_role_arn
artifacts {
type = "CODEPIPELINE"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/standard:1.0"
image_pull_credentials_type = "CODEBUILD"
privileged_mode = true
type = "LINUX_CONTAINER"
environment_variable {
name = "AWS_ACCOUNT_ID"
value = data.aws_caller_identity.current.account_id
}
environment_variable {
name = "AWS_REGION"
value = var.aws_region
}
environment_variable {
name = "BUILD_IMAGE_REPOSITORY"
value = "${var.project_name}/build"
}
environment_variable {
name = "DEPLOY_IMAGE_REPOSITORY"
value = "${var.project_name}/deploy"
}
}
# TODO: This source causes repeated updates.
# source {
# buildspec = var.docker_build_spec
# git_clone_depth = 1
# type = "CODEPIPELINE"
# }
source {
buildspec = var.docker_build_spec
git_clone_depth = 0
insecure_ssl = false
report_build_status = false
type = "CODEPIPELINE"
}
}
# -----------------------------------------------------------------------------
# CODEBUILD (BUILD)
# This CodeBuild project tests and builds the contents of the CodeCommit
# repository. This project may also produce a build artifact that may be used
# in the Deploy stage.
# -----------------------------------------------------------------------------
resource "aws_codebuild_project" "build_codebuild_project" {
name = "${var.project_name}-build"
service_role = var.codebuild_role_arn
artifacts {
type = "CODEPIPELINE"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "${aws_ecr_repository.build_ecr_repository.repository_url}:latest"
image_pull_credentials_type = "SERVICE_ROLE"
type = "LINUX_CONTAINER"
}
source {
buildspec = var.build_build_spec
git_clone_depth = 0
insecure_ssl = false
report_build_status = false
type = "CODEPIPELINE"
}
}
# -----------------------------------------------------------------------------
# CODEBUILD (DEPLOY)
# This CodeBuild project deploys the contents of the CodeCommit repository or
# the build artifact produced by the Build stage. This project has the ability
# to deploy across AWS accounts using the `DESTINATION_CODEBUILD_ROLE_ARN`
# environment variable.
#
# An example of a cross-account deployment can be found in the
# `examples/multi-account` directory. Specifically, the `assume-role` script
# demostates how to assume a cross-account role.
#
# If `DESTINATION_CODEBUILD_ROLE_ARN` is empty (""), the deployment is
# executed in the current account.
#
# The following is an example of the `destination_account_ids` variable:
#
# {
# "operations": "<account-id>",
# "sandbox": "<account-id>",
# "production": "<account-id>"
# }
# -----------------------------------------------------------------------------
resource "aws_codebuild_project" "deploy_codebuild_project" {
for_each = var.destination_account_ids
# If a key in `destination_account_ids` is equal to "current", the deployment
# will be made to the current account.
#
# WARNING: The cleanest way to associate the correct CodeBuild project with
# the dynamic 'stage' block of the CodePipeline resource is by name. If the
# CodeBuild project naming convention changes, the dynamic 'stage' block will
# need to be updated.
name = each.key == "current" ? "${var.project_name}-deploy" : "${var.project_name}-deploy-${lower(each.key)}"
service_role = var.codebuild_role_arn
artifacts {
type = "CODEPIPELINE"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "${aws_ecr_repository.deploy_ecr_repository.repository_url}:latest"
image_pull_credentials_type = "SERVICE_ROLE"
type = "LINUX_CONTAINER"
environment_variable {
name = "PROFILE"
value = lower(each.key)
}
# If a value in `destination_account_ids` is an empty string (""), the
# deployment will be made to the current account.
environment_variable {
name = "DESTINATION_CODEBUILD_ROLE_ARN"
value = "arn:aws:iam::${each.value == "" ? data.aws_caller_identity.current.account_id : each.value}:role/${var.destination_codebuild_role_name}"
}
}
source {
buildspec = var.deploy_build_spec
git_clone_depth = 0
insecure_ssl = false
report_build_status = false
type = "CODEPIPELINE"
}
}