-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.tf
358 lines (331 loc) · 9.77 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
#################
# Data and local variables
#################
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_vpc" "this" {
id = local.is_vpc_provided ? var.vpc_id : null
default = local.is_vpc_provided ? false : true
}
data "aws_ip_ranges" "codebuild" {
regions = [data.aws_region.current.name]
services = ["codebuild"]
}
locals {
bucket_name = substr("${var.prefix}-image-${data.aws_region.current.name}-${random_id.s3.hex}", 0, 63)
is_vpc_provided = var.vpc_id != "" && length(var.subnet_ids) > 0
sg_source_cidrs = local.is_vpc_provided ? [data.aws_vpc.this.cidr_block] : data.aws_ip_ranges.codebuild.cidr_blocks
}
#################
# Resources
#################
resource "random_id" "s3" {
byte_length = 16
}
resource "aws_s3_bucket" "source" {
acl = "private"
bucket = local.bucket_name
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
}
}
}
tags = merge(var.tags, { Name : local.bucket_name })
versioning {
enabled = true
}
}
data "aws_iam_policy_document" "service-assume" {
statement {
actions = [
"sts:AssumeRole"
]
effect = "Allow"
principals {
identifiers = [
"codebuild.amazonaws.com"
]
type = "Service"
}
}
}
resource "aws_iam_role" "service" {
name = "${var.prefix}-kafka-codebuild"
tags = var.tags
assume_role_policy = data.aws_iam_policy_document.service-assume.json
}
data "aws_iam_policy_document" "codebuild" {
statement {
actions = [
"logs:*",
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups",
"ec2:DescribeDhcpOptions",
"ec2:DescribeVpcs",
"ec2:CreateNetworkInterfacePermission"
]
effect = "Allow"
resources = ["*"]
}
statement {
actions = [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:PutObject"
]
effect = "Allow"
resources = [
"arn:aws:s3:::${local.bucket_name}",
"arn:aws:s3:::${local.bucket_name}/*"
]
}
statement {
actions = [
"s3:ListBucket",
"s3:GetBucketAcl",
"s3:GetBucketLocation"
]
effect = "Allow"
resources = [
"arn:aws:s3:::${local.bucket_name}"
]
}
statement {
actions = [
"ec2:AttachVolume",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:CopyImage",
"ec2:CreateImage",
"ec2:CreateKeypair",
"ec2:CreateSecurityGroup",
"ec2:CreateSnapshot",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:DeleteKeyPair",
"ec2:DeleteSecurityGroup",
"ec2:DeleteSnapshot",
"ec2:DeleteVolume",
"ec2:DeregisterImage",
"ec2:DescribeImageAttribute",
"ec2:DescribeImages",
"ec2:DescribeInstances",
"ec2:DescribeRegions",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSnapshots",
"ec2:DescribeSubnets",
"ec2:DescribeTags",
"ec2:DescribeVolumes",
"ec2:DetachVolume",
"ec2:GetPasswordData",
"ec2:ModifyImageAttribute",
"ec2:ModifyInstanceAttribute",
"ec2:ModifySnapshotAttribute",
"ec2:RegisterImage",
"ec2:RunInstances",
"ec2:StopInstances",
"ec2:TerminateInstances",
"sts:DecodeAuthorizationMessage"
]
effect = "Allow"
resources = ["*"]
}
statement {
actions = [
"iam:PassRole",
"iam:GetInstanceProfile"
]
effect = "Allow"
resources = ["*"]
}
}
resource "aws_iam_role_policy" "codebuild" {
name = "codebuild"
role = aws_iam_role.service.id
policy = data.aws_iam_policy_document.codebuild.json
}
data "aws_iam_policy_document" "packer-assume" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
identifiers = ["ec2.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_iam_role" "packer" {
name = "${var.prefix}-packer-instance-role"
tags = var.tags
assume_role_policy = data.aws_iam_policy_document.packer-assume.json
}
resource "aws_iam_instance_profile" "packer" {
name = aws_iam_role.packer.name
role = aws_iam_role.packer.name
}
data "aws_iam_policy_document" "packer" {
statement {
actions = [
"ec2:AttachVolume",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:CopyImage",
"ec2:CreateImage",
"ec2:CreateKeypair",
"ec2:CreateSecurityGroup",
"ec2:CreateSnapshot",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:DeleteKeyPair",
"ec2:DeleteSecurityGroup",
"ec2:DeleteSnapshot",
"ec2:DeleteVolume",
"ec2:DeregisterImage",
"ec2:DescribeImageAttribute",
"ec2:DescribeImages",
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus",
"ec2:DescribeRegions",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSnapshots",
"ec2:DescribeSubnets",
"ec2:DescribeTags",
"ec2:DescribeVolumes",
"ec2:DetachVolume",
"ec2:GetPasswordData",
"ec2:ModifyImageAttribute",
"ec2:ModifyInstanceAttribute",
"ec2:ModifySnapshotAttribute",
"ec2:RegisterImage",
"ec2:RunInstances",
"ec2:StopInstances",
"ec2:TerminateInstances",
"ec2:CreateLaunchTemplate",
"ec2:DeleteLaunchTemplate",
"ec2:CreateFleet",
"ec2:DescribeSpotPriceHistory"
]
effect = "Allow"
resources = ["*"]
}
}
resource "aws_iam_role_policy" "packer" {
name = "ec2"
role = aws_iam_role.packer.id
policy = data.aws_iam_policy_document.packer.json
}
resource "aws_cloudwatch_log_group" "packer" {
name = "/aws/codebuild/${var.prefix}-kafka-automation-packer"
retention_in_days = 7
}
resource "aws_security_group" "codebuild-egress" {
count = local.is_vpc_provided ? 1 : 0
name_prefix = "${var.prefix}-kafka-codebuild-"
description = "Group that CodeBuild uses to allow access to resources in the VPC and the Internet."
vpc_id = var.vpc_id
egress {
from_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
to_port = 0
}
tags = merge(var.tags, { Name : "${var.prefix}-kafka-codebuild" })
}
data "archive_file" "sources" {
type = "zip"
output_path = "${path.module}/sources.zip"
source_dir = "${path.module}/packer"
}
resource "aws_s3_bucket_object" "sources" {
acl = "private"
key = "codebuild/${var.prefix}-kafka-packer-sources.zip"
bucket = aws_s3_bucket.source.bucket
source = data.archive_file.sources.output_path
storage_class = "STANDARD_IA"
etag = data.archive_file.sources.output_md5
}
// Introduce wait time to work around race condition between CodeBuild project and IAM service role creations.
// CodeBuild complains it can't assume the role, even though it exists and has the proper assume policy.
resource "null_resource" "delay" {
depends_on = [
aws_iam_role.service
]
provisioner "local-exec" {
command = "/bin/sleep 10"
}
}
resource "aws_codebuild_project" "packer" {
name = "${var.prefix}-kafka-automation-packer"
// The resource id isn't important, it's there to force dependency on the resource.
description = "Runs Packer to build AMI${substr(null_resource.delay.id, 0, 0)}"
service_role = aws_iam_role.service.arn
logs_config {
cloudwatch_logs {
group_name = aws_cloudwatch_log_group.packer.name
}
}
artifacts {
type = "NO_ARTIFACTS"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/amazonlinux2-x86_64-standard:3.0"
type = "LINUX_CONTAINER"
}
source {
buildspec = <<EOF
---
version: 0.2
phases:
install:
runtime-versions:
java: corretto11
commands:
- curl -sL -o packer.zip https://releases.hashicorp.com/packer/1.7.8/packer_1.7.8_linux_amd64.zip && unzip packer.zip
- pip3 -q install 'ansible~=2.9'
pre_build:
commands:
- ./packer validate
-var region=${data.aws_region.current.name}
-var vpc_id='${local.is_vpc_provided ? data.aws_vpc.this.id : ""}'
-var security_group_source_cidrs=${join(",", local.sg_source_cidrs)}
-var subnet_id='${local.is_vpc_provided ? var.subnet_ids[0] : ""}'
-var ami_base_name=${var.prefix}
-var iam_instance_profile=${aws_iam_instance_profile.packer.name}
-var instance_type=${var.packer_instance_type}
-var source_version=$${CODEBUILD_SOURCE_VERSION-LATEST}
-var build_id=$${CODEBUILD_BUILD_ID-UNKNOWN}
-var build_number=$${CODEBUILD_BUILD_NUMBER-UNKNOWN}
${var.packer_template}
build:
commands:
- ./packer build -color=false
-var region=${data.aws_region.current.name}
-var vpc_id='${local.is_vpc_provided ? data.aws_vpc.this.id : ""}'
-var security_group_source_cidrs=${join(",", local.sg_source_cidrs)}
-var subnet_id='${local.is_vpc_provided ? var.subnet_ids[0] : ""}'
-var ami_base_name=${var.prefix}
-var iam_instance_profile=${aws_iam_instance_profile.packer.name}
-var instance_type=${var.packer_instance_type}
-var source_version=$${CODEBUILD_SOURCE_VERSION-LATEST}
-var build_id=$${CODEBUILD_BUILD_ID-UNKNOWN}
-var build_number=$${CODEBUILD_BUILD_NUMBER-UNKNOWN}
${var.packer_template}
EOF
type = "S3"
location = "${aws_s3_bucket_object.sources.bucket}/${aws_s3_bucket_object.sources.key}"
}
tags = var.tags
dynamic "vpc_config" {
for_each = local.is_vpc_provided ? map(var.vpc_id, var.subnet_ids) : {}
content {
security_group_ids = [aws_security_group.codebuild-egress[0].id]
subnets = vpc_config.value
vpc_id = vpc_config.key
}
}
}