-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
97 lines (81 loc) · 2.07 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
resource "tls_private_key" "runner_key" {
count = var.create_ssh_key ? 1 : 0
algorithm = "RSA"
rsa_bits = "8192"
}
resource "aws_key_pair" "runner_key" {
key_name = "ssh-key-ci-runner"
public_key = local.public_key
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = [var.arch]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "runner" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
key_name = aws_key_pair.runner_key.key_name
associate_public_ip_address = true
tags = {
Name = "GitLab CI runner"
}
}
resource "null_resource" "bootstrap_runner" {
depends_on = [tls_private_key.runner_key]
triggers = {
host = aws_instance.runner.public_ip
private_key = local.private_key
}
connection {
type = "ssh"
host = self.triggers.host
user = "ubuntu"
private_key = self.triggers.private_key
}
provisioner "remote-exec" {
inline = [
file(format("%s/files/docker.sh", path.module)),
]
}
provisioner "file" {
source = format("%s/files/daemon.json", path.module)
destination = "/home/ubuntu/daemon.json"
}
provisioner "remote-exec" {
inline = [
"sudo mkdir -p /etc/docker",
"sudo mv /home/ubuntu/daemon.json /etc/docker/daemon.json",
"sudo chown 0:0 /etc/docker/daemon.json",
"sudo chmod 0644 /etc/docker/daemon.json"
]
}
provisioner "remote-exec" {
inline = [
"sudo systemctl daemon-reload",
"sudo systemctl restart docker"
]
}
provisioner "remote-exec" {
inline = [
templatefile(format("%s/files/runner.tpl", path.module), local.runner_params),
]
}
provisioner "remote-exec" {
when = destroy
inline = [
"docker exec -it runner gitlab-runner unregister --all-runners",
]
}
}