Skip to content

Instantly share code, notes, and snippets.

@snakeye
Last active June 8, 2022 14:46
Show Gist options
  • Save snakeye/3b3004cdc3cbb9a9498d0b4e8f7fa33e to your computer and use it in GitHub Desktop.
Save snakeye/3b3004cdc3cbb9a9498d0b4e8f7fa33e to your computer and use it in GitHub Desktop.
CloudFormation template to spin up the Minecraft server
---
AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template for Minecraft server
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
InstanceType:
Description: WebServer EC2 instance type
Type: String
Default: t2.small
AllowedValues:
- t2.micro
- t2.small
- t2.medium
- t2.large
- t2.xlarge
- t3.micro
- t3.small
- t3.large
- t3.xlarge
ConstraintDescription: must be a valid EC2 instance type.
SSHLocation:
Description: The IP address range that can be used to SSH to the EC2 instances
Type: String
MinLength: 9
MaxLength: 18
Default: 0.0.0.0/0
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
ServerVersion:
Type: String
Description: Minecraft Server version
Default: "1.19"
AllowedValues:
- "1.19"
- "1.18.2"
Motd:
Type: String
Default: A Minecraft Server
Description: Sever MOTD
GameMode:
Type: String
Default: survival
AllowedValues:
- survival
- creative
- adventure
Description: Minecraft game mode
LevelSeed:
Type: String
Description: The seed for initial world generation
Mappings:
MinecraftServerDownloadUrls:
'1.18.2':
Url: https://launcher.mojang.com/v1/objects/c8f83c5655308435b3dcf03c06d9fe8740a77469/server.jar
'1.19':
Url: https://launcher.mojang.com/v1/objects/e00c4052dac1d59a1188b2aa9d5a87113aaf1122/server.jar
Resources:
MinecraftServerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable Minecraft server port + SSH access
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '25565'
ToPort: '25565'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp:
Ref: SSHLocation
MinecraftServerInstance:
Type: AWS::EC2::Instance
Metadata:
Comment1: Install minecraft
AWS::CloudFormation::Init:
configSets:
InstallMinecraft:
- install_packages
- install_minecraft
install_packages:
packages:
yum:
java-17-amazon-corretto-headless: []
install_minecraft:
users:
minecraft:
uid: "1001"
files:
'/etc/systemd/system/minecraft.service':
content:
Fn::Join:
- "\n"
- - "[Unit]"
- "Description=Minecraft Server"
- "After=network.target"
- ""
- "[Service]"
- "User=minecraft"
- "Nice=5"
- "KillMode=none"
- "SuccessExitStatus=0 1"
- "InaccessibleDirectories=/root /sys /srv /media -/lost+found"
- "NoNewPrivileges=true"
- "WorkingDirectory=/opt/minecraft/server"
- "ReadWriteDirectories=/opt/minecraft/server"
- "ExecStart=/usr/bin/java -Xmx1024M -Xms1024M -jar server.jar nogui"
- "ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password stop"
- ""
- "[Install]"
- "WantedBy=multi-user.target"
'/opt/minecraft/server/eula.txt':
content: "eula=TRUE"
'/opt/minecraft/server/server.properties':
content:
Fn::Join:
- ''
- - "enable-jmx-monitoring=false\n"
- "rcon.port=25575\n"
- Fn::Join:
- ''
- - "level-seed="
- Ref: LevelSeed
- "\n"
- Fn::Join:
- ''
- - "gamemode="
- Ref: GameMode
- "\n"
- "enable-command-block=false\n"
- "enable-query=false\n"
- "generator-settings={}\n"
- "level-name=world\n"
- Fn::Join:
- ''
- - "motd="
- Ref: Motd
- "\n"
- "query.port=25565\n"
- "pvp=true\n"
- "generate-structures=true\n"
- "difficulty=easy\n"
- "network-compression-threshold=256\n"
- "max-tick-time=60000\n"
- "require-resource-pack=false\n"
- "use-native-transport=true\n"
- "max-players=20\n"
- "online-mode=true\n"
- "enable-status=true\n"
- "allow-flight=false\n"
- "broadcast-rcon-to-ops=true\n"
- "view-distance=10\n"
- "server-ip=\n"
- "resource-pack-prompt=\n"
- "allow-nether=true\n"
- "server-port=25565\n"
- "enable-rcon=false\n"
- "sync-chunk-writes=true\n"
- "op-permission-level=4\n"
- "prevent-proxy-connections=false\n"
- "hide-online-players=false\n"
- "resource-pack=\n"
- "entity-broadcast-range-percentage=100\n"
- "simulation-distance=10\n"
- "rcon.password=\n"
- "player-idle-timeout=0\n"
- "force-gamemode=false\n"
- "rate-limit=0\n"
- "hardcore=false\n"
- "white-list=false\n"
- "broadcast-console-to-ops=true\n"
- "spawn-npcs=true\n"
- "spawn-animals=true\n"
- "function-permission-level=2\n"
- "level-type=default\n"
- "text-filtering-config=\n"
- "spawn-monsters=true\n"
- "enforce-whitelist=false\n"
- "resource-pack-sha1=\n"
- "spawn-protection=16\n"
- "max-world-size=29999984\n"
commands:
01_mkdir:
command: "sudo mkdir -p /opt/minecraft/server"
02_download:
command:
Fn::Join:
- ''
- - "sudo wget "
- Fn::FindInMap:
- MinecraftServerDownloadUrls
- Ref: ServerVersion
- Url
- " -O /opt/minecraft/server/server.jar"
03_chown:
command: "sudo chown -R minecraft:minecraft /opt/minecraft"
04_chmod:
command: "sudo chmod 2775 /opt/minecraft"
05_chmod:
command: "sudo find /opt/minecraft -type d -exec sudo chmod 2775 {} \\;"
06_chmod:
command: "sudo find /opt/minecraft -type f -exec sudo chmod 0664 {} \\;"
07_chmod:
command: "sudo chmod 664 /etc/systemd/system/minecraft.service"
08_service_reload:
command: "sudo systemctl daemon-reload"
09_service_enable:
command: "sudo chkconfig minecraft on"
10_service_start:
command: "sudo service minecraft start"
Properties:
ImageId: ami-09439f09c55136ecf
InstanceType:
Ref: InstanceType
SecurityGroups:
- Ref: MinecraftServerSecurityGroup
KeyName:
Ref: KeyName
UserData:
Fn::Base64:
Fn::Join:
- ''
- - "#!/bin/bash -xe\n"
- "yum update aws-cfn-bootstrap\n"
- "# Install the files and packages from the metadata\n"
- "/opt/aws/bin/cfn-init "
- " --stack "
- Ref: AWS::StackName
- " --resource MinecraftServerInstance "
- " --configsets InstallMinecraft "
- " --region "
- Ref: AWS::Region
- "\n"
- "# Signal the status from cfn-init\n"
- "/opt/aws/bin/cfn-signal -e $? "
- " --stack "
- Ref: AWS::StackName
- " --resource MinecraftServerInstance "
- " --region "
- Ref: AWS::Region
- "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment