forked from cloudposse/terraform-aws-cicd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
267 lines (217 loc) · 6.89 KB
/
main.tf
File metadata and controls
267 lines (217 loc) · 6.89 KB
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
data "aws_caller_identity" "default" {
}
data "aws_region" "default" {
}
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.15.0"
enabled = var.enabled
namespace = var.namespace
name = var.name
stage = var.stage
delimiter = var.delimiter
attributes = var.attributes
tags = var.tags
}
resource "aws_s3_bucket" "default" {
count = var.enabled ? 1 : 0
bucket = module.label.id
acl = "private"
force_destroy = var.force_destroy
tags = module.label.tags
}
resource "aws_iam_role" "default" {
count = var.enabled ? 1 : 0
name = module.label.id
assume_role_policy = join("", data.aws_iam_policy_document.assume.*.json)
}
data "aws_iam_policy_document" "assume" {
count = var.enabled ? 1 : 0
statement {
sid = ""
actions = [
"sts:AssumeRole"
]
principals {
type = "Service"
identifiers = ["codepipeline.amazonaws.com"]
}
effect = "Allow"
}
}
resource "aws_iam_role_policy_attachment" "default" {
count = var.enabled ? 1 : 0
role = join("", aws_iam_role.default.*.id)
policy_arn = join("", aws_iam_policy.default.*.arn)
}
resource "aws_iam_policy" "default" {
count = var.enabled ? 1 : 0
name = module.label.id
policy = join("", data.aws_iam_policy_document.default.*.json)
}
data "aws_iam_policy_document" "default" {
count = var.enabled ? 1 : 0
statement {
sid = ""
actions = [
"elasticbeanstalk:*",
"ec2:*",
"elasticloadbalancing:*",
"autoscaling:*",
"cloudwatch:*",
"s3:*",
"sns:*",
"cloudformation:*",
"rds:*",
"sqs:*",
"ecs:*",
"iam:PassRole",
"logs:PutRetentionPolicy",
]
resources = ["*"]
effect = "Allow"
}
}
resource "aws_iam_role_policy_attachment" "s3" {
count = var.enabled ? 1 : 0
role = join("", aws_iam_role.default.*.id)
policy_arn = join("", aws_iam_policy.s3.*.arn)
}
resource "aws_iam_policy" "s3" {
count = var.enabled ? 1 : 0
name = "${module.label.id}-s3"
policy = join("", data.aws_iam_policy_document.s3.*.json)
}
data "aws_iam_policy_document" "s3" {
count = var.enabled ? 1 : 0
statement {
sid = ""
actions = [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:GetBucketVersioning",
"s3:PutObject",
]
resources = [
join("", aws_s3_bucket.default.*.arn),
"${join("", aws_s3_bucket.default.*.arn)}/*",
"arn:aws:s3:::elasticbeanstalk*"
]
effect = "Allow"
}
}
resource "aws_iam_role_policy_attachment" "codebuild" {
count = var.enabled ? 1 : 0
role = join("", aws_iam_role.default.*.id)
policy_arn = join("", aws_iam_policy.codebuild.*.arn)
}
resource "aws_iam_policy" "codebuild" {
count = var.enabled ? 1 : 0
name = "${module.label.id}-codebuild"
policy = join("", data.aws_iam_policy_document.codebuild.*.json)
}
data "aws_iam_policy_document" "codebuild" {
count = var.enabled ? 1 : 0
statement {
sid = ""
actions = [
"codebuild:*"
]
resources = [module.codebuild.project_id]
effect = "Allow"
}
}
module "codebuild" {
source = "git::https://github.com/cloudposse/terraform-aws-codebuild.git?ref=tags/0.17.0"
enabled = var.enabled
namespace = var.namespace
name = var.name
stage = var.stage
build_image = var.build_image
build_compute_type = var.build_compute_type
buildspec = var.buildspec
delimiter = var.delimiter
attributes = concat(var.attributes, ["build"])
tags = var.tags
privileged_mode = var.privileged_mode
aws_region = var.region != "" ? var.region : data.aws_region.default.name
aws_account_id = var.aws_account_id != "" ? var.aws_account_id : data.aws_caller_identity.default.account_id
image_repo_name = var.image_repo_name
image_tag = var.image_tag
github_token = var.github_oauth_token
environment_variables = var.environment_variables
cache_bucket_suffix_enabled = var.codebuild_cache_bucket_suffix_enabled
}
resource "aws_iam_role_policy_attachment" "codebuild_s3" {
count = var.enabled ? 1 : 0
role = module.codebuild.role_id
policy_arn = join("", aws_iam_policy.s3.*.arn)
}
# Only one of the `aws_codepipeline` resources below will be created:
# "source_build_deploy" will be created if `var.enabled` is set to `true` and the Elastic Beanstalk application name and environment name are specified
# This is used in two use-cases:
# 1. GitHub -> S3 -> Elastic Beanstalk (running application stack like Node, Go, Java, IIS, Python)
# 2. GitHub -> ECR (Docker image) -> Elastic Beanstalk (running Docker stack)
# "source_build" will be created if `var.enabled` is set to `true` and the Elastic Beanstalk application name or environment name are not specified
# This is used in this use-case:
# 1. GitHub -> ECR (Docker image)
resource "aws_codepipeline" "default" {
# Elastic Beanstalk application name and environment name are specified
count = var.enabled ? 1 : 0
name = module.label.id
role_arn = join("", aws_iam_role.default.*.arn)
artifact_store {
location = join("", aws_s3_bucket.default.*.bucket)
type = "S3"
}
stage {
name = "Source"
action {
name = "Source"
category = "Source"
owner = "ThirdParty"
provider = "GitHub"
version = "1"
output_artifacts = ["code"]
configuration = {
OAuthToken = var.github_oauth_token
Owner = var.repo_owner
Repo = var.repo_name
Branch = var.branch
PollForSourceChanges = var.poll_source_changes
}
}
}
stage {
name = "Build"
action {
name = "Build"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
version = "1"
input_artifacts = ["code"]
output_artifacts = ["package"]
configuration = {
ProjectName = module.codebuild.project_name
}
}
}
dynamic "stage" {
for_each = var.elastic_beanstalk_application_name != "" && var.elastic_beanstalk_environment_name != "" ? ["true"] : []
content {
name = "Deploy"
action {
name = "Deploy"
category = "Deploy"
owner = "AWS"
provider = "ElasticBeanstalk"
input_artifacts = ["package"]
version = "1"
configuration = {
ApplicationName = var.elastic_beanstalk_application_name
EnvironmentName = var.elastic_beanstalk_environment_name
}
}
}
}
}