-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
165 lines (148 loc) · 6.61 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
########################################
# Tagging
# resources to reformat tags
# transform from map to json for CloudFormation template
########################################
locals {
tags_asg_format = jsonencode(null_resource.tags_for_asg.*.triggers)
tags_lt_format = jsonencode(null_resource.tags_for_lt.*.triggers)
create_lt = var.launch_template_name == "" && lower(var.scaling_object_type) == "launchtemplate"
access_logging = var.lb_access_logging_bucket == null ? [] : [{
bucket = var.lb_access_logging_bucket
prefix = var.lb_access_logging_prefix
}]
}
resource "null_resource" "tags_for_asg" {
count = length(keys(var.tags))
triggers = {
"Key" = keys(var.tags)[count.index]
"PropagateAtLaunch" = true
"Value" = values(var.tags)[count.index]
}
}
resource "null_resource" "tags_for_lt" {
count = length(keys(var.tags))
triggers = {
"Key" = keys(var.tags)[count.index]
"Value" = values(var.tags)[count.index]
}
}
########################################
# LB
########################################
resource "aws_cloudformation_stack" "this" {
name = "${var.name}-asg-stack"
tags = var.tags
template_body = templatefile("${path.module}/auto-scaling-group.yml.tpl",
{
name = var.name
asg_tags = local.tags_asg_format
createLt = local.create_lt
description = "Autoscaling group for EC2 cluster"
healthCheck = var.health_check_type
image_id = var.image_id
instance_security_groups = join("\", \"", var.instance_security_groups)
instance_type = var.instance_type
key_name = var.keypair_name
launchTemplateName = var.launch_template_name
launchTemplateOverrides = var.launch_template_overrides
launchTemplateVersion = var.launch_template_version
maxBatch = var.batch_max_size
maxLifetime = var.max_instance_lifetime
maxSize = var.max_instances
minInService = var.min_instances_in_service
minSize = var.min_instances
onDemandAllocationStrategy = var.on_demand_allocation_strategy
onDemandBaseCapacity = var.on_demand_base_capacity
onDemandPercentageAboveBaseCapacity = var.on_demand_percent_above_base_capacity
pauseTime = var.pause_time
scalingObject = var.scaling_object_type
spotAllocationStrategy = var.spot_allocation_strategy
spotInstancePools = var.spot_instance_pools
spotMaxPrice = var.spot_max_price
tags = local.tags_lt_format
targetGroups = aws_lb_target_group.this.id
subnets = join("\",\"", var.subnet_ids)
}
)
}
resource "aws_lb" "this" {
name = "${var.name}-alb"
drop_invalid_header_fields = var.elb_drop_invalid_headers
internal = var.aws_lb_internal
load_balancer_type = "application"
security_groups = var.lb_security_groups
subnets = var.subnet_ids
tags = var.tags
dynamic "access_logs" {
iterator = log
for_each = local.access_logging
content {
bucket = log.value.bucket
enabled = true
prefix = lookup(log.value, "prefix", null)
}
}
}
resource "aws_lb_listener" "this" {
load_balancer_arn = aws_lb.this.arn
port = var.lb_listener_port
protocol = var.lb_listener_protocol #tfsec:ignore:AWS004
ssl_policy = var.lb_listener_protocol == "HTTPS" ? var.lb_listener_ssl_policy : null
certificate_arn = var.lb_listener_certificate != "" ? var.lb_listener_certificate : null
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.this.arn
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_lb_target_group" "this" {
name_prefix = substr("${var.name}-tg", 0, 6)
port = var.health_check_port
protocol = "HTTP"
slow_start = 60
tags = var.tags
target_type = "instance"
vpc_id = var.vpc_id
health_check {
healthy_threshold = "2"
path = var.health_check_path
protocol = "HTTP" #tfsec:ignore:AWS004, TODO: make var
}
}
output "rendered" {
value = templatefile("${path.module}/auto-scaling-group.yml.tpl",
{
name = var.name
asg_tags = local.tags_asg_format
createLt = local.create_lt
description = "Autoscaling group for EC2 cluster"
healthCheck = var.health_check_type
image_id = var.image_id
instance_security_groups = join("\", \"", var.instance_security_groups)
instance_type = var.instance_type
key_name = var.keypair_name
launchTemplateName = var.launch_template_name
launchTemplateOverrides = var.launch_template_overrides
launchTemplateVersion = var.launch_template_version
maxBatch = var.batch_max_size
maxLifetime = var.max_instance_lifetime
maxSize = var.max_instances
minInService = var.min_instances_in_service
minSize = var.min_instances
onDemandAllocationStrategy = var.on_demand_allocation_strategy
onDemandBaseCapacity = var.on_demand_base_capacity
onDemandPercentageAboveBaseCapacity = var.on_demand_percent_above_base_capacity
pauseTime = var.pause_time
scalingObject = var.scaling_object_type
spotAllocationStrategy = var.spot_allocation_strategy
spotInstancePools = var.spot_instance_pools
spotMaxPrice = var.spot_max_price
tags = local.tags_lt_format
targetGroups = aws_lb_target_group.this.id
subnets = join("\",\"", var.subnet_ids)
}
)
}