forked from aws-ia/terraform-aws-ec2-image-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
311 lines (259 loc) · 10.6 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
# ---------------------------------------------------------------------------------------------------------------------
# Data
# ---------------------------------------------------------------------------------------------------------------------
data "aws_vpc" "selected" {
id = var.vpc_id
}
data "aws_ami" "source_ami" {
most_recent = true
owners = [var.source_ami_owner]
filter {
name = "name"
values = [var.source_ami_name]
}
}
data "aws_imagebuilder_components" "managed_components" {
for_each = {
for index, mc in var.managed_components :
mc.name => mc
}
owner = "Amazon"
filter {
name = "platform"
values = [var.platform]
}
filter {
name = "name"
values = [each.value.name]
}
filter {
name = "version"
values = [each.value.version]
}
}
# ---------------------------------------------------------------------------------------------------------------------
# EC2 Security Group
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_security_group" "security_group" {
count = var.create_security_group ? 1 : 0
#checkov:skip=CKV2_AWS_5:Security Group is being attached if var create_security_group is true
name = "${var.name}-sg"
description = "Security Group for for the EC2 Image Builder Build Instances"
vpc_id = data.aws_vpc.selected.id
tags = var.tags
}
resource "aws_security_group_rule" "sg_https_ingress" {
count = var.create_security_group ? 1 : 0
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [data.aws_vpc.selected.cidr_block]
security_group_id = aws_security_group.security_group[count.index].id
description = "HTTPS from VPC"
}
resource "aws_security_group_rule" "sg_rdp_ingress" {
count = var.create_security_group && length(var.source_cidr) > 0 ? 1 : 0
type = "ingress"
from_port = 3389
to_port = 3389
protocol = "tcp"
cidr_blocks = var.source_cidr
security_group_id = aws_security_group.security_group[count.index].id
description = "RDP from the source variable CIDR"
}
#tfsec:ignore:aws-vpc-no-public-egress-sgr
resource "aws_security_group_rule" "sg_internet_egress" {
count = var.create_security_group ? 1 : 0
type = "egress"
from_port = 0
to_port = 0
protocol = "all"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.security_group[count.index].id
description = "Access to the internet"
}
# ---------------------------------------------------------------------------------------------------------------------
# IAM Role
# ---------------------------------------------------------------------------------------------------------------------
data "aws_iam_policy_document" "assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "awsserviceroleforimagebuilder" {
assume_role_policy = data.aws_iam_policy_document.assume.json
name = "${var.name}-role"
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "imagebuilder" {
policy_arn = "arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilder"
role = aws_iam_role.awsserviceroleforimagebuilder.name
}
resource "aws_iam_role_policy_attachment" "ssm" {
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
role = aws_iam_role.awsserviceroleforimagebuilder.name
}
resource "aws_iam_instance_profile" "iam_instance_profile" {
name = "EC2InstanceProfileImageBuilder"
role = aws_iam_role.awsserviceroleforimagebuilder.name
}
resource "aws_iam_role_policy_attachment" "custom_policy" {
count = var.attach_custom_policy ? 1 : 0
policy_arn = var.custom_policy_arn
role = aws_iam_role.awsserviceroleforimagebuilder.name
}
resource "aws_iam_role_policy" "aws_policy" {
name = "${var.name}-aws-access"
role = aws_iam_role.awsserviceroleforimagebuilder.id
#checkov:skip=CKV_AWS_290:The policy must allow *
#checkov:skip=CKV_AWS_355:The policy must allow *
policy = data.aws_iam_policy_document.aws_policy.json
}
#tfsec:ignore:aws-iam-no-policy-wildcards
data "aws_iam_policy_document" "aws_policy" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
resources = ["arn:aws:iam::*:role/EC2ImageBuilderDistributionCrossAccountRole"]
}
}
# ---------------------------------------------------------------------------------------------------------------------
# EC2 Image Builder Infrastructure Configuration
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_imagebuilder_infrastructure_configuration" "imagebuilder_infrastructure_configuration" {
count = 1
instance_profile_name = aws_iam_instance_profile.iam_instance_profile.name
instance_types = var.instance_types
key_pair = var.instance_key_pair
name = "${var.name}-infrastructure-configuration"
security_group_ids = var.create_security_group ? [aws_security_group.security_group[count.index].id] : var.security_group_ids
subnet_id = var.subnet_id
instance_metadata_options {
http_tokens = "required"
http_put_response_hop_limit = 2
}
terminate_instance_on_failure = var.terminate_on_failure
resource_tags = var.tags
tags = var.tags
logging {
s3_logs {
s3_bucket_name = var.s3_bucket_name
s3_key_prefix = "logs"
}
}
}
# ---------------------------------------------------------------------------------------------------------------------
# EC2 Image Builder Image
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_imagebuilder_image" "imagebuilder_image" {
count = 1
image_recipe_arn = aws_imagebuilder_image_recipe.imagebuilder_image_recipe.arn
infrastructure_configuration_arn = aws_imagebuilder_infrastructure_configuration.imagebuilder_infrastructure_configuration[count.index].arn
distribution_configuration_arn = try(aws_imagebuilder_distribution_configuration.imagebuilder_distribution_configuration[count.index].arn, null)
image_tests_configuration {
image_tests_enabled = true
}
tags = var.tags
timeouts {
create = var.timeout
}
}
# ---------------------------------------------------------------------------------------------------------------------
# EC2 Image Builder Image Pipeline
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_imagebuilder_image_pipeline" "imagebuilder_image_pipeline" {
count = 1
image_recipe_arn = aws_imagebuilder_image_recipe.imagebuilder_image_recipe.arn
infrastructure_configuration_arn = aws_imagebuilder_infrastructure_configuration.imagebuilder_infrastructure_configuration[count.index].arn
distribution_configuration_arn = try(aws_imagebuilder_distribution_configuration.imagebuilder_distribution_configuration[count.index].arn, null)
dynamic "schedule" {
for_each = try(var.schedule_expression, tomap({}))
content {
schedule_expression = schedule.key
pipeline_execution_start_condition = schedule.value
}
}
name = "${var.name}-pipeline"
tags = var.tags
}
# ---------------------------------------------------------------------------------------------------------------------
# EC2 Image Builder Image Recipe
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_imagebuilder_image_recipe" "imagebuilder_image_recipe" {
name = "${var.name}-image-recipe"
parent_image = data.aws_ami.source_ami.id
version = var.recipe_version
# it seems there is a bug on checkov for check CKV_AWS_200, even supressing it doesn't help, had to add the below block_device_mapping to pass
block_device_mapping {
device_name = "/dev/xvdb"
ebs {
delete_on_termination = true
volume_size = var.recipe_volume_size
volume_type = var.recipe_volume_type
encrypted = true
kms_key_id = var.imagebuilder_image_recipe_kms_key_arn
}
}
lifecycle {
create_before_destroy = true
}
dynamic "component" {
for_each = {
for key, value in data.aws_imagebuilder_components.managed_components : key => value.arns
}
content {
component_arn = tolist(component.value)[0]
}
}
dynamic "component" {
for_each = var.build_component_arn
content {
component_arn = component.value
parameter {
name = "S3BucketName"
value = var.s3_bucket_name
}
}
}
dynamic "component" {
for_each = var.test_component_arn
content {
component_arn = component.value
parameter {
name = "S3BucketName"
value = var.s3_bucket_name
}
}
}
tags = var.tags
}
# ---------------------------------------------------------------------------------------------------------------------
# EC2 Image Builder Distribution Configuration
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_imagebuilder_distribution_configuration" "imagebuilder_distribution_configuration" {
count = length(var.ami_regions_kms_key) > 0 ? 1 : 0 #need to add a count due the error "At least 1 "distribution" blocks are required" when ami_regions_kms_key = {}
name = "${var.name}-distribution"
dynamic "distribution" {
for_each = var.ami_regions_kms_key # for each region we need to have a new distribution setting
content {
region = distribution.key
ami_distribution_configuration {
name = "${var.ami_name}-{{ imagebuilder:buildDate }}"
description = var.ami_description
target_account_ids = var.target_account_ids
launch_permission {
user_ids = var.target_account_ids
}
ami_tags = var.tags
# AMI Should be encrypted by different KMS key according to the region, however if no KMS Key is provided the image will not be encrypted
kms_key_id = length(distribution.value) > 0 ? distribution.value : null
}
}
}
tags = var.tags
}