Chapters

Hide chapters

Server-Side Swift with Vapor

Third Edition - Early Acess 1 · iOS 13 · Swift 5.2 - Vapor 4 Framework · Xcode 11.4

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Section I: Creating a Simple Web API

Section 1: 13 chapters
Show chapters Hide chapters

33. Deploying with AWS
Written by Jonas Schwartz

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Note: This update is an early-access release. This chapter has not yet been updated to Vapor 4.

Amazon Web Services (AWS) is by far the largest Cloud provider today. It provides a number of service offerings which simplify the deployment and maintenance of applications. In this chapter, you’ll learn how to use a few of these to deploy a Vapor app.

Before starting

To perform the steps in this chapter, you must have an AWS account. If you don’t already have one, follow the instructions at https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/ to create one.

Setup your AWS instance

Your first step is to start an EC2 instance. EC2 is AWS Virtual Machine product. This gives you a plain Linux machine you can use to run your Vapor application.

chmod 600 /path/to/your/ssh/key
ssh -i /location/to/your/ssh/key ubuntu@your-aws-ip
Host vapor-til
    HostName <your public IP or public DNS name>
    User ubuntu
    IdentityFile </path/to/your/key/file>
ssh vapor-til
sudo su -
apt-get update
apt-get upgrade

Install Swift

To build your Vapor app, you must install Swift on your EC2 instance. The easiest way to install and update Swift on Ubuntu is to use the Vapor APT repository.

What is APT?

APT (Advanced Package Tool) is the default package manager for Debian-based Linux systems, like Ubuntu. The Vapor team maintains an APT repository for Swift and the Vapor Toolbox.

Install Swift from APT

To install Swift from the Vapor APT, enter the following commands:

# 1
wget -q https://repo.vapor.codes/apt/keyring.gpg -O- | \
  apt-key add -
# 2
echo "deb https://repo.vapor.codes/apt $(lsb_release -sc) main"\
  | tee /etc/apt/sources.list.d/vapor.list
# 3
apt-get update
# 4
apt-get install swift ctls
swift --version

Setting up your application

To set up your application, you will first clone it from GitHub.

# 1
exit
# 2
git clone https://github.com/raywenderlich/vapor-til.git
# 3
cd vapor-til
# 4
swift build -c release
./.build/release/Run

Setting up a PostgreSQL server

For your database, you will use Amazon Relational Database Service (RDS). This AWS database service is based on AWS S3 and can emulate a number of popular relational database systems including PostgreSQL.

Installing and configuring nginx

nginx is a popular web server, typically used as a proxy server in front of other web apps. For Vapor apps, this is useful because it provides additional features such as compression, caching, HTTP/2 support, TLS (HTTPS) and more.

sudo su -
apt-get install nginx
server {
  listen 80;

  root /home/ubuntu/vapor-til/Public;
  try_files $uri @proxy;
  
  location @proxy {
    proxy_pass http://localhost:8080;
    proxy_pass_header Server;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 3s;
    proxy_read_timeout 10s;
  }
}
# 1
rm /etc/nginx/sites-enabled/default
# 2
ln -s /etc/nginx/sites-available/vapor-til \
  /etc/nginx/sites-enabled/vapor-til
# 3
systemctl reload nginx

Running your app as a system service

You want your app to run when your instance boots and to restart if it crashes due to a critical error. The easiest way to accomplish this is to integrate it as a system service. The versions of Linux that Swift — and, therefore, Vapor — supports use a service called systemd to accomplish this.

sudo su -
DATABASE_HOSTNAME='<your AWS RDS endpoint>'
DATABASE_USER='vaportil'
DATABASE_DB='vaportil'
DATABASE_PASSWORD='<your chosen password>'
SENDGRID_API_KEY='test'
GOOGLE_CALLBACK_URL='test'
GOOGLE_CLIENT_ID='test'
GOOGLE_CLIENT_SECRET='test'
GITHUB_CALLBACK_URL='test'
GITHUB_CLIENT_ID='test'
GITHUB_CLIENT_SECRET='test'
# 1
[Unit]
Description="Vapor TILapp"
After=network.target

# 2
[Service]
User=ubuntu
EnvironmentFile=/etc/vapor-til.conf
WorkingDirectory=/home/ubuntu/vapor-til
# 3
Restart=always
# 4
ExecStart=/home/ubuntu/vapor-til/.build/release/Run \
  --env production

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start vapor-til.service
systemctl enable vapor-til.service
systemctl status -l vapor-til.service
systemctl restart vapor-til.service
systemctl stop vapor-til.service

Where to go from here?

You now have the basics of how to set up a Vapor app on AWS. There are many more things AWS allows, such as scaling, IP pooling, automatic backups, replication and so on. Spend some time with the AWS documentation and tutorials to learn more.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now