The other day a colleague tipped me about a
docker image that contains a Minecraft server. I did a setup to start this server on an AWS EC2 instance that might be
of interest to others. With an AWS account, AWS cli configured via
aws configure,
AWS Access Key ID: ****************GH77
AWS Secret Access Key: ****************+CSp
Default region name: eu-central-1
Default output format: json
you can easily start an instance by executing my CloudFormation template
with
aws cloudformation create-stack --stack-name minecraft-server
--template-body file://minecraft-server.yaml
in the same directory as the yaml file,
---
AWSTemplateFormatVersion: "2010-09-09"
Description: "Minecraft Server using Docker"
Resources:
MinecraftServerSec:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: "Minecraft Server Sec. Group"
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
MinecraftServerEC2:
Type: "AWS::EC2::Instance"
Properties:
ImageId: "ami-0453cb7b5f2b7fca2"
InstanceType: "t2.medium"
Tags:
- Key: "Name"
Value: "Minecraft Server"
SecurityGroups:
- !Ref MinecraftServerSec
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
yum update -y && \
yum install docker -y && \
sudo service docker start && \
sudo usermod -a -G docker ec2-user && \
docker run -d -it -p 25565:25565 \
-e EULA=TRUE \
-e MODE=survival \
-e restart=always \
-e MOTD="-^^,--,~" \
itzg/minecraft-server
An EC2 instance should have been created running a Minecraft server with an assigned IP address on port 25565. If you want to access the running docker container, you can ssh into the server.
Say you want a Bash script that logs visits. Create a file,
count, run chmod +x count. Because you would
need to restart your Docker container, do not make
ln -s count /usr/local/bin/count but use the less elegant
solution cp count /usr/local/bin/count.
count should contain,
ID=$(docker ps | grep 25565 | awk '{print $1}')
echo "
VISIT COUNT
==========="
docker logs $ID | grep "joined the game" | cut -c 34- | \
sort -nr | uniq -c | awk '{print $2 " has visited " $1 " times"}'
outputting as an example,
VISIT COUNT
===========
USER_1 has visited 5 times
USER_2 has visited 2 times
USER_3 has visited 1 times
The power of cloud computing comes from its ability to produce abstractions and declaratively tie them together in a way that allows reliability, flexibility, security and scalability.
The principles behind cloud computing can be seen as an extension of the Unix philosophy through its focus on shared responsibility in and between individual "units", which work well together in their individuality but also through composition work well together as a coherent system. "Code as infrastructure" is in fact just a variant and aspect of the Unix philosophy's slogan "text as a universal interface".
By working according to the principle code as infrastructure and allowing the possibility to declare connections, collaboration, scalability, etc. - all in text format - you not only have the opportunity for version control, you also get the opportunity to reproduce the same system on a large scale, and could also tie this, if needed, to domain-specific languages, etc.
An important cornerstone in the idea of consistent reproducibility is the predictability that immutable data allows; a view of data that is found in functional programming and that grows stronger every year. Nix as a package manager and operating system is an obvious example, but the best known example must undoubtedly be docker.
There are many points of contact between the Unix philosophy, functional programming and cloud computing through its emphasis on singular responsibility in small programs, which via composition can be combined in an infinite number of expressions, focus on immutability but perhaps above all the idea of abstraction as a distance from the “machine”, the hard core (which in cloud computing services is completely delegated to the provider).
That something is declarative is a rather fuzzy thing, imbued with relativity and subjectivity. But if you can grasp something about the declarative in cloud computing, Unix and functional programming, the focus is on what is needed to achieve something, not how this is to be achieved. I wanted a server with certain properties starting a shell script - and I don’t need to digress on how to create it.