Running Vapor on Amazon Lightsail ⛵️
So, you’re really into cutting edge technologies and mashing all the latest tools and platforms together for fun and/or profit, eh? Cool, so am I. 😬
What this will cover
- Setting up Lightsail with Ubuntu 16.04 LTS
- Installing Vapor and Swift through SSH
- Creating and configuring a Vapor app
Background (feel free to skip)
Vapor is one of the hippest, Swiftiest web frameworks out there, and thanks to heavy pushing by Ray Wenderlich and a very pretty website I decided to try it out. But I wasn’t content to follow the well-trodden paths of installing Vapor on Heroku or to simply play around with it on my own computer. (If you’ve never used Vapor I recommend you try the latter first!)
Amazon Lightsail was released at the end of 2016 as an onramp to Amazon’s more serious cloud services. It’s targeted toward indie developers and startups that want the quick and simple setup we’ve come to expect from smaller shops such as DigitalOcean and Heroku, but with the scalability and (general 😅) reliability of Amazon’s infrastructure. It’s also worth noting that scaling to tremendous sizes on Amazon’s enterprise-level solutions is far easier if you’re already in its ecosystem.
As a mobile developer, I don’t get many opportunities to work with web technologies. Vapor struck my fancy when it started to gain popularity in early 2016, but I hadn’t had a chance to give it a full trial until recently. Killing two birds with one stone, I decided to learn the ropes of Lightsail while I was at it. What follows is the cleanest path I found from zero to server using Vapor, Ubuntu, and Amazon Lightsail. 🏎
Assumptions: You know some Swift, you’re moderately comfortable with bash, and you’re willing to spend hours of trial and error 😓 to get things to work.
Set up your server
- Sign up for Lightsail. You’ll need an Amazon Web Services (AWS) account, which is free on its own, though you’ll be forced to enter in credit card information in the event that your app becomes wildly popular and eats up tons of Amazon’s resources. You get 30 days of the Lightsail service for free, which should be plenty to try it out and see if it’s what you need.
- Set up a new instance with just an OS — pick Ubuntu! The Amazon Linux build isn’t as widely supported as Ubuntu, which has a large community and proven support for Swift and Vapor.
- You should see the new server instance you just created. Open it up and head to the Connect tab. Locate and press the big orange button that says Connect using SSH to spin up a Secure SHell (SSH) instance within your browser. Welcome to the Matrix.
- This is where you’ll be doing the heavy lifting of setting up your server. Go ahead and paste the following code into your shell to get Ubuntu fully up-to-date. I’ve included some comments above each command to provide a general sense of what’s happening.
# Run following commands as root (su = superuser)
# Update and upgrade the current system and installed packages
apt update && apt upgrade -y && apt dist-upgrade -y && apt install htop -y
# Set up automatic security updates
apt install unattended-upgrades -y && dpkg-reconfigure -plow unattended-upgrades
# Install and run ntp so we can communicate with other servers
apt install ntp -y && ntpq -p && service ntp restart
# Restart the server to finalize installation of updates
ntp is the Network Time Protocol package, which allows your server’s clock to synchronize with computers across the interwebs.
- If you get a big colored box that asks about automatically downloading unattended upgrades, hit
Yesand continue on. You may get a second colorful prompt — just hit
Okhere and you’ll be fine. These configurations will help keep your Ubuntu installation secure by automatically installing important updates. CAUTION: These are not a substitute for good security practices and it is up to you to maintain the sanctity of your server. 🙌🏼
- If you didn’t see the purple box, as I didn’t the first time I went through this process, don’t worry about it! However, these prompts break the flow of the commands we entered above and you’ll need to manually continue by pasting and running
apt install ntp -y && ntpq -p && service ntp restart, then finish with
- Since we called
reboot, you may see a warning that you’ve lost your connection. Wait a few seconds, do some quick stretches, then go ahead and reconnect.
Install Vapor and Swift
- Install the final pieces of the puzzle with the following commands:
# Install dependencies necessary for Swift to run on Linux
sudo apt install git cmake ninja-build clang python uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config libblocksruntime-dev libcurl4-openssl-dev autoconf libtool systemtap-sdt-dev unzip -y
# Run Vapor's pre-installation code
eval "$(curl -sL https://apt.vapor.sh)"
# Install the latest version of Vapor and its dependencies, including Swift
sudo apt -f install vapor -y
You may wonder why we didn’t install the latest version of Swift straight from the Swift website, which is very observant of you. As it turns out, Vapor currently only supports up to Swift 3.1.0, and the most recent release of Swift (3.1.1 as of 5/1/2017) actually prevents Vapor from successfully installing without a whole lot of stress and heartache. Installing Swift as a Vapor dependency ensures Vapor gets the most recent version of Swift it can handle.
- To check that the installation was a success, run the command
which swift && swift --version. You should get back something like:
Swift version 3.1 (swift-3.1-RELEASE)
If you do, awesome! You’ve got Swift! If you don’t get that result, go through the previous steps again and ensure you copy and paste each of the commands exactly as shown.
- To check that Vapor installed correctly, just run
vapor --help. The results should look like this:
Usage: vapor <new|build|run|fetch|update|clean|test|xcode|version|heroku>
Join our Slack if you have questions, need help,
or want to contribute: http://vapor.team
Boo-ya! 🚀 You’ve now got the Vapor Toolbox installed on an Ubuntu instance running on Amazon Lightsail. How cool is that?
There are a few more steps before you have an actual web app running, but you’ve laid the groundwork for building and running Swift code on the internet!
Create and run a Vapor project
- To get started with Vapor you can run
vapor new <project name>, and replace
<project name>with whatever you want to call your project. The vapor logo should appear and you’ll then be able to start digging into your new web app!
cdinto your new project folder and use
ls -ato see its contents. Use
vapor buildto build the sample project included in your fresh Vapor installation. It’ll take a while the first time through, so be patient. When it’s done, use
vapor run serveto run your Vapor app.
- Go back to your Lightsail instance dashboard and find your public IP address (or static IP, if you’ve set that up). Copy this into the address bar of a new browser tab with the port
8080appended to the end.
8080is Vapor’s default port, and is what you’ll need to use to access the sample project for now. The whole address should look something like
12.34.567.890:8080, but with your public IP. Hit return and you’ll see that the request has failed. What!? No worries, that’s supposed to happen. 🤡
8080is a somewhat unusual port, and Lightsail isn’t set up by default to accept requests coming in on it. Head to the Networking tab in Lightsail and scroll down to your Firewall. Here you’ll need to add a new application that accepts connections on port
- After you’ve added and saved the Custom application with the configuration shown above, try heading back to your app. At last, you should see this:
Great job if you’ve made it this far! There’s a lot more you’ll need to do to have the app of your dreams running in the cloud, but you’ve now got the experience of setting up a working Vapor app in an Ubuntu Server instance on Amazon Lightsail. (Buzzword, buzzword, buzzword…)
You’ll need to find a way to manage and update the files on your server, be it Git, FTP, or some other cool tool. I’m a fan of the visual nature of FTP clients such as FileZilla, but if you’ve got a system you use and love to push and pull code to and from your server, let me know in the comments!
The article I referenced most during this process was Sebastian Kreutzberger’s amazing Deployment of a Swift Vapor App to the AWS EC2 Cloud. Definitely give the second section a read for some more detail on how to make your Vapor app more reliable by running it as a system service.
The “Getting Started” section of Apple’s Swift docs also came in handy in figuring out how to set up Swift on Ubuntu.
Thanks for taking the time to read all the way down here. I’m sure there are some inefficiencies in what I’ve provided and hopefully not too many errors. If you come up with any improvements please don’t hesitate to let me know in the comments, and if you found this at all useful please share the love! 💚