-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
146 lines (116 loc) · 3.99 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
locals {
enabled = var.enabled == "true"
}
data "aws_ami" "search" {
most_recent = true
filter {
name = "virtualization-type"
values = ["hvm"]
}
name_regex = lookup(var.amis_os_map_regex, var.os)
owners = [length(var.amis_primary_owners) == 0 ? lookup(var.amis_os_map_owners, var.os) : var.amis_primary_owners]
}
resource "aws_instance" "this" {
count = local.enabled ? 1 : 0
ami = data.aws_ami.search.id
associate_public_ip_address = var.associate_public_ip_address
disable_api_termination = var.disable_api_termination
ebs_optimized = var.ebs_optimized
iam_instance_profile = var.aws_iam_instance_profile
instance_type = var.instance_type
monitoring = var.monitoring
subnet_id = var.subnet_id
tags = var.tags
vpc_security_group_ids = var.vpc_security_group_ids
key_name = var.key_name
user_data = var.user_data
root_block_device {
volume_size = var.root_volume_size
volume_type = var.volume_type
encrypted = var.root_volume_encryption
tags = var.tags
}
lifecycle {
ignore_changes = [ami,ebs_block_device,root_block_device]
}
}
resource "aws_ebs_volume" "this" {
count = var.enabled_ebs_volume1 ? 1 : 0
size = var.ebs_volume1_size
type = var.volume_type
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
}
resource "aws_volume_attachment" "this" {
count = var.enabled_ebs_volume1 ? 1 : 0
device_name = "/dev/sdh"
volume_id = aws_ebs_volume.this.*.id[0]
instance_id = aws_instance.this.*.id[0]
lifecycle {
ignore_changes = [instance_id,volume_id]
}
}
resource "aws_ebs_volume" "vol2" {
count = var.enabled_ebs_volume2 ? 1 : 0
size = var.ebs_volume2_size
type = var.volume_type
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
}
resource "aws_volume_attachment" "attachment2" {
count = var.enabled_ebs_volume2 ? 1 : 0
device_name = "/dev/sdf"
volume_id = aws_ebs_volume.vol2.*.id[0]
instance_id = aws_instance.this.*.id[0]
lifecycle {
ignore_changes = [instance_id,volume_id]
}
}
resource "aws_ebs_volume" "vol3" {
count = var.enabled_ebs_volume2 ? 1 : 0
size = var.ebs_volume2_size
type = var.volume_type
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
}
resource "aws_volume_attachment" "attachment3" {
count = var.enabled_ebs_volume3 ? 1 : 0
device_name = "/dev/sdj"
volume_id = aws_ebs_volume.vol3.*.id[0]
instance_id = aws_instance.this.*.id[0]
lifecycle {
ignore_changes = [instance_id,volume_id]
}
}
resource "aws_ebs_volume" "vol4" {
count = var.enabled_ebs_volume4 ? 1 : 0
size = var.ebs_volume4_size
type = var.volume_type
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
}
resource "aws_volume_attachment" "attachment4" {
count = var.enabled_ebs_volume4 ? 1 : 0
device_name = "/dev/sdi"
volume_id = aws_ebs_volume.vol4.*.id[0]
instance_id = aws_instance.this.*.id[0]
lifecycle {
ignore_changes = [instance_id,volume_id]
}
}
resource "aws_ebs_volume" "vol5" {
count = var.enabled_ebs_volume5 ? 1 : 0
size = var.ebs_volume5_size
type = var.volume_type
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
}
resource "aws_volume_attachment" "attachment5" {
count = var.enabled_ebs_volume5 ? 1 : 0
device_name = "/dev/sdk"
volume_id = aws_ebs_volume.vol5.*.id[0]
instance_id = aws_instance.this.*.id[0]
lifecycle {
ignore_changes = [instance_id,volume_id]
}
}