Skip to content

hernada3/CS132_projectpt2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS Minecraft Server Using Terraform and Ansible

This guide will explain the steps needed to start a minecraft server using AWS, Terraform, Ansible, and a Bash script.

Pipeline Diagram

Pipeline Diagram

Tools used

  • AWS CLI
  • Terraform
  • Ansible
  • Bash
  • Git

Preparation

The first thing you are going to want to do is to clone this repository by running git clone https://github.com/hernada3/CS132_projectpt2.git.

In order for the bash script to run you will need to get your AWS credentails in order to use AWS CLI. Do this by getting your credentials, which should look something like this:

[default]
aws_access_key_id=<VALUE>
aws_session_token=<VALUE>
aws_session_token=<VALUE>

Copy and paste this into ~/.aws/credentials.

You are also going to want to generate an ssh key in order for the ansible script to connect to the instance that is created. You can do this by running the following command: ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa. This should generate a public and private key and store it in the .ssh folder.

When running the bash script this should automatically transfer the public key to the instance.

Execution

Now that everything is set up properly you are going to want to be in the same directory as main.tf and script.bash.

All that needs to be done is to run the command: bash script.bash. This will run the following script.

#!/bin/bash

set -e

terraform init
terraform apply -auto-approve

SERVER_IP=$(terraform output -raw server_ip)

echo "Server IP is $SERVER_IP"

cat > ansible/inventory.ini <<EOF
[Minecraft_servers]
$SERVER_IP ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/id_rsa
EOF

ansible-playbook -i ansible/inventory.ini ansible/playbook.yml

terraform init will initialize the working directory with the terraform configuration file.

terraform apply -auto-approve will apply the configuration files. In this case it will start the EC2 instance and VPC. It will also output the public IP that will be used to connect to connect to the Minecraft server.

SERVER_IP=$(terraform output -raw server_ip) will get the public IP adress of the instance to use in the inventory.ini file.

echo "Server IP is $SERVER_IP" will print the public IP onto the terminal.

The following block will fill in the needed information for the ansible script to work, the information being the public IP address. The script also contains the location of the ssh key you made earlier in order to connect to the instance.

[Minecraft_servers]
$SERVER_IP ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/id_rsa
EOF

Finally ansible-playbook -i ansible/inventory.ini ansible/playbook.yml will run the ansible playbook which will install the needed tools and configures the Minecraft server.

When running the script there will be a prompt when it gets to the ansible part, type yes and it will continue. Having the ansible playbook run will also automatically agree to the EULA.

Some things can be changed in the Minecraft server configuration, like the amount of ram or the version of Minecraft that is getting hosted.

To change the server version you can change the minecraft_server_jar_url to have the correct link for the server.jar download.

Connecting to the Mincraft Server

Once the script has successfully finished you can go to your Minecraft client, click muliplayer then add server. Put the public IP that was printed earlier into the server address box and click done. This should log you into the server successfully.

At this point you are done, the server should restart automatically if there are any outages or reboots. Enjoy your Minecraft server!

References

https://github.com/chadgeary/minecraft.git

https://docs.ansible.com/ansible/latest/getting_started/get_started_inventory.html

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule

https://abstractlabs.co.uk/how-to-deploy-a-minecraft-server-on-aws-using-terraform-iac-b8691e87b55f

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published