Deploy Swift HTTP Serverless Container to Google Cloud Run in 5 minutes

Alfian Losari
Apr 23 · 5 min read

At Google Cloud Next 2019, Google has just introduced Google Cloud Run. It’s a serverless computing service, which means we don’t have to manage the infrastructure by ourselves. It is based on containers, so we only need to provide the Dockerfile that contains our HTTP server application and deploy. The service will be invocable via the HTTP request from the client and the payment will be using pay per use model.

There are many features that Google Cloud provides for the Cloud Run, such as:

  1. Fast autoscaling, automatically scales our app up and down based on the traffic.
  2. Container based, using Docker container to build and deploy our service.
  3. No DevOps. All we need to do is deploy our container and Google Cloud will manage all the rest for us.
  4. Based on Knative, the portability means we can also deploy to Google Kubernetes Engine (GKE Cluster) across platforms.
  5. Integrated logging and monitoring with StackDriver.
  6. Ability to use our own custom domains.

You can learn more about the Cloud Run directly from Google with the official link below.

What we will build

  1. Prepare our HTTP Swift app.
  2. Build the Dockerfile.
  3. Upload to Container Registry.
  4. Deploy container to Google Cloud Run.

Setting up Google Cloud

  1. Register and Sign in to the Google Cloud Platform. (https://console.cloud.google.com/).
  2. Download and install Google Cloud SDK to your machine. (https://cloud.google.com/sdk/install).
  3. Create a new project from the Google Cloud Console.
  4. Make sure to follow all these steps in here to activate the Google Cloud Run API for your project. (https://cloud.google.com/run/docs/setup).

Prepare our HTTP Swift Application

mkdir hello-swift-cloudrun
cd hello-swift-cloudrun

Inside the directory, create a new swift package.

swift package init --type executable

Next, open Package.swift and copy the following code into the file. We will add the Swifter tiny HTTP server library as the dependency to run our HTTP Server in Swift.

/ swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescriptionlet package = Package(
name: "hello-swift-cloudrun",
dependencies: [
.package(url: "https://github.com/httpswift/swifter.git", .upToNextMajor(from: "1.4.6"))
],
targets: [
.target(
name: "hello-swift-cloudrun",
dependencies: ["Swifter"]),
.testTarget(
name: "hello-swift-cloudrunTests",
dependencies: ["hello-swift-cloudrun"]),
.]
)

Next, open the main.swift file from the Sources directory. Copy the following code.

Here are the things that it performs:

  1. Register 2 routes /html and /api. These routes will return the response containing the current date that is formatted using DateFormatter. The html path will return the text in HTML format, while the api path will return the response in JSON format.
  2. Retrieve the PORT from the environment variable.
  3. Start the HTTPServer passing the PORT to listen for the request in the port.

Try to build and run the server by typing these commands the terminal.

swift build
swift run

To test, open your browser and navigate to the address http://localhost:8080/html. You should see the text printed with the current time in your browser.

Swift HTTP Server

Build the Dockerfile

FROM ibmcom/swift-ubuntu:latestWORKDIR /usr/src/appCOPY . .RUN swift build --configuration releaseCMD [ "swift", "run", "--configuration", "release"  ]

This will copy all the file to the container image, then run the swift build using release configuration. It will also run the server after the build has been finished.

Upload to Container Registry

gcloud builds submit --tag gcr.io/[PROJECT-ID]/hello-swift-cloudrun

Wait for the container builds process to finish and then uploaded to container registry. It will print the success message to the terminal.

You can check the list of the successfully uploaded container using this command.

gcloud container images list

Deploy Container to Google Cloud Run

gcloud config set run/region us-central1
gcloud beta run deploy --image gcr.io/[PROJECT-ID]/hello-swift-cloudrun --memory 512M --allow-unauthenticated

Here are several things that it performs:

  1. Set the region of deployment to us-central1.
  2. Deploy the image from the Container Registry path in this case hello-swift-cloudrun.
  3. Configure to use 512M of memory to use.
  4. Allow unauthenticated request to invoke the HTTP.

You can configure other things, from memory, concurrency, and request timeout. Check the link below

After the deployment finished successfully, the terminal will print the URL endpoint of the deployed service that we can use. Open your browse and navigate to:

  1. https://$URL/html
  2. https://$URL/api

Monitoring through Dashboard

https://console.cloud.google.com/run

Cloud Run Dashboard

In here you can also manage the custom domains, delete and create services, and view the logs of your deployed services.

!!!Make sure to delete all the resources that you have created after you finish this article to avoid billings!!!

Conclusion

That’s it, in just a simple steps we have deployed our serverless backend using Docker to the Google Cloud Run managed autoscaling service. The serverless paradigm provide us speed and reliability to execute rapidly as the size of our application grows over time ⚡️⚡️⚡️.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Alfian Losari

Written by

Mobile, Web, a bit of backend Software Developer and Lifelong Learner. Currently building super app @ Go-Jek.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade