-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
executable file
·211 lines (170 loc) · 4.9 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
data "aws_caller_identity" "this" {}
data "aws_region" "current" {}
terraform {
required_version = ">= 0.12"
}
locals {
name = var.name
common_tags = {
"Name" = local.name
"Terraform" = true
"Environment" = var.environment
}
tags = merge(var.tags, local.common_tags)
command = format("aws ec2 wait spot-instance-request-fulfilled --spot-instance-request-ids %s && aws ec2 describe-spot-instance-requests --spot-instance-request-ids %s | jq -r '.SpotInstanceRequests[].InstanceId'", aws_spot_instance_request.this.*.id[0], aws_spot_instance_request.this.*.id[0])
command_chomped = chomp(local.command)
command_when_destroy_chomped = chomp(var.command_when_destroy)
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = [
"ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = [
"hvm"]
}
owners = [
"099720109477"]
# Canonical
}
resource "aws_eip" "this" {
vpc = true
lifecycle {
prevent_destroy = "false"
}
}
resource "aws_eip_association" "this" {
allocation_id = aws_eip.this.id
// instance_id = module.instance_id.stdout
instance_id = chomp(null_resource.contents.triggers["stdout"])
}
resource "aws_ebs_volume" "this" {
availability_zone = var.azs[0]
size = var.ebs_volume_size
type = "gp2"
tags = merge(
local.tags,
{
Name = "ebs-main"
},
)
lifecycle {
prevent_destroy = "false"
}
}
resource "aws_volume_attachment" "this" {
device_name = var.volume_path
volume_id = aws_ebs_volume.this.id
// instance_id = module.instance_id.stdout
instance_id = chomp(null_resource.contents.triggers["stdout"])
force_detach = true
}
data "template_file" "user_data" {
template = file("${path.module}/data/user_data_ubuntu_ebs.sh")
vars = {
log_config_bucket = var.log_config_bucket
log_config_key = var.log_config_key
}
}
resource "aws_spot_instance_request" "this" {
wait_for_fulfillment = true
spot_price = var.spot_price
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
user_data = data.template_file.user_data.rendered
key_name = var.key_name
iam_instance_profile = var.instance_profile_id
subnet_id = var.subnet_id
security_groups = var.security_groups
root_block_device {
volume_type = "gp2"
volume_size = var.root_volume_size
delete_on_termination = true
}
}
//module "instance_id" {
// source = "matti/resource/shell"
// command = format("aws ec2 wait spot-instance-request-fulfilled --spot-instance-request-ids %s && aws ec2 describe-spot-instance-requests --spot-instance-request-ids %s | jq -r '.SpotInstanceRequests[].InstanceId'", aws_spot_instance_request.this.*.id[0], aws_spot_instance_request.this.*.id[0])
//}
resource "null_resource" "wait_on_startup" {
//This will fail on windows as it has a different sleep command - USE WSL ALWAYS
provisioner "local-exec" {
command = "sleep 20"
}
depends_on = [aws_spot_instance_request.this]
}
resource "null_resource" "start" {
triggers = {
depends_id = var.depends_id
}
}
resource "null_resource" "shell" {
triggers = {
string = var.trigger
}
provisioner "local-exec" {
command = "${local.command_chomped} 2>\"${path.module}/stderr.${null_resource.start.id}\" >\"${path.module}/stdout.${null_resource.start.id}\"; echo $? >\"${path.module}/exitstatus.${null_resource.start.id}\""
}
provisioner "local-exec" {
when = destroy
command = local.command_when_destroy_chomped == "" ? ":" : local.command_when_destroy_chomped
}
provisioner "local-exec" {
when = destroy
command = "rm \"${path.module}/stdout.${null_resource.start.id}\""
on_failure = continue
}
provisioner "local-exec" {
when = destroy
command = "rm \"${path.module}/stderr.${null_resource.start.id}\""
on_failure = continue
}
provisioner "local-exec" {
when = destroy
command = "rm \"${path.module}/exitstatus.${null_resource.start.id}\""
on_failure = continue
}
depends_on = [null_resource.wait_on_startup]
}
data "external" "stdout" {
depends_on = [
null_resource.shell]
program = [
"sh",
"${path.module}/read.sh",
"${path.module}/stdout.${null_resource.start.id}"]
}
data "external" "stderr" {
depends_on = [
null_resource.shell]
program = [
"sh",
"${path.module}/read.sh",
"${path.module}/stderr.${null_resource.start.id}"]
}
data "external" "exitstatus" {
depends_on = [
null_resource.shell]
program = [
"sh",
"${path.module}/read.sh",
"${path.module}/exitstatus.${null_resource.start.id}"]
}
resource "null_resource" "contents" {
depends_on = [
null_resource.shell]
triggers = {
stdout = data.external.stdout.result["content"]
stderr = data.external.stderr.result["content"]
exitstatus = data.external.exitstatus.result["content"]
string = var.trigger
}
lifecycle {
ignore_changes = [
triggers]
}
}