Your goal is to deploy a Spring Boot project to a remote server and verify its execution. This will establish the basics for working in a cloud environment.
Get Airwaze Running locally
master
or in a day5-solution
branchapplication.properties
file is using tokens like ${APP_DB_NAME}
, if not change it to use thembootRun
elasticsearch-starter
branchGet Airwaze Ready for the Cloud
src/main/resources/import.sql
COPY route(src, src_id, dst, dst_id, airline, route_geom) from '/home/airwaze/routes.csv' DELIMITER ',' CSV HEADER;
COPY airport(airport_id, name, city, country, faa_code, icao, altitude, time_zone, airport_lat_long) from '/home/airwaze/Airports.csv' DELIMITER ',' CSV HEADER;
Tasks > build > bootRepackage
/YOUR-AIRWAZE-REPO/build/libs/app-0.0.1-SNAPSHOT.jar
Note
The file name app-0.0.1-SNAPSHOT.jar
comes from the jar
property in build.gradle
.
Screen shot of AWS console with arrow pointing to Region menu
Services
in the page header.EC2
under Compute.Screen shot of AWS console with a red arrow pointing to the EC2 link in the Services dropdown
Instances
link in the sidebarScreen shot of AWS console with a red arrow pointing to the Instances link in the sidebar
Launch Instance
button at the top of the pageScreen shot of AWS console with a red circle around the Launch Instance button
When creating a new instance, Amazon provides multiple free Amazon Machine Images (AMIs) to choose from. This is a pre-configured operating system installation with multiple tools ready for use. For this exercise, we want to use the Ubuntu Server 16.04 LTS AMI. Locate it in the list of “Quick Start” images and click its Select
button.
Screen shot showing Ubuntu Server selection in AMI screen
Next, the console will ask which type of instance to set up. Your choice here defines the amount of virtual CPU cores, RAM, and network perforance you want. This also directly affects the cost of the running instance. Select the t2.micro
service, then click Configure Instance Details.
Screen show with t2.micro selected and red circle highlighting the selection
Next, click Configure Instance Details
We aren’t going to change anything on this page now
However, please take a quick look at the properties that you can change on this page
Next, click Add Storage
On this screen, you can choose what storage is available to your instance. AWS will provision a virtual volume in Elastic Block Store to serve as the volume(s) mounted in your instance. By default, it will create an 8 GiB volume to serve as the instance’s root volume.
Screen shot showing pre-filled 8 GiB storage selection
The Add Tags screen is helpful to “name” our ec2 instance. Since lots of us are going to be creating instances, please click Add Tag add a Name
tag with a value of something unique and relevant to you, example blakes-ec2-walkthrough
.
Screen shot demonstrating an empty Add Tags screen and the Add Tag button
Next click Configure Security Group
The Security Group controls network traffic in and out of the server you are creating. You can create rules for different kinds of traffic on different ports. Examples: SSH
, HTTP
, port 8080
.
Configuring the security groups for your server is critical for protecting your instance from unauthorized remote access. The organization or indiviaul who created the AWS account is liable for the costs generated by any instances that are setup, in this case LaunchCode is that origanization. An openly-accessible instance can risk your infrastructure security and accumulate great costs to your organization if it were to be compromised.
SSH
access to your instance, but only from the IP you’re currently using to access AWSScreen shot showing Create Security Group page with My IP circled in red to highlight the selection
Next click Review and Launch button in the bottom right
This screen gives you a final chance to review and change the settings you chose for this instance.
This will open a popup on the screen that allows you to configure a key pair for the instance. This will generate the key necessary to SSH into the instance and without this you will not be able to access your instance. In an enterprise environment, there will likely already be multiple key pairs set up that you would use here. For the purpose of this project, create a new key pair:
*.pem
file in a good location and do not lose it. A suggestion is to put them in ~/.ssh
folder.mv ~/Downloads/your-keypair.pem ~/.ssh
AWS will now begin launching your instance. After Launching your instance will be availabe in the list of EC2 Instances. You can click the identifier for your instance to monitor it as it starts up. This will take you back to the Instances dashboard. In the Description tab of your instance you can see important properties such as public DNS
, IP
, running state
, instance type
, links to security group(s)
, key pair
, etc.
Screen shot showing Instances dashboard and a running instance. A red circle is around the Public DNS entry.
At this point we have created a server in the cloud, but at this point it’s just a server. We haven’t deployed our application to it yet. In the next steps we will deploy the Airwaze application to our new server.
.pem
file to be read-only by your user:$ chmod 400 name-of-pem.pem
$ ssh -i ~/.ssh/name-of-pem.pem ubuntu@PUBLIC-DNS-OF-SERVER.compute.amazonaws.com
Note
Note the ubuntu
part of the above command is the user/role you are attempting to connect with on the remote computer.
Screen shot of terminal showing successful SSH connection to AWS instance
Congratulations! You have successfully created and connected to an instance running in the cloud.
Now that you have a server running in the cloud, you need to use it to do some work. Let’s prepare the server to run our application.
First, you don’t want the application running under your system account, so we need to create a new user::
(On remote server)
ubuntu$ sudo adduser --system airwaze
We are going to upload our app jar file and the two csv files to the server. We’ll use scp
to securely transmit the file to our server.
ssh
session open, but open a new terminal on your cmoputer by hitting Command + T
while in your terminal(On local computer, NOT in ssh session)
$ scp -i ~/.ssh/name-of-pem.pem /your-airwaze-repo/build/libs/app-0.0.1-SNAPSHOT.jar [email protected]:/home/ubuntu/app-0.0.1-SNAPSHOT.jar
$ scp -i ~/.ssh/name-of-pem.pem /your-airwaze-repo/*.csv [email protected]:/home/ubuntu
The remotes servers will not come with everything we need already isntalled. However it does come with a tool that makes it easy to install software. apt-get is the “Package Manager” that comes with Ubuntu. We will use it to install the JDK and other tools we need.
We need Java to run our app, we will install it using apt-get
:
(On remote server)
ubuntu$ sudo apt-get update
ubuntu$ sudo apt-get install openjdk-8-jdk
ubuntu$ java -version
Now, on the server, move the file to the airwaze home directory, and make it owned and executable by that user. Notice the changes in ls -l
after the owner and permissions calls are made.:
(On remote server)
(move files to airwaze home)
ubuntu$ sudo mv ~/app-0.0.1-SNAPSHOT.jar /home/airwaze/app-0.0.1-SNAPSHOT.jar
ubuntu$ sudo mv ~/*.csv /home/airwaze
ubuntu$ cd /home/airwaze
ubuntu$ ls -l
(change it so that the owner can execute the file)
ubuntu$ sudo chmod 500 /home/airwaze/app-0.0.1-SNAPSHOT.jar
(change the owner to airwaze user)
ubuntu$ sudo chown airwaze:ubuntu app-0.0.1-SNAPSHOT.jar
ubuntu$ ls -l
Now the airwaze user can execute app-0.0.1-SNAPSHOT.jar.:
-rw-r--r-- 1 airwaze ubuntu 881432 May 20 01:23 Airports.csv
-r-x------ 1 airwaze ubuntu 46309179 May 20 01:22 app-0.0.1-SNAPSHOT.jar
-rw-r--r-- 1 airwaze ubuntu 6464492 May 20 01:23 routes.csv
Before trying to start the application, we’ll install postgres
locally so we can start Airwaze Studio.
Normally you would install the database on it’s own server. Installing the database on the same cloud server ** is something you would never do in a real cloud instance**.
We are doing it here to get practice working with cloud servers, we will learn how to use postgresql differently later this week.:
(On remote server)
$ sudo apt-get update
$ sudo apt-get install postgresql postgresql-contrib postgis
(on remote server)
ubuntu$ psql -U postgres
psql: FATAL: Peer authentication failed for user "postgres"
nano
or vi
(On remote server)
ubuntu$ sudo nano /etc/postgresql/9.5/main/pg_hba.conf
#
peer
to be md5
. Be careful to change the correct line(Section in red box shoud look like this after editing it)
# "local" is for Unix domain socket connections only
local all all md5
(on remote server)
(restart postgresql)
ubuntu$ sudo /etc/init.d/postgresql restart
(when prompted provide password of your choice, but be sure to remember it)
ubuntu$ sudo -u postgres createuser --pwprompt --superuser airwaze_db_user
(now open a psql# shell)
ubuntu$ psql -U airwaze_db_user -d postgres
postgres=# CREATE DATABASE airwaze;
(install postgis extensions in airwaze database)
postgres=# \c airwaze;
airwaze=# CREATE EXTENSION postgis;
airwaze=# CREATE EXTENSION postgis_topology;
airwaze=# CREATE EXTENSION fuzzystrmatch;
airwaze=# CREATE EXTENSION postgis_tiger_geocoder;
Now that the app is on the cloud server and the database is ready, we can set up systemd
to run this app as a service. systemd
is used to configure
and run services on linux. More info in this linux.com article and this systemd wiki page.
In order to use systemd
, we have to make a script in /etc/systemd/system
to tell the service how to run our app.
(On remote server)
ubuntu$ sudo nano /etc/systemd/system/airwaze.service
Copy and paste this text into the airwaze.service
file and then save it:
[Unit]
Description=Airwaze Studio
After=syslog.target
[Service]
User=airwaze
ExecStart=/usr/bin/java -jar /home/airwaze/app-0.0.1-SNAPSHOT.jar SuccessExitStatus=143
Restart=no
EnvironmentFile=/home/airwaze/airwaze-env-variables.config
[Install]
WantedBy=multi-user.target
As we have stated many times, we do not want to hardcode usernames and passwords into our code. We need a way to configure the
environment variables that are referenced in application.properties
. We will create a new file named airwaze-env-variables.config
that will set the environment variables
when the airwaze service runs. Notice that the EnvironmentFile
property in the airwaze.server
tells the service where to look for
environment variables.
(On remote server)
ubuntu$ sudo nano /home/airwaze/airwaze-env-variables.config
Copy and paste this text into the airwaze-env-variables.config
file and then save it:
APP_DB_HOST=localhost
APP_DB_PORT=5432
APP_DB_NAME=airwaze
APP_DB_USER=airwaze_db_user
APP_DB_PASS=your-db-password (that you set in section 8)
Once this service definition is in place, set the service to start automatically on boot with systemd using the systemctl
utility and also start now:
(On remote server)
ubuntu$ sudo systemctl enable airwaze
ubuntu$ sudo systemctl start airwaze
And you can view the logs for the service with journalctl
.:
(On remote server)
ubuntu$ journalctl -f -u airwaze.service
http
?ssh
….(on remote server)
(to see if there are any clues/errors)
ubuntu$ journalctl -f -u airwaze
(on remote server)
(to see if anything is listening to port 8080 on the server)
ubuntu$ telnet localhost 8080
(on local computer)
(check to see if you connect to server from your local computer via http)
$ telnet PUBLIC-DNS-OF-SERVER.compute.amazonaws.com 8080
(if you got an error about telnet not being a command, then install it and try again)
$ brew install telnet
We can’t load the airwaze app in the browser because the server is currently only configured to allow inboud traffic on port 22 (the ssh
port).
We need to add a new configuration that will allow inboud traffic on port 8080
.
Inbound
tab and Edit
the inbound traffic listScreen shot of the security group settings with a red circle around the selected Inbound tab
Custom TCP
rule for port 8080 and select My IP
for the sourceScreen shot of Edit inbound rules display with a new rule of 8080 to “My IP” added with red circles around the 8080 port and “My IP”
Save
. This opens up a new port in the Security Group just for your IP. The Airwaze app is set up to listen to port 8080 and communicating with that port from your browser will allow you to communicate with the application.If you kept journalctl
running from before, you should see the logs progress as your browser communicates with the app.
Congratulations! You now have your own application in the cloud!
Your is currently showing up on the screen; however, the map may not be showing any airports. Troubleshoot the application and figure out why the airports are not showing up. Be sure to use your browser’s developer tools.
When you have found the problem, build a new copy of your jar and deploy it on your server.
bootRepackage
scp
the updated jar file to the serversudo systemctl stop airwaze
sudo systemctl disable airwaze