-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
103 lines (87 loc) · 2.66 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
resource "tls_private_key" "primary_server_key" {
algorithm = "RSA"
rsa_bits = 4096
provisioner "local-exec" {
command = "rm ~/.ssh/${var.instance_name}_key.pem || true"
}
}
resource "aws_key_pair" "primary_server_key" {
key_name = "${var.instance_name}-key"
public_key = tls_private_key.primary_server_key.public_key_openssh
provisioner "local-exec" {
command = "echo '${tls_private_key.primary_server_key.private_key_pem}' > ~/.ssh/${var.instance_name}_key.pem && chmod 400 ~/.ssh/${var.instance_name}_key.pem"
}
}
data "aws_vpc" "default" {
default = true
}
resource "aws_security_group" "primary_server_sg" {
name = "${var.instance_name}-sg"
description = "${var.instance_name} instace security group"
vpc_id = data.aws_vpc.default.id
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "CoD2 ports"
from_port = 28960
to_port = 28980
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "CoD2 ports"
from_port = 28960
to_port = 28980
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "primary_server" {
ami = "ami-0ec7f9846da6b0f61"
instance_type = var.instance_type
key_name = aws_key_pair.primary_server_key.key_name
vpc_security_group_ids = [aws_security_group.primary_server_sg.id]
tags = {
Name = var.instance_name
}
root_block_device {
volume_size = var.instance_disk_size
volume_type = "gp2"
encrypted = false
delete_on_termination = true
}
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.primary_server_key.private_key_pem
host = self.public_ip
}
provisioner "file" {
source = "setup/"
destination = "/home/ubuntu/"
}
provisioner "remote-exec" {
inline = [
"chmod +x -R ~/scripts",
"~/scripts/start.sh --mysql_root_password=${var.mysql_root_password} --aws_access_key_id=${var.aws_access_key_id} --aws_secret_access_key=${var.aws_secret_access_key} --s3_bucket_name=${var.s3_bucket_name} --s3_bucket_region=${var.s3_bucket_region} --domain=${var.domain}"
]
}
}