-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
190 lines (157 loc) · 5.08 KB
/
main.tf
File metadata and controls
190 lines (157 loc) · 5.08 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
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
provider "google" {
project = var.project_id
region = var.region
zone = var.zone
}
# Service account for server
resource "google_service_account" "factorio_sa" {
account_id = "factorio-server-sa"
display_name = "Factorio Server Service Account"
}
# Custom role with minimal permissions for server management
resource "google_project_iam_custom_role" "factorio_operator" {
role_id = "factorioServerOperator"
title = "Factorio Server Operator"
description = "Minimal permissions to start/stop Factorio server"
permissions = [
"compute.instances.get",
"compute.instances.start",
"compute.instances.stop",
"compute.instances.list",
"compute.zones.get",
"compute.zones.list"
]
}
# Service account for server management (start/stop scripts)
resource "google_service_account" "factorio_management_sa" {
account_id = "factorio-management-sa"
display_name = "Factorio Server Management Service Account"
}
# Grant custom role instead of broad instanceAdmin role
resource "google_project_iam_member" "factorio_management_custom" {
project = var.project_id
role = google_project_iam_custom_role.factorio_operator.id
member = "serviceAccount:${google_service_account.factorio_management_sa.email}"
}
# Static IP for consistent server address
resource "google_compute_address" "factorio_ip" {
name = "factorio-server-ip"
region = var.region
address_type = "EXTERNAL"
}
# VPC Network
resource "google_compute_network" "factorio_vpc" {
name = "factorio-vpc"
auto_create_subnetworks = false
mtu = 1460
}
# Subnet
resource "google_compute_subnetwork" "factorio_subnet" {
name = "factorio-subnet"
ip_cidr_range = "10.0.1.0/24"
region = var.region
network = google_compute_network.factorio_vpc.id
private_ip_google_access = true
}
# Firewall rules for Factorio
resource "google_compute_firewall" "factorio_game" {
name = "allow-factorio-game"
network = google_compute_network.factorio_vpc.name
allow {
protocol = "udp"
ports = ["34197"]
}
allow {
protocol = "tcp"
ports = ["27015"] # RCON
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["factorio-server"]
}
# HTTP API access for remote management
resource "google_compute_firewall" "factorio_http_api" {
name = "allow-factorio-http-api"
network = google_compute_network.factorio_vpc.name
allow {
protocol = "tcp"
ports = ["8080"] # HTTP API
}
source_ranges = ["0.0.0.0/0"] # Public access
target_tags = ["factorio-server"]
}
# SSH access (restricted to Google Cloud IAP)
resource "google_compute_firewall" "ssh_admin" {
name = "allow-ssh-admin"
network = google_compute_network.factorio_vpc.name
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["35.235.240.0/20"] # Google Cloud IAP
target_tags = ["factorio-server"]
}
# Main server instance
resource "google_compute_instance" "factorio_server" {
name = "factorio-server"
machine_type = var.machine_type
zone = var.zone
tags = ["factorio-server"]
boot_disk {
initialize_params {
image = "cos-cloud/cos-stable"
size = 20
type = "pd-balanced"
}
}
network_interface {
network = google_compute_network.factorio_vpc.id
subnetwork = google_compute_subnetwork.factorio_subnet.id
access_config {
nat_ip = google_compute_address.factorio_ip.address
}
}
service_account {
email = google_service_account.factorio_sa.email
scopes = ["cloud-platform"]
}
# FIX: Add startup script to handle permissions before containers start
metadata_startup_script = <<-EOF
#!/bin/bash
echo "=== Factorio Server Startup Script ==="
# Wait for the stateful partition to be available
echo "Waiting for stateful partition..."
while [ ! -d "/mnt/stateful_partition" ]; do
echo "Waiting for /mnt/stateful_partition to be available..."
sleep 2
done
# Create and fix permissions for factorio data directory
echo "Setting up factorio data directory..."
mkdir -p /mnt/stateful_partition/factorio/{saves,config,mods}
# Set correct ownership for factorio user (UID 845, GID 845)
chown -R 845:845 /mnt/stateful_partition/factorio
chmod -R 755 /mnt/stateful_partition/factorio
echo "✅ Factorio data directory permissions set correctly"
echo "Directory ownership:"
ls -la /mnt/stateful_partition/ | grep factorio
echo "=== Startup script completed ==="
EOF
metadata = {
gce-container-declaration = templatefile("${path.module}/container-spec.yaml", {
server_name = var.server_name
server_description = var.server_description
max_players = var.max_players
admin_users = jsonencode(var.admin_users)
autosave_interval = var.autosave_interval
autosave_slots = var.autosave_slots
})
}
labels = {
container-vm = "cos-stable"
}
scheduling {
preemptible = false
automatic_restart = true
}
# Allow stopping for cost savings
allow_stopping_for_update = true
}