Chapters

Hide chapters

Server-Side Swift with Vapor

Third Edition · 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

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

Amazon Web Services (AWS) is by far the largest Cloud provider today. It provides many 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 an 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 apt-get update
sudo apt-get upgrade -y

Install Swift

To build your Vapor app, you must install Swift on your EC2 instance. Swift supports a number of Linux platforms, including Ubuntu and CentOS. Visit https://swift.org/getting-started/ for details on installing for your platform.

wget https://swift.org/builds/swift-5.3.2-release/ubuntu2004/swift-5.3.2-RELEASE/swift-5.3.2-RELEASE-ubuntu20.04.tar.gz
tar -xzf swift-5.3.2-RELEASE-ubuntu20.04.tar.gz
sudo apt-get install binutils git gnupg2 libc6-dev \
  libcurl4 libedit2 libgcc-9-dev libpython2.7 \
  libsqlite3-0 libstdc++-9-dev libxml2 libz3-dev \
  pkg-config tzdata zlib1g-dev -y
echo "export PATH=/home/ubuntu/swift-5.3.2-RELEASE-ubuntu20.04/usr/bin:${PATH}" >> .profile
source .profile
swift --version

System Memory

The Swift compiler can use a lot of memory. Small cloud instances, such as a t2.micro, don’t contain enough memory for the Swift compiler to work. You can solve this problem by enabling swap space. In Terminal, enter the following:

sudo su -
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
exit

Setting up your application

To set up your application, you will first clone it from GitHub. To build the TILapp example from the rest of the book, enter the following commands:

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

Setting up a PostgreSQL server

For your database, you will use Amazon Relational Database Service (RDS). This AWS database service supports several 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 -y
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. Most versions of Linux that Swift — and, therefore, Vapor — support use a service called systemd to accomplish this.

sudo su -
DATABASE_HOST='<your AWS RDS endpoint>'
DATABASE_USERNAME='vaportil'
DATABASE_NAME='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'
SIWA_REDIRECT_URL='test'
IOS_APPLICATION_IDENTIFIER='test'
WEBSITE_APPLICATION_IDENTIFIER='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. You can add load balancers and custom DNS names with TLS certificates. There are also other deployment options, such as running in Docker or even using AWS Lambda. Covering all of AWS would be a whole book in itself! 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