Deploying Server Side Swift on AWS

Jonathan Guthrie
Server Side Swift and More
6 min readMar 16, 2017

Once you’ve finished developing your app, the next stage is to make it available it to your audience… so the natural desire is to deploy it to a server.

While it’s possible to deploy a Swift application to any Ubuntu 16 server, in this article I’m going to focus on how to deploy with the Perfect Assistant to an AWS instance.

There are three prerequisites if you want to follow along: The Perfect Assistant application, a working Docker application on your Mac, and an AWS account.

AWS Setup

Some things to gather from AWS:

  • Access Key ID & Secret
  • A public/private key pair
  • A Security Group

Access Key & Secret

The steps to create a new Access Key & Secret:

  • If you don’t have an IAM user, create one, making sure you add the “Programmatic access” checkbox, the click the “Attach existing policies directly”, and select the “AmazonAPIGatewayInvokeFullAccess & AmazonEC2FullAccess” properties. This will create a new user account with a key & secret. Make a note of them!
  • If you already have an IAM user account, choose the name of the desired user, and then choose the Security Credentials tab. Click the “Create access key” button, and record the key’s id and secret.

Public / Private Key Pair

Next we want to create a new Public/Private key pair. Go to the “EC2” section, click on “Key Pairs” in the navigation, and click the “Create Key Pair” button. Make sure you download it after! Put the downloaded file (which ends in “.pem”) into “~/.ssh/“. This makes it accessible to applications that use SSH to connect to remote systems, like the Assistant.

Create a new Key Pair

Security Group

Next, click “Security Groups” in the navigation, and “Create Security Group”. Give your new group a brief name and description, and then with the “inbound” tab selected, click “Add Rule”.

For this demo I suggest you add port 8181 and select “Anywhere” in the source dropdown, and make sure you add SSH access from your current IP address.

Configuring Perfect Assistant

OK back in the Perfect Assistant, on the Welcome screen, click the “Configure EC2 Credentials” button in the EC2 box. This opens a new window where you can add new credentials.

Click “Create”, the give your new credential setting a name, and enter the Access Key and Secret that we got from AWS. The “Region” should be something you set that is geographically close to you, or the majority of your audience. Pressing “Save” will lock the configuration in.

Setting up the demo app for deployment

Next, let’s grab the “Perfect App Template” from GitHub. Still in the Welcome screen, click on “Create New Project”, the “Custom Repository URL”. Find a home for your project by clicking “Browse” next to the location, and then paste in the URL for the template: “https://github.com/PerfectlySoft/PerfectAppTemplate.git"

Leave the “Integrate Linux builds with Xcode project” checked, as we are targeting Linux deployment in this phase.

Once you click “Save” the system will start it’s initial clone of the project for the macOS side.

Doing a Linux build is as simple as clicking the “BUILD: Linux” button. What this is doing behind the scenes is getting a Docker container ready, cloning the project’s dependencies into a sandboxed location, and doing a Linux build that is sandboxed as well.

Deployment configurations

Now we’ve made sure it builds ok on Linux, we can click the “Deploy” button. This opens up a sheet that allows you to select a Deployment Configuration, or create a new one. Let’s create a new one for AWS.

Name the config, and then select the EC2 Credentials we created earlier. Select an SSH key — once again this is the “.pem” file we got earlier. If you don’t see any “Available Instances” listed, press “Reload”. It’s important to note here that the instances listed are the “Perfect compatible” AWS instances. If you don’t see one, or wish to create a new one, click the “Add…” button.

Here, you can define how many instances to create, and then the details for the instances you’re creating.

Select the Security Group we created earlier, and an instance type, availability zone and subnet. For the Key Pair, you should see just the one that you entered before. Similarly, right now there is just one “Image ID” you can select… Later they’re may be more as specific variants are added.

Pressing “Launch” will trigger AWS to create the instance or instances you have requested. You’re returned to the previous sheet — you may have to wait a minute or two for AWS to spin up your new instance, and then press “Reload” to get it. Unfortunately, when it does appear it won’t be named, so if you have named all your instances in the AWS console, it’s going to be the one that is unnamed.

Select your new instance, and press “Save”.

And, now… Deploy

You may need to go back and reselect your project in Assistant, but now when you click “Deploy” you will see your new config, and confirming that will do a new release build, and then connect to each instance you selected in the configuration, and upload the webroot, config and the executable, then create and enable the service file and start the service.

In the console output you’ll see it mentions each instance’s IP address — let’s copy that and go to it (on port 8104) in the browser. The URL will look something like this — http://123.123.123.123:8104

For examples visit GitHub.com/perfectexamples — there is an ever growing library of examples and demos there.

And of course if you’re looking for live help from our awesome community, join our Slack channel via www.perfect.ly

--

--

Jonathan Guthrie
Server Side Swift and More

Developer Evangelist, Musician, and Active Activist... Newmarket ON, Canada