This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 485
/
main.tf
265 lines (216 loc) · 10.2 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
# ----------------------------------------------------------------------------------------------------------------------
# REQUIRE A SPECIFIC TERRAFORM VERSION OR HIGHER
# ----------------------------------------------------------------------------------------------------------------------
terraform {
# This module is now only being tested with Terraform 1.0.x. However, to make upgrading easier, we are setting
# 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it
# forwards compatible with 1.0.x code.
required_version = ">= 0.12.26"
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE AN AUTO SCALING GROUP (ASG) TO RUN CONSUL
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_autoscaling_group" "autoscaling_group" {
name_prefix = var.cluster_name
launch_configuration = aws_launch_configuration.launch_configuration.name
availability_zones = var.availability_zones
vpc_zone_identifier = var.subnet_ids
# Run a fixed number of instances in the ASG
min_size = var.cluster_size
max_size = var.cluster_size
desired_capacity = var.cluster_size
termination_policies = [var.termination_policies]
health_check_type = var.health_check_type
health_check_grace_period = var.health_check_grace_period
wait_for_capacity_timeout = var.wait_for_capacity_timeout
service_linked_role_arn = var.service_linked_role_arn
enabled_metrics = var.enabled_metrics
protect_from_scale_in = var.protect_from_scale_in
tags = flatten(
[
{
key = "Name"
value = var.cluster_name
propagate_at_launch = true
},
{
key = var.cluster_tag_key
value = var.cluster_tag_value
propagate_at_launch = true
},
var.tags,
]
)
dynamic "initial_lifecycle_hook" {
for_each = var.lifecycle_hooks
content {
name = initial_lifecycle_hook.key
default_result = lookup(initial_lifecycle_hook.value, "default_result", null)
heartbeat_timeout = lookup(initial_lifecycle_hook.value, "heartbeat_timeout", null)
lifecycle_transition = initial_lifecycle_hook.value.lifecycle_transition
notification_metadata = lookup(initial_lifecycle_hook.value, "notification_metadata", null)
notification_target_arn = lookup(initial_lifecycle_hook.value, "notification_target_arn", null)
role_arn = lookup(initial_lifecycle_hook.value, "role_arn", null)
}
}
lifecycle {
# As of AWS Provider 3.x, inline load_balancers and target_group_arns
# in an aws_autoscaling_group take precedence over attachment resources.
# Since the consul-cluster module does not define any Load Balancers,
# it's safe to assume that we will always want to favor an attachment
# over these inline properties.
#
# For further discussion and links to relevant documentation, see
# https://github.com/hashicorp/terraform-aws-vault/issues/210
ignore_changes = [load_balancers, target_group_arns]
}
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE LAUNCH CONFIGURATION TO DEFINE WHAT RUNS ON EACH INSTANCE IN THE ASG
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_launch_configuration" "launch_configuration" {
name_prefix = "${var.cluster_name}-"
image_id = var.ami_id
instance_type = var.instance_type
user_data = var.user_data
spot_price = var.spot_price
iam_instance_profile = var.enable_iam_setup ? element(
concat(aws_iam_instance_profile.instance_profile.*.name, [""]),
0,
) : var.iam_instance_profile_name
key_name = var.ssh_key_name
security_groups = concat(
[aws_security_group.lc_security_group.id],
var.additional_security_group_ids,
)
placement_tenancy = var.tenancy
associate_public_ip_address = var.associate_public_ip_address
ebs_optimized = var.root_volume_ebs_optimized
root_block_device {
volume_type = var.root_volume_type
volume_size = var.root_volume_size
delete_on_termination = var.root_volume_delete_on_termination
encrypted = var.root_volume_encrypted
}
# Important note: whenever using a launch configuration with an auto scaling group, you must set
# create_before_destroy = true. However, as soon as you set create_before_destroy = true in one resource, you must
# also set it in every resource that it depends on, or you'll get an error about cyclic dependencies (especially when
# removing resources). For more info, see:
#
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
# https://terraform.io/docs/configuration/resources.html
lifecycle {
create_before_destroy = true
}
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE A SECURITY GROUP TO CONTROL WHAT REQUESTS CAN GO IN AND OUT OF EACH EC2 INSTANCE
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_security_group" "lc_security_group" {
name_prefix = var.cluster_name
description = "Security group for the ${var.cluster_name} launch configuration"
vpc_id = var.vpc_id
# aws_launch_configuration.launch_configuration in this module sets create_before_destroy to true, which means
# everything it depends on, including this resource, must set it as well, or you'll get cyclic dependency errors
# when you try to do a terraform destroy.
lifecycle {
create_before_destroy = true
}
tags = merge(
{
"Name" = var.cluster_name
},
var.security_group_tags,
)
}
resource "aws_security_group_rule" "allow_ssh_inbound" {
count = length(var.allowed_ssh_cidr_blocks) >= 1 ? 1 : 0
type = "ingress"
from_port = var.ssh_port
to_port = var.ssh_port
protocol = "tcp"
cidr_blocks = var.allowed_ssh_cidr_blocks
security_group_id = aws_security_group.lc_security_group.id
}
resource "aws_security_group_rule" "allow_ssh_inbound_from_security_group_ids" {
count = var.allowed_ssh_security_group_count
type = "ingress"
from_port = var.ssh_port
to_port = var.ssh_port
protocol = "tcp"
source_security_group_id = element(var.allowed_ssh_security_group_ids, count.index)
security_group_id = aws_security_group.lc_security_group.id
}
resource "aws_security_group_rule" "allow_all_outbound" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.lc_security_group.id
}
# ---------------------------------------------------------------------------------------------------------------------
# THE CONSUL-SPECIFIC INBOUND/OUTBOUND RULES COME FROM THE CONSUL-SECURITY-GROUP-RULES MODULE
# ---------------------------------------------------------------------------------------------------------------------
module "security_group_rules" {
source = "../consul-security-group-rules"
security_group_id = aws_security_group.lc_security_group.id
allowed_inbound_cidr_blocks = var.allowed_inbound_cidr_blocks
allowed_inbound_security_group_ids = var.allowed_inbound_security_group_ids
allowed_inbound_security_group_count = var.allowed_inbound_security_group_count
server_rpc_port = var.server_rpc_port
cli_rpc_port = var.cli_rpc_port
serf_lan_port = var.serf_lan_port
serf_wan_port = var.serf_wan_port
http_api_port = var.http_api_port
https_api_port = var.https_api_port
dns_port = var.dns_port
enable_https_port = var.enable_https_port
}
# ---------------------------------------------------------------------------------------------------------------------
# ATTACH AN IAM ROLE TO EACH EC2 INSTANCE
# We can use the IAM role to grant the instance IAM permissions so we can use the AWS CLI without having to figure out
# how to get our secret AWS access keys onto the box.
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_iam_instance_profile" "instance_profile" {
count = var.enable_iam_setup ? 1 : 0
name_prefix = var.cluster_name
path = var.instance_profile_path
role = element(concat(aws_iam_role.instance_role.*.name, [""]), 0)
# aws_launch_configuration.launch_configuration in this module sets create_before_destroy to true, which means
# everything it depends on, including this resource, must set it as well, or you'll get cyclic dependency errors
# when you try to do a terraform destroy.
lifecycle {
create_before_destroy = true
}
}
resource "aws_iam_role" "instance_role" {
count = var.enable_iam_setup ? 1 : 0
name_prefix = var.cluster_name
assume_role_policy = data.aws_iam_policy_document.instance_role.json
permissions_boundary = var.iam_permissions_boundary
# aws_iam_instance_profile.instance_profile in this module sets create_before_destroy to true, which means
# everything it depends on, including this resource, must set it as well, or you'll get cyclic dependency errors
# when you try to do a terraform destroy.
lifecycle {
create_before_destroy = true
}
}
data "aws_iam_policy_document" "instance_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
# ---------------------------------------------------------------------------------------------------------------------
# THE IAM POLICIES COME FROM THE CONSUL-IAM-POLICIES MODULE
# ---------------------------------------------------------------------------------------------------------------------
module "iam_policies" {
source = "../consul-iam-policies"
enabled = var.enable_iam_setup
iam_role_id = element(concat(aws_iam_role.instance_role.*.id, [""]), 0)
}