-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
183 lines (160 loc) · 5.22 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
locals {
enabled = var.enabled == "true"
}
#provider "aws" {
# region = var.region
#}
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 block for ec2 #
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.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,associate_public_ip_address]
ignore_changes = [ami,associate_public_ip_address]
}
metadata_options {
http_endpoint = "enabled"
http_put_response_hop_limit = 1
http_tokens = var.metadata_http_tokens
instance_metadata_tags = "disabled"
}
}
# resource block for eip #
resource "aws_eip" "this" {
count = var.enabled_eip ? 1 : 0
# domain = "vpc"
tags = var.tags
}
# resource block for ec2 and eip association #
resource "aws_eip_association" "eip_assoc" {
count = var.enabled_eip ? 1 : 0
instance_id = aws_instance.this.*.id[0]
allocation_id = aws_eip.this.*.id[0]
}
# resource block for ebs volumes #
resource "aws_ebs_volume" "this" {
count = var.enabled_ebs_volume1 ? 1 : 0
size = var.ebs_volume1_size
type = var.volume_type
snapshot_id = var.snapshot_id_volume1
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
lifecycle {
ignore_changes = [availability_zone]
}
}
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
snapshot_id = var.snapshot_id_volume2
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
lifecycle {
ignore_changes = [availability_zone]
}
}
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_volume3_size
type = var.volume_type
snapshot_id = var.snapshot_id_volume3
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
lifecycle {
ignore_changes = [availability_zone]
}
}
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
snapshot_id = var.snapshot_id_volume4
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
lifecycle {
ignore_changes = [availability_zone]
}
}
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
snapshot_id = var.snapshot_id_volume5
availability_zone = aws_instance.this.*.availability_zone[0]
tags = var.tags
lifecycle {
ignore_changes = [availability_zone]
}
}
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]
}
}