forked from lukeogg/ubuntu-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
145 lines (128 loc) · 3.89 KB
/
Copy pathmain.tf
File metadata and controls
145 lines (128 loc) · 3.89 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
resource "random_pet" "name" {}
locals {
machine_name = "${var.owner}-${random_pet.name.id}-dev-machine"
generated_key_name = "${var.owner}-${random_pet.name.id}-dev-key-pair"
}
resource "aws_instance" "ubuntu-dev-machine" {
ami = var.ami
instance_type = var.instance_type
availability_zone = "us-west-2c"
iam_instance_profile = "kaptain-dev-machine-instance-profile"
tags = {
owner = var.owner
expiration = var.expiration
Name = local.machine_name
}
key_name = local.generated_key_name
vpc_security_group_ids = [aws_security_group.main.id]
# use this to increase the ebs volume size and iops
ebs_block_device {
device_name = "/dev/sda1"
volume_type = "gp3"
volume_size = var.base_volume_size
iops = 16000
throughput = 1000
}
provisioner "file" {
source = "install.bash"
destination = "/home/ubuntu/install.bash"
connection {
type = "ssh"
user = "ubuntu"
agent = false
host = self.public_dns
private_key = tls_private_key.dev_key.private_key_pem
timeout = "2m"
}
}
provisioner "remote-exec" {
inline = [
"chmod +x /home/ubuntu/install.bash",
"sudo /home/ubuntu/install.bash",
]
connection {
type = "ssh"
user = "ubuntu"
agent = false
host = self.public_dns
private_key = tls_private_key.dev_key.private_key_pem
timeout = "5m"
}
}
}
### Second Drive
# resource "aws_ebs_volume" "ubuntu-dev-machine-volume" {
# #count = var.control_plane_imagefs_volume_enabled ? var.control_plane_count : 0
# availability_zone = "us-west-2c"
# type = var.volume_type
# size = var.volume_size
# #encrypted = var.control_plane_kms_key_id != "" ? true : false
# #kms_key_id = var.control_plane_kms_key_id
# tags = {
# owner = var.owner
# expiration = var.expiration
# Name = "${local.machine_name}-volume"
# }
# }
# resource "aws_volume_attachment" "control_plane_imagefs" {
# #count = var.control_plane_imagefs_volume_enabled ? var.control_plane_count : 0
# device_name = "/dev/${var.volume_device}"
# volume_id = aws_ebs_volume.ubuntu-dev-machine-volume.id
# instance_id = aws_instance.ubuntu-dev-machine.id
# force_detach = true
# }
resource "aws_security_group" "main" {
name = "${local.machine_name}-sg"
description = "Allow inbound SSH to manage machine"
egress = [
{
cidr_blocks = [ "0.0.0.0/0", ]
description = ""
from_port = 0
ipv6_cidr_blocks = []
prefix_list_ids = []
protocol = "-1"
security_groups = []
self = false
to_port = 0
}
]
ingress = [
{
cidr_blocks = [ "0.0.0.0/0", ]
description = ""
from_port = 22
ipv6_cidr_blocks = []
prefix_list_ids = []
protocol = "tcp"
security_groups = []
self = false
to_port = 22
}
]
}
resource "tls_private_key" "dev_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = local.generated_key_name
public_key = tls_private_key.dev_key.public_key_openssh
provisioner "local-exec" { # Generate "terraform-key-pair.pem" in current directory
command = <<-EOT
echo '${tls_private_key.dev_key.private_key_pem}' > ./'${local.generated_key_name}'.pem
chmod 400 ./'${local.generated_key_name}'.pem
EOT
}
}
### The Ansible inventory
resource "local_file" "AnsibleInventory" {
content = templatefile("inventory.tmpl", {
private-dns = aws_instance.ubuntu-dev-machine.*.public_dns,
private-ip = aws_instance.ubuntu-dev-machine.*.public_ip,
private-id = aws_instance.ubuntu-dev-machine.*.id
key-file-path = "${local.generated_key_name}.pem"
}
)
filename = "inventory"
}