-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yaml
More file actions
163 lines (158 loc) · 5.77 KB
/
template.yaml
File metadata and controls
163 lines (158 loc) · 5.77 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
discord-bot-minecraft-ec2-serverless
Sample SAM Template for discord-bot-minecraft-ec2-serverless
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 15
Environment:
Variables:
APPLICATION_PUBLIC_KEY: ''
S3_BUCKET: !Ref SamTempBucket
OBJECT_KEY: tmp.json
INSTANCE_ID: !Ref Ec2Instance
REGION: us-west-1
Resources:
InteractionFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: src/
Handler: app.lambda_handler
Runtime: python3.13
Architectures:
- x86_64
MemorySize: 1024
Policies:
- Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
- ec2:StopInstances
- ec2:StartInstances
- ec2:DescribeInstanceStatus
Resource: "*"
Events:
Interaction:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /interactions
Method: post
FollowUpFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: src/
Handler: follow_up.lambda_handler
Runtime: python3.13
Architectures:
- x86_64
Policies:
- Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
- s3:DeleteObject
- ec2:DescribeInstances
Resource: "*"
Events:
EC2StateChange:
Type: EventBridgeRule
Properties:
Pattern:
source:
- "aws.ec2"
detail-type:
- "EC2 Instance State-change Notification"
detail:
state:
- "running"
- "stopped"
SamTempBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Join
- "-"
- - "sam-temp-bucket"
- !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split
- "/"
- !Ref "AWS::StackId"
Ec2Instance:
Type : AWS::EC2::Instance
Properties:
ImageId: ami-00142eb1747a493d9
InstanceType: t2.small
SecurityGroupIds:
- !Ref InstanceSecurityGroup
Tags:
- Key: Name
Value: discord-bot-minecraft-ec2-serverless
UserData:
Fn::Base64: !Sub |
#!/bin/bash
sudo dnf update -y
sudo dnf remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo sed -i 's/$releasever/9/g' /etc/yum.repos.d/docker-ce.repo
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo systemctl enable --now docker
sudo mkdir /mc && sudo cd /mc
sudo echo $'services:\n mc:\n image: itzg/minecraft-server\n tty: true\n stdin_open: true\n ports:\n - "25565:25565"\n environment:\n EULA: "TRUE"\n volumes:\n - ./data:/data\n restart: always' > './docker-compose.yml'
sudo docker compose -p minecraft-server up -d
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 25565
ToPort: 25565
CidrIp: 0.0.0.0/0
- IpProtocol: udp
FromPort: 25565
ToPort: 25565
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: discord-bot-minecraft-ec2-serverless
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
InteractionApi:
Description: "API Gateway endpoint URL for Prod stage for Interaction function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/Prod/interactions"
InteractionFunction:
Description: "Interaction Lambda Function ARN"
Value: !GetAtt InteractionFunction.Arn
InteractionFunctionIamRole:
Description: "Implicit IAM Role created for Interaction function"
Value: !GetAtt InteractionFunctionRole.Arn
FollowUpFunction:
Description: "Follow Up Lambda Function ARN"
Value: !GetAtt FollowUpFunction.Arn
FollowUpFunctionIamRole:
Description: "Implicit IAM Role created for Follow Up function"
Value: !GetAtt FollowUpFunctionRole.Arn
SamTempBucket:
Description: "Sam Temp Bucket ARN"
Value: !GetAtt SamTempBucket.Arn
Ec2Instance:
Description: "Ec2 Instance ARN"
Value: !Ref Ec2Instance
InstanceSecurityGroup:
Description: "Instance Security Group ARN"
Value: !Ref InstanceSecurityGroup