Repository navigation
Expand file tree
/
Copy pathec2_spot_instance.tf
More file actions
164 lines (135 loc) · 4.03 KB
/
Copy pathec2_spot_instance.tf
File metadata and controls
164 lines (135 loc) · 4.03 KB
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
locals {
availability_zone = format("%s%s", var.region, var.availability_zone)
}
# Create a VPC
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
tags = {
"created_by" = "terraform",
}
}
resource "aws_subnet" "subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.10.0/24"
availability_zone = local.availability_zone
tags = {
"name" = "aws-subnet",
"created_by" = "terraform",
}
}
resource "aws_ebs_volume" "ebs_volume" {
for_each = {
for k, v in var.ec2_instance_settings : k => v if v.ebs_volume
}
availability_zone = local.availability_zone
size = 20
type = "gp3"
tags = {
"purpose" = each.key,
"created_by" = "terraform",
}
}
resource "aws_key_pair" "key_pair" {
key_name = var.public_key["key_name"]
public_key = var.public_key["public_key"]
tags = {
"created_by" = "terraform"
}
}
resource "aws_spot_instance_request" "spot_instance_request" {
for_each = {
for k, v in var.ec2_instance_settings : k => v
if v.enabled
}
# General settings
ami = each.value.ami
instance_type = each.value.instance_type
spot_price = each.value.spot_price
availability_zone = local.availability_zone
key_name = aws_key_pair.key_pair.key_name
private_ip = cidrhost(aws_subnet.subnet.cidr_block, each.value.hostnum)
associate_public_ip_address = true
subnet_id = aws_subnet.subnet.id
vpc_security_group_ids = [aws_security_group.security_group.id]
# Spot settings
spot_type = "persistent"
instance_interruption_behavior = "stop"
wait_for_fulfillment = true
credit_specification {
cpu_credits = "standard"
}
capacity_reservation_specification {
capacity_reservation_preference = "open"
}
tags = {
"purpose" = each.key,
"created_by" = "terraform",
}
}
resource "aws_volume_attachment" "volume_attachment" {
for_each = {
for k, v in var.ec2_instance_settings : k => v
if v.enabled && v.ebs_volume
}
device_name = "/dev/sdh"
volume_id = aws_ebs_volume.ebs_volume[each.key].id
instance_id = aws_spot_instance_request.spot_instance_request[each.key].spot_instance_id
}
resource "aws_internet_gateway" "internet_gateway" {
vpc_id = aws_vpc.vpc.id
tags = {
"created_by" = "terraform"
}
}
resource "aws_route" "internet_access" {
route_table_id = aws_vpc.vpc.main_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.internet_gateway.id
}
# A security group for the ELB so it is accessible via the VM
resource "aws_security_group" "security_group" {
description = "Allow connection between NLB and target"
vpc_id = aws_vpc.vpc.id
tags = {
"created_by" = "terraform"
}
}
resource "aws_security_group_rule" "ingress_ssh" {
security_group_id = aws_security_group.security_group.id
from_port = 22
to_port = 22
protocol = "tcp"
type = "ingress"
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "ingress_minecraft" {
security_group_id = aws_security_group.security_group.id
from_port = 25565
to_port = 25565
protocol = "tcp"
type = "ingress"
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "egress" {
security_group_id = aws_security_group.security_group.id
from_port = 0
to_port = 0
protocol = "all"
type = "egress"
cidr_blocks = ["0.0.0.0/0"]
}
data "aws_route53_zone" "route53_zone" {
zone_id = var.zone_id
}
resource "aws_route53_record" "a_record" {
for_each = {
for k, v in var.ec2_instance_settings : k => v
if v.enabled
}
zone_id = data.aws_route53_zone.route53_zone.zone_id
name = each.key
type = "A"
ttl = "300"
records = [aws_spot_instance_request.spot_instance_request[each.key].public_ip]
}