-
Notifications
You must be signed in to change notification settings - Fork 0
/
infra.yml
192 lines (169 loc) · 6.5 KB
/
infra.yml
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
AWSTemplateFormatVersion: 2010-09-09
Description: Valheim server
Parameters:
ServerName:
Type: String
Description: The public name for your Valheim dedicated server
Default: "Dedicated Valheim server"
AMI:
Type: String
Description: The Base image for your server, defaults to Amazon AMI eu-west-2
Default: "ami-0ffd774e02309201f"
ServerPassword:
Type: String
Description: The password for your Valheim dedicated server
EmailAddress:
Type: String
Description: An email address to use for server change notifications. (restarted|updated|crashed|backup)
S3BucketName:
Type: String
Description: The unique name of an S3 bucket to restore and backup to
Resources:
EC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.medium
IamInstanceProfile: !Ref InstanceProfile
SecurityGroups:
- Ref: InstanceSecurityGroup
ImageId: !Ref AMI
UserData:
Fn::Base64: !Sub |
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
# Update server and install dependencies
yum update -y
yum install epel-release -y
yum install curl wget tar bzip2 gzip unzip python3 binutils bc jq tmux glibc.i686 libstdc++ libstdc++.i686 -y
# Create new user to use
adduser vhserver
su - vhserver
cd /home/vhserver
# Use LinuxGSM automated install
sudo -u vhserver wget -O linuxgsm.sh https://linuxgsm.sh --output-file=wget_error.log
sudo -u vhserver chmod +x linuxgsm.sh
sudo -u vhserver bash linuxgsm.sh vhserver
# Install Valheim
sudo -u vhserver /home/vhserver/vhserver auto-install
# Configure the server with our parameters
echo 'startparameters="-name '${ServerName}' -password ${ServerPassword} -port 2456 -world '${ServerName}' -public 1"' >> /home/vhserver/lgsm/config-lgsm/vhserver/vhserver.cfg
# Create cron tasks
touch /tmp/crontab_template
touch /tmp/global_crontab_template
echo 'MAILTO="${EmailAddress}"' >> /tmp/crontab_template
echo '*/5 * * * * /home/vhserver/vhserver monitor > /dev/null 2>&1' >> /tmp/crontab_template
echo '*/30 * * * * /home/vhserver/vhserver update > /dev/null 2>&1' >> /tmp/crontab_template
echo '0 0 * * 0 /home/vhserver/vhserver update-lgsm > /dev/null 2>&1' >> /tmp/crontab_template
# Setup Automated Half Hourly Backups
if [[ -z "${S3BucketName}" ]]; then
echo 'No S3 Bucket Set - Skipping Backup Setup'
else
touch /home/vhserver/backup_worlds.sh
chmod +x /home/vhserver/backup_worlds.sh
echo '#!/bin/sh' >> /home/vhserver/backup_worlds.sh
echo '' >> /home/vhserver/backup_worlds.sh
echo 'aws s3 cp /home/vhserver/.config/unity3d/IronGate/Valheim/worlds s3://${S3BucketName}/vhserver/ --recursive' >> /home/vhserver/backup_worlds.sh
echo 'MAILTO="${EmailAddress}"' >> /tmp/global_crontab_template
echo '*/30 * * * * /home/vhserver/backup_worlds.sh > /dev/null 2>&1' >> /tmp/global_crontab_template
fi
# Restore Existing Worlds / From Backup
if [[ -z "${S3BucketName}" ]]; then
echo 'NO S3 Bucket Set - Skipping Restore Setup'
else
aws s3 cp s3://${S3BucketName}/vhserver/ /home/vhserver/.config/unity3d/IronGate/Valheim/worlds --recursive --exclude "*" --include "${ServerName}.*"
fi
# Ensure vhserver owns all files and folders
sudo chown -R vhserver:vhserver /home/vhserver
# Add cron tasks
sudo -u vhserver crontab /tmp/crontab_template
sudo crontab /tmp/global_crontab_template
# Start the server
sudo -u vhserver /home/vhserver/vhserver start
# Check everything is working
sudo -u vhserver /home/vhserver/vhserver monitor
Tags:
- Key: Name
Value: Valheim-EC2Instance
# Create security rules for the instance
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Valheim inbound and outbound security
SecurityGroupIngress:
- Description: Valheim default UDP ports.
IpProtocol: udp
FromPort: 2456
ToPort: 2458
CidrIp: 0.0.0.0/0
- Description: SSH traffic
IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: Valheim-EC2SecurityGroup
# Create and associate an elastic IP to the instance
ElasticIPAddress:
DependsOn: EC2Instance
Type: AWS::EC2::EIP
Properties:
InstanceId: !Ref EC2Instance
Tags:
- Key: Name
Value: Valheim-EC2ElasticIPAddress
# Create a role for the instance
InstanceRole:
Type: AWS::IAM::Role
Properties:
RoleName: Valheim-IAMRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: DistAccess
PolicyDocument:
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
Resource:
- !Sub arn:aws:s3::*:${S3BucketName}/*
Tags:
- Key: Name
Value: Valheim-IAMRole
# Create an IAM profile for the instance
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
InstanceProfileName: Valheim-IAMInstanceProfile
Path: /
Roles:
- !Ref InstanceRole
Outputs:
FullServerAddress:
Description: The full address you can use to connect to your server
Value: !Sub "${EC2Instance.PublicIp}:2456"