-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.tf
300 lines (252 loc) · 11.5 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
locals {
has_ssh_key_secret = var.ssh_key_secret_arn != null || var.ssh_key_secret_name != null
image_name = coalesce(
var.image_name,
var.name
)
log_prefix_computed = (
var.log_prefix != null
? replace("/${var.log_prefix}/*", "//{2,}/", "/")
: "/*"
)
shared_user_groups = var.public ? ["all"] : null
use_custom_distribution_config = length(var.custom_distribution_configs) > 0 ? true : false
}
data "aws_iam_policy_document" "log_write" {
count = var.log_bucket != null ? 1 : 0
statement {
actions = ["s3:PutObject"]
resources = ["arn:aws:s3:::${var.log_bucket}${local.log_prefix_computed}"]
}
}
resource "aws_iam_policy" "log_write" {
count = var.log_bucket != null ? 1 : 0
name_prefix = "${var.name}-logging-policy-"
description = "IAM policy granting write access to the logging bucket for ${var.name}"
policy = data.aws_iam_policy_document.log_write[0].json
}
data "aws_secretsmanager_secret" "ssh_key" {
count = local.has_ssh_key_secret ? 1 : 0
arn = var.ssh_key_secret_arn
name = var.ssh_key_secret_name
}
data "aws_iam_policy_document" "secret_read" {
count = local.has_ssh_key_secret ? 1 : 0
statement {
resources = [data.aws_secretsmanager_secret.ssh_key[0].arn]
actions = [
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret",
"secretsmanager:ListSecretVersionIds"
]
}
}
resource "aws_iam_policy" "secret_read" {
count = local.has_ssh_key_secret ? 1 : 0
name_prefix = "${var.name}-secret-read-policy-"
description = "IAM policy granting read access to the ssh key secret at ${data.aws_secretsmanager_secret.ssh_key[0].name}"
policy = data.aws_iam_policy_document.secret_read[0].json
}
locals {
core_iam_policies = [
"arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore",
"arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilder"
]
}
data "aws_iam_policy_document" "assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "this" {
assume_role_policy = data.aws_iam_policy_document.assume.json
name_prefix = "${var.name}-imagebuilder-role-"
tags = merge(
var.tags,
{
Name : "${var.name}-imagebuilder-role"
}
)
}
resource "aws_iam_role_policy_attachment" "core" {
count = length(local.core_iam_policies)
policy_arn = local.core_iam_policies[count.index]
role = aws_iam_role.this.name
}
resource "aws_iam_role_policy_attachment" "log_write" {
count = var.log_bucket != null ? 1 : 0
policy_arn = aws_iam_policy.log_write[0].arn
role = aws_iam_role.this.name
}
resource "aws_iam_role_policy_attachment" "secret_read" {
count = local.has_ssh_key_secret ? 1 : 0
policy_arn = aws_iam_policy.secret_read[0].arn
role = aws_iam_role.this.name
}
resource "aws_iam_role_policy_attachment" "additional" {
count = length(var.additional_iam_policy_arns)
policy_arn = var.additional_iam_policy_arns[count.index]
role = aws_iam_role.this.name
}
resource "aws_iam_instance_profile" "this" {
name_prefix = "${var.name}-imagebuilder-instance-profile-"
role = aws_iam_role.this.name
}
resource "aws_imagebuilder_image_pipeline" "this" {
name = var.name
container_recipe_arn = var.container_recipe_arn
description = var.description
distribution_configuration_arn = aws_imagebuilder_distribution_configuration.this.arn
enhanced_image_metadata_enabled = var.enhanced_image_metadata_enabled
image_recipe_arn = var.image_recipe_arn
infrastructure_configuration_arn = aws_imagebuilder_infrastructure_configuration.this.arn
status = var.enabled ? "ENABLED" : "DISABLED"
tags = var.tags
image_tests_configuration {
image_tests_enabled = var.image_tests_enabled
timeout_minutes = var.image_tests_timeout_minutes
}
dynamic "schedule" {
for_each = var.schedule_cron != "" ? ["1"] : []
content {
pipeline_execution_start_condition = var.schedule_pipeline_execution_start_condition
schedule_expression = var.schedule_cron
timezone = var.schedule_timezone
}
}
}
resource "aws_imagebuilder_infrastructure_configuration" "this" {
name = "${var.name} - Infrastructure Config"
description = var.description
instance_profile_name = aws_iam_instance_profile.this.name
instance_types = var.instance_types
key_pair = var.instance_key_pair
resource_tags = var.resource_tags
security_group_ids = var.security_group_ids
sns_topic_arn = var.sns_topic_arn
subnet_id = var.subnet
tags = var.tags
terminate_instance_on_failure = var.terminate_on_failure
dynamic "logging" {
for_each = var.log_bucket != null ? ["1"] : []
content {
s3_logs {
s3_bucket_name = var.log_bucket
s3_key_prefix = var.log_prefix
}
}
}
instance_metadata_options {
http_put_response_hop_limit = var.instance_metadata_http_put_hop_limit
http_tokens = var.instance_metadata_http_tokens
}
}
resource "aws_imagebuilder_distribution_configuration" "this" {
name = "${var.name} - Distribution Config"
description = var.description
tags = var.tags
# We'll normally use this, a sane distribution configuration for creating AMIs
dynamic "distribution" {
for_each = local.use_custom_distribution_config ? [] : var.regions
content {
region = distribution.value
license_configuration_arns = var.license_config_arns
ami_distribution_configuration {
ami_tags = var.tags
description = var.description
kms_key_id = var.kms_key_id
name = "${local.image_name}-{{ imagebuilder:buildDate }}"
launch_permission {
organization_arns = var.shared_organization_arns
organizational_unit_arns = var.shared_ou_arns
user_groups = local.shared_user_groups
user_ids = var.shared_account_ids
}
}
dynamic "launch_template_configuration" {
for_each = lookup(var.launch_template_configurations, distribution.value, [])
content {
default = lookup(launch_template_configuration.value, "default", null)
account_id = lookup(launch_template_configuration.value, "account_id", null)
launch_template_id = lookup(launch_template_configuration.value, "launch_template_id", null)
}
}
}
}
# Here be dragons. This is for specifying a custom set of distribution configurations as a parameter to the module.
# If you're not using the custom_distribution_configs var you can ignore this dynamic block safely.
dynamic "distribution" {
for_each = var.custom_distribution_configs
content {
region = lookup(distribution.value, "region", null)
license_configuration_arns = lookup(distribution.value, "license_configuration_arns", null)
dynamic "ami_distribution_configuration" {
for_each = length(keys(lookup(distribution.value, "ami_distribution_configuration", {}))) == 0 ? [] : [lookup(distribution.value, "ami_distribution_configuration", {})]
content {
ami_tags = lookup(ami_distribution_configuration.value, "ami_tags", null)
description = lookup(ami_distribution_configuration.value, "description", null)
kms_key_id = lookup(ami_distribution_configuration.value, "kms_key_id", null)
name = lookup(ami_distribution_configuration.value, "name", null)
target_account_ids = lookup(ami_distribution_configuration.value, "target_account_ids", null)
dynamic "launch_permission" {
for_each = length(keys(lookup(ami_distribution_configuration.value, "launch_permission", {}))) == 0 ? [] : [lookup(ami_distribution_configuration.value, "launch_permission", {})]
content {
organization_arns = lookup(launch_permission.value, "organization_arns", null)
organizational_unit_arns = lookup(launch_permission.value, "organizational_unit_arns", null)
user_groups = lookup(launch_permission.value, "user_groups", null)
user_ids = lookup(launch_permission.value, "user_ids", null)
}
}
}
}
dynamic "container_distribution_configuration" {
for_each = length(keys(lookup(distribution.value, "container_distribution_configuration", {}))) == 0 ? [] : [lookup(distribution.value, "container_distribution_configuration", {})]
content {
container_tags = lookup(container_distribution_configuration.value, "container_tags", null)
description = lookup(container_distribution_configuration.value, "description", null)
dynamic "target_repository" {
for_each = length(keys(lookup(container_distribution_configuration.value, "target_repository", {}))) == 0 ? [] : [lookup(container_distribution_configuration.value, "target_repository", {})]
content {
repository_name = lookup(target_repository.value, "repository_name", null)
service = lookup(target_repository.value, "service", null)
}
}
}
}
dynamic "fast_launch_configuration" {
for_each = length(keys(lookup(distribution.value, "fast_launch_configuration", {}))) == 0 ? [] : [lookup(distribution.value, "fast_launch_configuration", {})]
content {
account_id = lookup(fast_launch_configuration.value, "account_id", null)
enabled = lookup(fast_launch_configuration.value, "enabled", null)
max_parallel_launches = lookup(fast_launch_configuration.value, "max_parallel_launches", null)
dynamic "launch_template" {
for_each = length(keys(lookup(fast_launch_configuration.value, "launch_template", {}))) == 0 ? [] : [lookup(fast_launch_configuration.value, "launch_template", {})]
content {
launch_template_id = lookup(launch_template.value, "launch_template_id", null)
launch_template_name = lookup(launch_template.value, "launch_template_name", null)
launch_template_version = lookup(launch_template.value, "launch_template_version", null)
}
}
dynamic "snapshot_configuration" {
for_each = length(keys(lookup(fast_launch_configuration.value, "snapshot_configuration", {}))) == 0 ? [] : [lookup(fast_launch_configuration.value, "snapshot_configuration", {})]
content {
target_resource_count = lookup(snapshot_configuration.value, "target_resource_count", null)
}
}
}
}
dynamic "launch_template_configuration" {
for_each = length(keys(lookup(distribution.value, "launch_template_configuration", {}))) == 0 ? [] : [lookup(distribution.value, "launch_template_configuration", {})]
content {
default = lookup(launch_template_configuration.value, "default", null)
account_id = lookup(launch_template_configuration.value, "account_id", null)
launch_template_id = lookup(launch_template_configuration.value, "launch_template_id", null)
}
}
}
}
}