-
Notifications
You must be signed in to change notification settings - Fork 1
/
apply-pipeline.tf
208 lines (175 loc) · 5.47 KB
/
apply-pipeline.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
resource "aws_codepipeline" "apply-pipeline" {
name = "${var.namespace}-apply-pipeline"
role_arn = aws_iam_role.codepipeline_role.arn
tags = var.global_tags
artifact_store {
location = aws_s3_bucket.codepipeline_bucket.bucket
type = "S3"
}
stage {
name = "Source"
action {
name = "Source"
category = "Source"
owner = "AWS"
provider = "CodeStarSourceConnection"
version = "1"
output_artifacts = ["source_output"]
configuration = {
ConnectionArn = data.aws_codestarconnections_connection.github.id
FullRepositoryId = var.github_repo
BranchName = var.git_branch
DetectChanges = var.github_webhook_enabled
}
}
}
stage {
name = "Plan"
action {
name = "Plan"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["source_output"]
output_artifacts = ["plan_output"]
version = "1"
configuration = {
ProjectName = local.build_project_name_plan
}
}
}
stage {
name = "Approval"
action {
name = "Approval"
category = "Approval"
owner = "AWS"
provider = "Manual"
version = "1"
configuration = {
# CustomData = "CustomData"
# ExternalEntityLink = "http://www.example.com"
NotificationArn = aws_sns_topic.notify-topic.arn
}
}
}
stage {
name = "Apply"
action {
name = "Apply"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["source_output", "plan_output"]
version = "1"
configuration = {
ProjectName = local.build_project_name_apply
PrimarySource = "source_output"
}
}
}
}
resource "aws_cloudwatch_log_group" "build-plan" {
name = local.build_project_name_plan
retention_in_days = var.log_retention_in_days
tags = var.global_tags
}
resource "aws_cloudwatch_log_group" "build-apply" {
name = local.build_project_name_apply
retention_in_days = var.log_retention_in_days
tags = var.global_tags
}
resource "aws_codebuild_project" "build-plan" {
name = local.build_project_name_plan
build_timeout = "10"
service_role = aws_iam_role.build-role.arn
tags = var.global_tags
artifacts {
type = "CODEPIPELINE"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
# image = "aws/codebuild/standard:1.0"
image = "aws/codebuild/amazonlinux2-x86_64-standard:3.0"
type = "LINUX_CONTAINER"
image_pull_credentials_type = "CODEBUILD"
}
source {
type = "CODEPIPELINE"
buildspec = templatefile(
"${path.module}/buildspec.plan.tmpl.yml",
{
TERRAFORM_VERSION = var.terraform_version
RESOURCES_PATH = var.resources_path
tf_log = var.tf_log
}
)
}
logs_config {
cloudwatch_logs {
status = "ENABLED"
group_name = local.build_project_name_plan
}
s3_logs {
status = "DISABLED"
# location = "${aws_s3_bucket.codepipeline_bucket.id}/build-logs/apply"
}
}
}
resource "aws_codebuild_project" "build-apply" {
name = local.build_project_name_apply
build_timeout = "10"
service_role = aws_iam_role.apply-role.arn
tags = var.global_tags
artifacts {
type = "CODEPIPELINE"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
# image = "aws/codebuild/standard:1.0"
image = "aws/codebuild/amazonlinux2-x86_64-standard:3.0"
type = "LINUX_CONTAINER"
image_pull_credentials_type = "CODEBUILD"
}
source {
type = "CODEPIPELINE"
buildspec = templatefile(
"${path.module}/buildspec.apply.tmpl.yml",
{
TERRAFORM_VERSION = var.terraform_version
RESOURCES_PATH = var.resources_path
tf_log = var.tf_log
}
)
}
logs_config {
cloudwatch_logs {
status = "ENABLED"
group_name = local.build_project_name_apply
}
s3_logs {
status = "DISABLED"
# location = "${aws_s3_bucket.codepipeline_bucket.id}/build-logs/apply"
}
}
}
#####################################################################
# APPLY Notifications based on pipeline process
#####################################################################
resource "aws_codestarnotifications_notification_rule" "apply-pipeline-notify" {
detail_type = "FULL"
event_type_ids = [
"codepipeline-pipeline-pipeline-execution-failed",
"codepipeline-pipeline-pipeline-execution-canceled",
"codepipeline-pipeline-pipeline-execution-resumed",
"codepipeline-pipeline-pipeline-execution-succeeded",
"codepipeline-pipeline-pipeline-execution-superseded",
"codepipeline-pipeline-pipeline-execution-started"
]
name = "${aws_codepipeline.apply-pipeline.name}-notify-teams"
resource = aws_codepipeline.apply-pipeline.arn
tags = var.global_tags
target {
address = aws_sns_topic.notify-topic.arn
}
}