-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.tf
More file actions
285 lines (242 loc) · 10.2 KB
/
main.tf
File metadata and controls
285 lines (242 loc) · 10.2 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
locals {
tags = {
terraform = true
Name = var.ec2_name
}
}
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "3.3.0"
count = var.create_ec2 ? 1 : 0
name = var.ec2_name
ami = var.ec2_ami
instance_type = var.ec2_instance_type
availability_zone = var.ec2_availability_zone
key_name = var.ec2_key_name != null ? var.ec2_key_name : element(concat(aws_key_pair.this.*.key_name, [""]), 0)
monitoring = var.ec2_monitoring
vpc_security_group_ids = var.ec2_vpc_security_group_ids != null ? var.ec2_vpc_security_group_ids : [element(concat(aws_security_group.ec2_sg.*.id, [""]), 0)]
subnet_id = var.ec2_subnet_id
user_data = var.ec2_user_data
user_data_base64 = var.ec2_user_data != null ? null : var.ec2_user_data_base64
hibernation = var.ec2_hibernation
iam_instance_profile = var.create_iam_instance_profile ? element(concat(aws_iam_instance_profile.this.*.id, [""]), 0) : var.instance_profile_name != "" ? var.instance_profile_name : null
associate_public_ip_address = var.ec2_associate_public_ip_address
private_ip = var.ec2_private_ip
secondary_private_ips = var.ec2_secondary_private_ips
ipv6_address_count = var.ec2_ipv6_address_count
ipv6_addresses = var.ec2_ipv6_addresses
ebs_optimized = var.ec2_ebs_optimized
root_block_device = var.ec2_ebs_block_device
tags = merge(
local.tags,
var.tags
)
volume_tags = merge(
local.tags,
var.tags
)
}
resource "aws_lb_target_group_attachment" "test" {
count = var.create_target_group_attachment ? length(var.target_group) : 0
target_group_arn = var.target_group[count.index].attachment
target_id = element(concat(module.ec2_instance.*.id, [""]), 0)
port = var.target_group[count.index].port
}
resource "aws_eip" "eip" {
count = var.create_eip && var.create_ec2 ? 1 : 0
instance = element(concat(module.ec2_instance.*.id, [""]), 0)
address = var.eip_address != "" ? var.eip_address : null
vpc = true
}
resource "aws_eip_association" "eip_assoc" {
count = var.create_eip_association && var.create_ec2 ? 1 : 0
instance_id = element(concat(module.ec2_instance.*.id, [""]), 0)
allocation_id = var.eip_association_allocation_id
}
resource "aws_iam_instance_profile" "this" {
count = var.create_iam_instance_profile ? 1 : 0
name = var.ec2_name
role = var.create_role ? element(concat(aws_iam_role.this.*.id, [""]), 0) : var.instance_profile_role
}
resource "aws_iam_role" "this" {
count = var.create_role ? 1 : 0
name = "${var.ec2_name}-role"
assume_role_policy = element(concat(data.aws_iam_policy_document.policy_role.*.json, [""]), 0)
}
data "aws_iam_policy_document" "policy_role" {
count = var.create_role ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "attach" {
count = var.create_attachment_role ? 1 : 0
role = element(concat(aws_iam_role.this.*.name, [""]), 0)
policy_arn = element(concat(aws_iam_policy.this.*.arn, [""]), 0)
}
resource "aws_iam_role_policy_attachment" "attach_aws" {
count = length(var.attach_aws_policy_arn) != 0 ? length(var.attach_aws_policy_arn) : 0
role = element(concat(aws_iam_role.this.*.name, [""]), 0)
policy_arn = var.attach_aws_policy_arn[count.index]
}
resource "aws_iam_policy" "this" {
count = var.create_attachment_role && var.create_policy ? 1 : 0
name = var.iam_policy_name == "" ? "${var.ec2_name}-policy" : var.iam_policy_name
description = "A policy for ec2 bastion"
policy = var.policy_json
}
resource "aws_key_pair" "this" {
count = var.create_key_pair ? 1 : 0
key_name = var.key_name
public_key = var.public_key
}
resource "aws_security_group" "ec2_sg" {
count = var.create_sg ? 1 : 0
name = "${var.ec2_name}-ec2"
description = "Security Group for ${var.ec2_name} ec2"
vpc_id = var.sg_vpc_id
dynamic "ingress" {
for_each = var.ec2_sg_ingress_rules
content {
description = ingress.value["description"]
from_port = ingress.value["from_port"]
to_port = ingress.value["to_port"]
cidr_blocks = lookup(ingress.value, "cidr_blocks", null)
protocol = ingress.value["protocol"]
security_groups = lookup(ingress.value, "security_groups", null)
}
}
dynamic "egress" {
for_each = var.ec2_sg_egress_rules
content {
description = egress.value["description"]
from_port = egress.value["from_port"]
to_port = egress.value["to_port"]
cidr_blocks = lookup(egress.value, "cidr_blocks", null)
protocol = egress.value["protocol"]
security_groups = lookup(egress.value, "security_groups", null)
}
}
tags = merge(
local.tags,
var.tags
)
}
resource "aws_launch_template" "this" {
count = var.create_lauch_template ? 1 : 0
name_prefix = var.ec2_name
ebs_optimized = var.ec2_ebs_optimized
instance_type = var.ec2_instance_type
image_id = var.ec2_ami
key_name = var.ec2_key_name != null ? var.ec2_key_name : element(concat(aws_key_pair.this.*.key_name, [""]), 0)
user_data = var.ec2_user_data_base64
iam_instance_profile {
name = var.create_iam_instance_profile ? element(concat(aws_iam_instance_profile.this.*.id, [""]), 0) : var.instance_profile_name != "" ? var.instance_profile_name : null
}
network_interfaces {
device_index = 0
associate_public_ip_address = var.ec2_associate_public_ip_address
delete_on_termination = true
security_groups = var.ec2_vpc_security_group_ids != null ? var.ec2_vpc_security_group_ids : [element(concat(aws_security_group.ec2_sg.*.id, [""]), 0)]
}
dynamic "block_device_mappings" {
for_each = var.block_device_mappings
content {
device_name = lookup(block_device_mappings.value, "device_name", null)
no_device = lookup(block_device_mappings.value, "no_device", null)
virtual_name = lookup(block_device_mappings.value, "virtual_name", null)
dynamic "ebs" {
for_each = lookup(block_device_mappings.value, "ebs", null) == null ? [] : ["ebs"]
content {
delete_on_termination = lookup(block_device_mappings.value.ebs, "delete_on_termination", null)
encrypted = lookup(block_device_mappings.value.ebs, "encrypted", null)
iops = lookup(block_device_mappings.value.ebs, "iops", null)
kms_key_id = lookup(block_device_mappings.value.ebs, "kms_key_id", null)
snapshot_id = lookup(block_device_mappings.value.ebs, "snapshot_id", null)
volume_size = lookup(block_device_mappings.value.ebs, "volume_size", null)
volume_type = lookup(block_device_mappings.value.ebs, "volume_type", null)
}
}
}
}
dynamic "instance_market_options" {
for_each = var.instance_market_options != null ? [var.instance_market_options] : []
content {
market_type = lookup(instance_market_options.value, "market_type", null)
dynamic "spot_options" {
for_each = (instance_market_options.value.spot_options != null ?
[instance_market_options.value.spot_options] : [])
content {
block_duration_minutes = lookup(spot_options.value, "block_duration_minutes", null)
instance_interruption_behavior = lookup(spot_options.value, "instance_interruption_behavior", null)
max_price = lookup(spot_options.value, "max_price", null)
spot_instance_type = lookup(spot_options.value, "spot_instance_type", null)
valid_until = lookup(spot_options.value, "valid_until", null)
}
}
}
}
tag_specifications {
resource_type = "instance"
tags = merge(
local.tags,
var.tags
)
}
tags = merge(
local.tags,
var.tags
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "this" {
count = var.create_autoscaling_group && var.autoscaling_name != "" ? 1 : 0
name = var.autoscaling_name
vpc_zone_identifier = var.autoscaling_subnets
max_size = var.autoscaling_max_size
min_size = var.autoscaling_min_size
desired_capacity = var.autoscaling_desired_capacity
target_group_arns = var.autoscaling_target_group_arns
health_check_grace_period = var.health_check_grace_period
#termination_policies = var.termination_policies
#enabled_metrics = var.enabled_metrics
#service_linked_role_arn = var.service_linked_role_arn
launch_template {
id = element(concat(aws_launch_template.this.*.id, [""]), 0)
version = element(concat(aws_launch_template.this.*.latest_version, [""]), 0)
}
lifecycle {
create_before_destroy = true
ignore_changes = [desired_capacity]
}
}
resource "aws_autoscaling_schedule" "up" {
count = var.create_autoscaling_group && var.create_autoscaling_schedule_up ? 1 : 0
scheduled_action_name = "up"
min_size = var.autoscaling_max_size
max_size = var.autoscaling_min_size
desired_capacity = var.autoscaling_desired_capacity
recurrence = var.up_recurrence
start_time = var.up_star_time
end_time = var.up_end_time
autoscaling_group_name = element(concat(aws_autoscaling_group.this.*.name, [""]), 0)
time_zone = var.time_zone
}
resource "aws_autoscaling_schedule" "down" {
count = var.create_autoscaling_group && var.create_autoscaling_schedule_down ? 1 : 0
scheduled_action_name = "down"
min_size = 0
max_size = 0
desired_capacity = 0
recurrence = var.down_recurrence
start_time = var.down_star_time
end_time = var.down_end_time
autoscaling_group_name = element(concat(aws_autoscaling_group.this.*.name, [""]), 0)
time_zone = var.time_zone
}