Deploying to Vapor Cloud 2 Tutorial
Learn how to deploy your Vapor app to Vapor Cloud 2, and take advantage of the free database tier in the new version of their cloud service! By Joe Howard.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Deploying to Vapor Cloud 2 Tutorial
20 mins
You’re ready to deploy that Vapor app you’ve worked so hard on. But how can you deploy it to a cloud solution, with minimum hassle? Vapor Cloud to the rescue!
Vapor Cloud 2 is a Platform as a Service (PaaS) built by the Vapor team specifically for hosting Vapor applications. In this tutorial, you’ll learn how to deploy your application to Vapor Cloud 2.
Vapor Cloud 2 is designed to simplify configuring servers and managing deployments so you can concentrate on writing code. Vapor Cloud 2 is still in pre-release, but it’s available to use for testing the deployment of your apps. And there’s some great news in this latest iteration: Vapor Cloud 2 provides a free database tier for persisting data! You can find out more about the release schedule of Vapor Cloud 2 here.
You’ll use a pre-built Vapor app named TIL (Today I Learned) that hosts user-supplied acronyms.
The high-level steps you’ll perform in this tutorial are:
- Setting up a Vapor Cloud account.
- Setting up Git access to the Vapor Cloud Git server.
- Forking a pre-built Vapor app on GitHub.
- Creating and setting up a new Vapor Cloud application for your app.
- Deploying your app to Vapor Cloud and bringing it online.
Note: This tutorial assumes you have some experience with using Vapor to build web apps. See Getting Started with Server-side Swift with Vapor if you’re new to Vapor. This tutorial also assumes you have some experience working with the command line, SSH, Docker, Git, and GitHub.
Note: This tutorial assumes you have some experience with using Vapor to build web apps. See Getting Started with Server-side Swift with Vapor if you’re new to Vapor. This tutorial also assumes you have some experience working with the command line, SSH, Docker, Git, and GitHub.
Getting Started
Click the Download Materials button at the top or bottom of this tutorial to download a text file in which you’ll keep track of all the credentials you’ll create in this tutorial.
You’ll also need a Vapor Cloud account in order to deploy your app. If you don’t have one, sign up for one here.
Once you’ve confirmed your email account and logged in to Vapor Cloud, you’ll see the Dashboard, which is still under construction at the time of writing this tutorial.
Setting Up Git Access
To host your code, you’ll need to access Vapor Cloud’s private Git server. The best way to do this is by adding your public SSH key to your new account.
On macOS, in Terminal, enter this command to find the file that contains your public SSH key:
ls ~/.ssh
Your file probably has one of the default public key filenames: id_rsa.pub, id_dsa.pub, id_ecdsa.pub or id_ed25519.pub.
Note: If you don’t have an SSH key, follow GitHub’s instructions to create one.
Note: If you don’t have an SSH key, follow GitHub’s instructions to create one.
Copy the name of the id_something.pub file, and paste it into the following command:
pbcopy < ~/.ssh/<your .pub file>
This copies your public SSH key to the clipboard.
Now go to Settings in the Vapor Cloud console, and click the + button in the upper right corner to add your SSH key:
Give the key a name, paste the key in, then click Create. To verify your access, enter the following command in Terminal:
ssh git@git.code.vapor.cloud
Enter yes
if it asks “Are you sure you want to continue connecting (yes/no)?”.
If your SSH key is working, you’ll see output similar to this:
PTY allocation request failed on channel 0 Hi joe@razeware.com, You've successfully authenticated to Vapor Cloud Git Connection to git.code.vapor.cloud closed.
Forking the TIL Project
Rather than start from scratch, you’ll fork an existing Vapor app, and work on your fork for the rest of this tutorial.
The Vapor TIL app is available in a public repo on GitHub.
Running the app locally looks like this:
To work with your own version of this app, fork the repo to your own GitHub account:
Next, create a local clone of the repo on your development machine by cloning the repo fork from your account. In Terminal, cd
to the directory where you want your local repo to live.
Then, if you’ve set up your GitHub profile with SSH, enter this command:
git clone git@github.com:<YOUR_GITHUB_USERNAME>/vapor-til.git
If you haven’t set up SSH access, you should clone the repository with HTTPS:
git clone https://github.com/<YOUR_GITHUB_USERNAME>/vapor-til.git
At this point, you want to ensure you have the version of the code that matches this tutorial. To make sure of this, execute the following commands against your local repo:
git reset --hard 3b7ead6
git push --force origin master
Doing this resets your fork to the particular commit you need for this tutorial.
Editing web.Dockerfile
Vapor Cloud uses Docker to build and run your app, and to handle HTTP requests on port 80. For this to happen, you’ll need to include a web.Dockerfile in the root folder of your project. web.Dockerfile sets up everything your app needs to run in a Docker container, including setting the Swift version and OS for the container. A number of prefabricated files are available at https://github.com/vapor-cloud/docker.
For the Vapor TIL app, you need the Swift version of web.Dockerfile. The root folder of the Vapor TIL app already contains this file. However, you need to make two changes to the template version of the file to work with your app.
Edit your local copy of web.Dockerfile and uncomment the following two lines:
COPY --from=builder /app/Public ./Public COPY --from=builder /app/Resources ./Resources
This tells the app to load resources from the Public directory, and also sets up your Vapor app to use Leaf templating when deploying to the cloud.
Once you’ve made these changes, push your local version of web.Dockerfile to your GitHub fork. Open web.Dockerfile in your Github repo, to verify that your changes made it there.
With your fork of the app in place, you now need to create an Application on Vapor Cloud for your app.