-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
118 lines (92 loc) · 2.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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>5.0"
}
}
}
locals {
tag_name = coalesce(var.tag_name, var.hostname)
volume_mounts = join("\n", [
for device, volume in var.additional_volumes :
templatefile(
"${path.module}/user_data_scripts/mount_volume.sh",
{
device = device
mount_point = volume.mount_point
}
)
]
)
user_data = join("\n", [
var.user_data,
file("${path.module}/user_data_scripts/reboot.sh")
]
)
instance_tags = merge(
{
Name = local.tag_name
Environment = var.tag_environment
Hostname = var.hostname
},
var.additional_tags,
)
}
resource "aws_instance" "instance" {
placement_group = var.placement_group
tags = local.instance_tags
volume_tags = local.instance_tags
instance_type = var.type
ami = var.ami
key_name = var.key_name
disable_api_termination = true
ebs_optimized = true
monitoring = true
iam_instance_profile = var.iam_instance_profile
subnet_id = var.subnet_id
vpc_security_group_ids = var.security_group_ids
user_data = local.volume_mounts == "" ? local.user_data : join("\n", [
local.volume_mounts,
local.user_data
]
)
user_data_replace_on_change = var.user_data_replace_on_change
metadata_options {
http_tokens = "required"
}
root_block_device {
volume_type = "gp3"
volume_size = var.root_volume_size
encrypted = var.encrypt_volumes
}
dynamic "ebs_block_device" {
for_each = var.additional_volumes
content {
device_name = ebs_block_device.value["name"]
volume_type = ebs_block_device.value["type"]
volume_size = ebs_block_device.value["size"]
encrypted = var.encrypt_volumes
}
}
}
data "aws_subnet" "instance" {
id = var.subnet_id
}
resource "aws_ebs_volume" "raid_array" {
count = var.raid_array_size > 0 ? 10 : 0
availability_zone = data.aws_subnet.instance.availability_zone
size = var.raid_array_size / 10
encrypted = var.encrypt_volumes
type = "gp3"
tags = local.instance_tags
lifecycle {
replace_triggered_by = [aws_instance.instance.id]
}
}
resource "aws_volume_attachment" "raid_array" {
count = length(aws_ebs_volume.raid_array.*.id)
volume_id = aws_ebs_volume.raid_array[count.index].id
instance_id = aws_instance.instance.id
device_name = "/dev/sd${substr("fghijklmnopqrstuvwxyz", count.index + 1, 1)}"
}