-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
main.tf
327 lines (260 loc) · 8.17 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
provider "aws" {
region = local.region
}
locals {
name = "example-ec2-complete"
region = "eu-west-1"
user_data = <<-EOT
#!/bin/bash
echo "Hello Terraform!"
EOT
tags = {
Owner = "user"
Environment = "dev"
}
}
################################################################################
# Supporting Resources
################################################################################
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 3.0"
name = local.name
cidr = "10.99.0.0/18"
azs = ["${local.region}a", "${local.region}b", "${local.region}c"]
public_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"]
private_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"]
database_subnets = ["10.99.7.0/24", "10.99.8.0/24", "10.99.9.0/24"]
tags = local.tags
}
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-hvm-*-x86_64-gp2"]
}
}
module "security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4.0"
name = local.name
description = "Security group for example usage with EC2 instance"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = ["0.0.0.0/0"]
ingress_rules = ["http-80-tcp", "all-icmp"]
egress_rules = ["all-all"]
tags = local.tags
}
resource "aws_placement_group" "web" {
name = local.name
strategy = "cluster"
}
resource "aws_kms_key" "this" {
}
resource "aws_network_interface" "this" {
subnet_id = element(module.vpc.private_subnets, 0)
}
################################################################################
# EC2 Module
################################################################################
module "ec2_disabled" {
source = "../../"
create = false
}
module "ec2_complete" {
source = "../../"
name = local.name
ami = data.aws_ami.amazon_linux.id
instance_type = "c5.4xlarge"
availability_zone = element(module.vpc.azs, 0)
subnet_id = element(module.vpc.private_subnets, 0)
vpc_security_group_ids = [module.security_group.security_group_id]
placement_group = aws_placement_group.web.id
associate_public_ip_address = true
# only one of these can be enabled at a time
hibernation = true
# enclave_options_enabled = true
user_data_base64 = base64encode(local.user_data)
cpu_core_count = 2 # default 4
cpu_threads_per_core = 1 # default 2
capacity_reservation_specification = {
capacity_reservation_preference = "open"
}
enable_volume_tags = false
root_block_device = [
{
encrypted = true
volume_type = "gp3"
throughput = 200
volume_size = 50
tags = {
Name = "my-root-block"
}
},
]
ebs_block_device = [
{
device_name = "/dev/sdf"
volume_type = "gp3"
volume_size = 5
throughput = 200
encrypted = true
kms_key_id = aws_kms_key.this.arn
}
]
tags = local.tags
}
module "ec2_network_interface" {
source = "../../"
name = "${local.name}-network-interface"
ami = data.aws_ami.amazon_linux.id
instance_type = "c5.large"
network_interface = [
{
device_index = 0
network_interface_id = aws_network_interface.this.id
delete_on_termination = false
}
]
tags = local.tags
}
module "ec2_metadata_options" {
source = "../../"
name = "${local.name}-metadata-options"
ami = data.aws_ami.amazon_linux.id
instance_type = "c5.4xlarge"
subnet_id = element(module.vpc.private_subnets, 0)
vpc_security_group_ids = [module.security_group.security_group_id]
metadata_options = {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 8
}
tags = local.tags
}
module "ec2_t2_unlimited" {
source = "../../"
name = "${local.name}-t2-unlimited"
ami = data.aws_ami.amazon_linux.id
instance_type = "t2.micro"
cpu_credits = "unlimited"
subnet_id = element(module.vpc.private_subnets, 0)
vpc_security_group_ids = [module.security_group.security_group_id]
associate_public_ip_address = true
tags = local.tags
}
module "ec2_t3_unlimited" {
source = "../../"
name = "${local.name}-t3-unlimited"
ami = data.aws_ami.amazon_linux.id
instance_type = "t3.micro"
cpu_credits = "unlimited"
subnet_id = element(module.vpc.private_subnets, 0)
vpc_security_group_ids = [module.security_group.security_group_id]
associate_public_ip_address = true
tags = local.tags
}
################################################################################
# EC2 Module - multiple instances with `for_each`
################################################################################
locals {
multiple_instances = {
one = {
instance_type = "t3.micro"
availability_zone = element(module.vpc.azs, 0)
subnet_id = element(module.vpc.private_subnets, 0)
root_block_device = [
{
encrypted = true
volume_type = "gp3"
throughput = 200
volume_size = 50
tags = {
Name = "my-root-block"
}
}
]
}
two = {
instance_type = "t3.small"
availability_zone = element(module.vpc.azs, 1)
subnet_id = element(module.vpc.private_subnets, 1)
root_block_device = [
{
encrypted = true
volume_type = "gp2"
volume_size = 50
}
]
}
three = {
instance_type = "t3.medium"
availability_zone = element(module.vpc.azs, 2)
subnet_id = element(module.vpc.private_subnets, 2)
}
}
}
module "ec2_multiple" {
source = "../../"
for_each = local.multiple_instances
name = "${local.name}-multi-${each.key}"
ami = data.aws_ami.amazon_linux.id
instance_type = each.value.instance_type
availability_zone = each.value.availability_zone
subnet_id = each.value.subnet_id
vpc_security_group_ids = [module.security_group.security_group_id]
enable_volume_tags = false
root_block_device = lookup(each.value, "root_block_device", [])
tags = local.tags
}
################################################################################
# EC2 Module - spot instance request
################################################################################
module "ec2_spot_instance" {
source = "../../"
name = "${local.name}-spot-instance"
create_spot_instance = true
ami = data.aws_ami.amazon_linux.id
instance_type = "c4.4xlarge"
availability_zone = element(module.vpc.azs, 0)
subnet_id = element(module.vpc.private_subnets, 0)
vpc_security_group_ids = [module.security_group.security_group_id]
placement_group = aws_placement_group.web.id
associate_public_ip_address = true
# Spot request specific attributes
spot_price = "0.60"
spot_wait_for_fulfillment = true
spot_type = "persistent"
spot_instance_interruption_behavior = "terminate"
# End spot request specific attributes
user_data_base64 = base64encode(local.user_data)
cpu_core_count = 2 # default 4
cpu_threads_per_core = 1 # default 2
capacity_reservation_specification = {
capacity_reservation_preference = "open"
}
enable_volume_tags = false
root_block_device = [
{
encrypted = true
volume_type = "gp3"
throughput = 200
volume_size = 50
tags = {
Name = "my-root-block"
}
},
]
ebs_block_device = [
{
device_name = "/dev/sdf"
volume_type = "gp3"
volume_size = 5
throughput = 200
encrypted = true
# kms_key_id = aws_kms_key.this.arn # you must grant the AWSServiceRoleForEC2Spot service-linked role access to any custom KMS keys
}
]
tags = local.tags
}