Twilio Powered Minecraft Server on AWS
Tags: twiliohackathon
Published: 2020-04-28 01:33:23

[Comment]: # (All of this is placeholder text. Use this format or any other format of your choosing to best describe your project.)

[Reminder]: # (Make sure you've submitted the Twilio CodeExchange agreement: https://ahoy.twilio.com/code-exchange-community)

[Important]: # (By making a submission, you agree to the competition's terms: https://www.twilio.com/legal/twilio-dev-hackathon-terms)


## What I built

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

![Demo](https://i.imgur.com/mWEtXYZ.gif)

## 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 codenamev/twilio-aws-minecraft no-readme %}

## Setup

### Prerequisites

- [setting up a Minecraft Server 
 on an EC2 instance](https://medium.com/exampro/2018-modded-minecraft-server-on-aws-part-1-run-a-modded-minecraft-server-on-aws-ec2-instance-b37290462d8d).  I hope to update this at some point with a slimmer set of instructions that I now use, but this got me where I needed.
- [Set up a Lambda to receive Twilio events](https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-python-amazon-lambda)

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](https://www.twilio.com/console).                                                                           |
| TWILIO_AUTH_TOKEN     | Used to authenticate - [just like the above, you'll find this here](https://www.twilio.com/console).                                                                           |
| EC2_INSTANCE_ID       | The ID of the EC2 Instance you created above. [You can find it here](https://console.aws.amazon.com/ec2/v2/home).                                                              |
| 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](https://www.twilio.com/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:

```bash
git clone https://github.com/codenamev/twilio-aws-minecraft.git \
cd twilio-aws-minecraft \
zip -r twilio_function.zip ./
```
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