DEV Community

Valentino Stoll
Valentino Stoll

Posted on

Twilio Powered Minecraft Server on AWS

What I built

A way to control access to a Minecraft server hosted on an EC2 instance via SMS.

Demo

Background

This all came about when my kids wanted to play Minecraft with their friends. While Realms seemed like a decent choice, I wanted something with more control and a way to allow other parents to easily add their kid(s) and start/stop the server.

The Full Picture

An administrator sends an SMS to a Twilio number with a command (e.g. status). Twilio then sends an incoming SMS web request to an Amazon API Gateway endpoint. This endpoint hands off the Twilio event and request data to an AWS Lambda function which validates and processes the command to perform one of the following:

1) startup – Starts the EC2 instance.
2) shutdown – Stops the EC2 instance.
3) whitelist add 1.2.3.4 as beetlejuice – Allows a new user to connect to the Minecraft Server.
4) whitelist remove 1.2.3.4 – Dis-allows an existing user from connecting to the Minecraft Server.
5) status – Check the status to see if the instance is running.

Introducing:

GitHub logo codenamev / twilio-aws-minecraft

Control a minecraft server in AWS via Twilio SMS

Setup

Prerequisites

With the above in place, you should have a working Minecraft Server running in EC2, and a simple Lambda setup with the API Gateway to receive events from Twilio.

Lambda Environment Variables

Make sure to add the following environment variables to your Lambda function:

Key Description
ADMIN_NUMBERS A comma delimited list of administrator phone numbers (e.g. +12125552368,+12125551234)
TWILIO_ACCOUNT_SID Your primary Twilio account identifier - find this in the Console.
TWILIO_AUTH_TOKEN Used to authenticate - just like the above, you'll find this here.
EC2_INSTANCE_ID The ID of the EC2 Instance you created above. You can find it here.
EC2_REGION The region your EC2 instance is running in (e.g. us-east-1b)
EC2_SECURITY_GROUP_ID The ID of the security group that is assigned to the EC2 Instance (e.g. sg-1234567ab89cd0987)
TWILIO_NUMBER the Twilio phone number you setup to receive SMS (e.g. +12125552368, also found in your Twilio Console)
MINECRAFT_HOST The server domain or IP that points to your Minecraft Server (e.g. minecraft.example.com)
MINECRAFT_PORT e.g. 25565
REQUEST_URL The API Gateway endpoint you setup for the Lambda function and added as the SMS incoming webhook (e.g. https://1somehash23.execute-api.us-east-1.amazonaws.com/prod/message)

Lambda permissions

You will have to add the following actions to the permissions for your Lambda function:

Action Description
ec2:AuthorizeSecurityGroupIngress Allows adding new IPs to the server whitelist
ec2:DescribeInstanceStatus Allows checking the status of the instance
ec2:DescribeSecurityGroups Allows looking for existing IPs in the server whitelist
ec2:RevokeSecurityGroupIngress Allows removing a whitelisted IP from the server
ec2:StartInstances Allows starting the instance
ec2:StopInstances Allows stopping the instance
logs:CreateLogStream Allows capturing output to logs
logs:PutLogEvents Allows saving log events

Next, you'll want to checkout my twilio-aws-minecraft repo:

git clone https://github.com/codenamev/twilio-aws-minecraft.git \
cd twilio-aws-minecraft \
zip -r twilio_function.zip ./
Enter fullscreen mode Exit fullscreen mode

That will create a packaged lambda script with all required libraries in .zip form that you can upload to replace the existing Lambda script you created in the previous step.

Once you've saved the newly uploaded Lambda and deployed, you can test out your new EC2 manager via SMS!

Somessues I faced:

  • Lambda IPs for EC2 whitelist
  • Minecraft Forge and minecraft.service setup issues
  • Instance startup not starting minecraft.service
  • UserData
  • GameMode(s)
  • Configuring OPs
  • Debugging

Top comments (0)