-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverless.yml
191 lines (178 loc) · 5.45 KB
/
serverless.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
service: valheim
plugins:
- serverless-esbuild
package:
individually: true
configValidationMode: error
custom:
prefix: ${self:service}-${sls:stage}
esbuild:
bundle: true
minify: true
sourcemap: true
external:
- '@aws-sdk/signature-v4-crt'
config:
vpc:
id: ${file(./scripts/get-default-vpc.js)}
bucketName: chungsta-valheim
region: ap-southeast-2
instanceType: t3.medium
serverName: bois-server
provider:
name: aws
runtime: nodejs16.x
architecture: arm64
deploymentMethod: direct
region: ${self:custom.config.region}
environment:
REGION: ${self:custom.config.region}
LOG_LEVEL: ${env:LOG_LEVEL, 'info'}
NODE_OPTIONS: --enable-source-maps
NODE_ENV: ${env:NODE_ENV, 'production'}
NODE_TLS_REJECT_UNAUTHORIZED: ${env:NODE_TLS_REJECT_UNAUTHORIZED, ''}
TZ: UTC
iam:
role:
statements:
- Effect: Allow
Action: 's3:PutObject'
Resource: !Sub ${ValheimSaveBucket.Arn}/*
- Effect: Allow
Action:
- ec2:AssociateAddress
- ec2:Describe*
- ec2:RequestSpotInstances
- ec2:RunInstances
- ec2:CreateTags
- ec2:CancelSpotInstanceRequests
- ec2:TerminateInstances
- ec2:ReleaseAddress
- ssm:GetParameter
- ssm:SendCommand
- ssm:GetCommandInvocation
- iam:CreateServiceLinkedRole
Resource: '*'
- Effect: Allow
Action: iam:PassRole
Resource: !Sub ${ValheimRole.Arn}
- Effect: Allow
Action:
- ssm:GetParameter
Resource: 'arn:aws:ssm:${aws:region}::parameter/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
- Effect: Allow
Action: lambda:InvokeFunction
Resource:
- arn:aws:lambda:${aws:region}:${aws:accountId}:function:${self:custom.prefix}-start-server
- arn:aws:lambda:${aws:region}:${aws:accountId}:function:${self:custom.prefix}-stop-server
functions:
# Slack
start-server:
maximumRetryAttempts: 0
memorySize: 256
timeout: 120
environment:
INSTANCE_TYPE: ${self:custom.config.instanceType}
INSTANCE_IAM_ROLE: !Sub ${ValheimInstanceProfile.Arn}
SECURITY_GROUP: !Sub ${ValheimSecurityGroup.GroupId}
ELASTIC_IP: !Ref ElasticIP
ELASTIC_IP_ALLOCATION_ID: !Sub ${ElasticIP.AllocationId}
BUCKET: ${self:custom.config.bucketName}
SERVER_NAME: ${self:custom.config.serverName}
SERVER_PASSWORD: ${env:SERVER_PASSWORD, 'hello-world'}
SERVER_WEBHOOK: ${env:SERVER_WEBHOOK, ''}
handler: src/functions/start-server/handler.handler
package:
patterns:
- src/functions/start-server/launch-template-user-data.sh
- src/functions/start-server/docker-compose.yml
stop-server:
maximumRetryAttempts: 0
environment:
BUCKET: ${self:custom.config.bucketName}
SERVER_WEBHOOK: ${env:SERVER_WEBHOOK, ''}
memorySize: 256
timeout: 120
handler: src/functions/stop-server/handler.handler
interactions:
maximumRetryAttempts: 0
memorySize: 192
environment:
PREFIX: ${self:custom.prefix}
DISCORD_PUBLIC_TOKEN: ${env:DISCORD_PUBLIC_TOKEN}
handler: src/functions/interactions/handler.handler
events:
- http:
method: POST
path: /interactions
resources:
Resources:
ValheimSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Valheim security group
GroupName: valheim
SecurityGroupIngress:
- IpProtocol: udp
FromPort: 2456
ToPort: 2458
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
VpcId: ${self:custom.config.vpc.id}
ValheimSaveBucket:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
AccessControl: Private
BucketName: ${self:custom.config.bucketName}
VersioningConfiguration:
Status: Enabled
LifecycleConfiguration:
Rules:
- Id: BackupsRule
Prefix: valheim/backups
Status: Enabled
ExpirationInDays: 7
NoncurrentVersionExpirationInDays: 7
- Id: SavesRule
Prefix: valheim/saves
Status: Enabled
NoncurrentVersionExpirationInDays: 7
ValheimRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: s3-access
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: 's3:ListBucket'
Resource: !Sub ${ValheimSaveBucket.Arn}
- Effect: Allow
Action: 's3:*Object*'
Resource: !Sub ${ValheimSaveBucket.Arn}/*
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
ValheimInstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Path: '/'
Roles:
- !Ref ValheimRole
ElasticIP:
Type: AWS::EC2::EIP