-
Notifications
You must be signed in to change notification settings - Fork 59
AWS Chess Server
Playing chess in your development environment by yourself can get old after a while. Wouldn't it be nice if you could actually play with your friends or family? The great thing is that your code is already designed to allow any computer in the world to talk to your HTTP server, you just need to put it in a place where it is accessible. To do this, you need to run your chess server on a device that has a public IP address and open up your network port so that it is externally accessible.
There are lots of ways you can accomplish this. You could lease an IP address from your internet service provider (ISP) and assign it to your laptop, or you could lease a server from a cloud provider such as Digital Ocean, Azure, or Amazon Web Services (AWS).
In this instruction we will go with AWS. Note that there is a cost involved in renting a server from AWS. Although if you are careful it shouldn't cost more than $5 a month.
Go to AWS and register for an account if you don't already have one. You will need a credit card, but you won't be charged for anything until you start using their services. Make sure you are aware of the pricing model for anything that you use so that you don't get any unexpected surprises with your monthly bill.
The Amazon Elastic Compute Cloud (EC2) service provides all of the functionality you need to launch a virtual server in an Amazon data center that is accessible to the world. After you have created your AWS account, use the AWS browser console to navigate to the EC2 service and lease your server with the following steps.
-
From the search bar type in EC2 and select the displayed EC2 dashboard.

-
Create a security group
- The security group will expose the ability to talk to our server over HTTP on the server's network port. This is usually port 8080. From the EC2 dashboard select the
Security Groupview from the options on the left. - Select the option to create a security group.
- Give it a meaningful name and description. Select the default VPC.
- In the
Inbound rulessection, create a rule that opens port 22 and port 8080 to everywhere.
- Save the security group.
- The security group will expose the ability to talk to our server over HTTP on the server's network port. This is usually port 8080. From the EC2 dashboard select the
-
Launch a server instance.
-
Navigate back to the EC2 dashboard.
-
Select the option to
Launch instance. -
Give your instance a name like
cs240-chessserver. -
Chose AWS linux for your Amazon Machine Image (AMI).

-
Set the instance type of t2.micro. If you are eligible for the free tier then you will not be billing for the first 12 months of your first t2.micro instance.

-
In the Key pair input select an existing key pair if you have created one previously, or select the
Create new key pairoption. Make sure you save the key pair to a safe place in your development environment. You will need this to connect to your server, and you do not want to let anyone else have access to it. Do not check the key pair into GitHub or any other publicly available location.
Note: You may need to change the file permissions to protect your key file before you can use it to access your AWS instance. If using a Mac or Linux, use the commandchmod 600 <path to your file>. -
In the
Network settingsyou specify how the world can access your server. Choose the option toSelect existing security groupand pick the security group you created previously.
-
Scroll past the remaining options and press
Launch instance. This will display a message saying that the instance has been successfully launched. You can then navigate to theInstancesview and click on your newly created server to see all of the details.
You will want to copy the
Public IPv4 addressso that you can remotely connect to the server using a secure shell (SSH) and also access your server from the browser in order to play a game of chess.
-
Now that you have your AWS EC2 server up and running you need to install MySQL so that you can store your user and game information.
First you need to remotely connect to your server using a secure shell (SSH). Usually you do this by opening a console, or terminal, window in your development environment and typing the command ssh. In order to connect, you need the key pair you used to launch your server. The command looks like this:
ssh -i youkeypairhere.pem ec2-user@youripaddresshereOnce you have shelled into your EC2 server you can download, install, and start MySQL with the following commands.
sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
sudo dnf install mysql80-community-release-el9-1.noarch.rpm -y
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
sudo dnf install mysql-community-client -y
sudo dnf install mysql-community-server -y
sudo systemctl start mysqldWhen MySQL was installed it created a random password for the root user. You can get the password from the MySQL log file and use that to login to the MySQL client and change the password.
sudo grep 'temporary password' /var/log/mysqld.log
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';If your password is not complicated enough for the default MySQL password checker, you can disable the checker, but first you must change the password to something that is acceptable.
ALTER USER 'root'@'localhost' IDENTIFIED BY '1Really~Complicated!!';
UNINSTALL COMPONENT 'file://component_validate_password';
ALTER USER 'root'@'localhost' IDENTIFIED BY 'monkeypie';Note: Make sure your password matches the root password specified in the db.properties file in your chess client.
Use the following to install the latest version of Java on your EC2 server.
- SSH into server as described above
- Install java
sudo dnf install java - Check that Java is running with
java --version
You may need to modify your Chess Client code so that you can specify the server that it connects to. The following are two possible changes you might make.
- Make it so your chess client can take the
youripaddresshere:portas a parameter.public static void main(String[] args) { var serverName = args.length > 0 ? args[0] : "localhost:8080"; ChessClient client = new ChessClient(serverName); // ... }
- Modify your web/index.html so your user can download your java client
<p>Download the <a href="client.jar">client</a>. Run with:</p> <pre>java -jar client.jar <span id="hostname">server</span>:8080</pre> <script> document.querySelector('#hostname').innerText = window.location.hostname; </script>
Build the client Jar and copy it to your server's resources web directory so that it can be downloaded from the server when someone wants to play chess.
mvn package -pl client -DskipTests
cp client/target/client-jar-with-dependencies.jar server/src/main/resources/web/client.jarNow we can build the server jar.
mvn package -pl server -DskipTestsThis will create the following server/target/server-jar-with-dependencies.jar
With the server and client built, we can copy the server jar up to the EC2 server using the secure copy utility scp.
scp -i youkeypairhere.pem server/target/server-jar-with-dependencies.jar ec2-user@youripaddresshere:server.jarNow you are ready to start up the server.
- SSH into server as described above
- Run your server using the Java runtime and your server.jar
java -jar server.jarThis should output that the server is successfully running on the assigned port. Now you should be able to open up a browser on any computer in the world and see the web interface for your chess server. When you open your browser, put the public IP address and port number of your EC2 Instance. This should be something like http://youripaddresshere:8080.

You can then click the link that you created to download the client. Note that you may need to tell your browser to load your web page even though it is not secure, as well as specifying that you want to download the jar file.

Now you can use the Java runtime to run your chess client. Make sure you provide the ip address and port number as parameters.
java -jar client.jar youripaddress:8080
Invoking your chess server from ssh will require you to keep the ssh terminal open. You can make your chess server start automatically every time your AWS instance is started by following these instructions:
- SSH into your server as described above
- Create and open a service description file:
sudo nano /etc/systemd/system/chess_server.service - Enter and save the following in the Nano code editor window:
Description=Chess Server
After=network.target
[Service]
Type=simple
ExecStart=java -jar /home/ec2-user/server.jar 8080
[Install]
WantedBy=multi-user.target
sudo systemctl enable chess_server
- Enable the chess server service:
sudo systemctl enable chess_server - Start the chess server service:
sudo systemctl start chess_server - Check the status of the chess server service:
sudo systemctl status chess_server - To confirm that the service starts whenever the AWS instance is started, stop and restart the AWS instance from the AWS console or use this command:
sudo reboot
Have fun! You have just taken the first step in becoming the world's best chess server.
- 🎥 Overview (1:41) - [transcript]
- 🎥 Launch an EC2 Instance (14:42) - [transcript]
- 🎥 Setup Your EC2 Instance (13:42) - [transcript]
- 🎥 Modify Your Client Code (7:50) - [transcript]
- 🎥 Deploy Code and Start the Server (17:49) - [transcript]
- 🎥 Configure Your Chess Server to Start When Your EC2 Instance Starts (8:39) - [transcript]