AWS Lambda Tutorial for Swift: Getting Started

Swift is now available in the world of serverless functions via AWS Lambda! Get started deploying your first on-demand serverless function with our AWS Lambda for Swift tutorial. By Ralph Kuepper.

Leave a rating/review
Download materials
Save for later
Share

Swift is now available in the world of serverless functions, specifically Amazon Web Services (AWS) Lambda. In this AWS Lambda Tutorial for Swift, you’ll write a simple currency conversion function that takes a value and returns the corresponding value for another currency.

Lambda is a platform provided by AWS that allows functions to run on demand. Multiple functions with a sole purpose support an application by responding to events as they happen — resources are computed, a server is spun up and your code runs using AWS resources. This is an excellent solution for applications with unpredictable workloads and scalability requirements.

Your project will be a serverless function for AWS Lambda using Swift. Along the way, you’ll learn how:

  • Swift runs on AWS Lambda
  • To set up your AWS account
  • Writing an AWS Lambda function in Swift works
  • To connect your AWS Lambda function to the world
Note: This tutorial assumes you have some basic knowledge of Swift and you know how to work with related Swift tools (like Xcode). If you don’t, check out Programming in Swift: Fundamentals. Additionally, you need an AWS account ready to go. If not, go here and sign up: https://aws.amazon.com. Be aware that costs may apply.

Getting Started

Download the starter project by clicking on the Download Materials button at the top or bottom of this tutorial.

You’ll also want to install Docker or use its web interface when prompted later in the tutorial.

Before you start writing actual code, you’ll need to set up your AWS account and its services. If you’re using an account that’s already in use for other projects or has customized settings, you may need to tweak a few settings and permissions. But, every new account will automatically create all the permissions you need for this tutorial.

You’ll need to set up the following services:

  • AWS Lambda
  • API Gateway

AWS Lambda is the platform this function will run on, while API Gateway connects the function to the world wide web.

Creating your AWS Lambda Function

Start by setting up your AWS Lambda function. Navigate to the Lambda section within AWS. When you first log in to AWS, you’ll find it either in the list of suggested services or by selecting it in the main menu, like this:

Finding Lambda in AWS after logging in.

Note: If you don’t find AWS Lambda right away, you can search for it by typing Lambda in the search box.

Click on Create function and make sure you have Author from scratch selected.

Now fill out the fields of this form with the following values:

  • Function name: eurCurrencyConverter
  • Runtime: Provide your own bootstrap on Amazon Linux 2
  • Permissions: Create a new role with basic AWS Lambda permissions

Creating a new lambda function

Click on Create function and wait for AWS to create the function. You can leave the function alone for now.

Creating Your API Gateway

Next, you’ll create an API Gateway to use for interacting with the AWS Lambda function.

Click on Services and select API Gateway. If you have trouble finding it, you can type in the beginning of API Gateway, as shown below:

Finding API Gateway in AWS

On the API Gateway, create a new HTTP API by selecting Build in the HTTP API section:

Create an HTTP API Gateway

When prompted, name the API “eurConversion” and click Add integration. In the drop-down, select AWS Lambda, and fill out the fields to match your existing AWS Lambda function — AWS will auto-complete for you. It should look like this when done:

Create an HTTP API Gateway

Note: Your AWS Region may be different, and the AWS Lambda Version may have changed since this article was written.

Configuring Your API’s Routes

Click Next and configure the routes. Set the path to /convert and set the integration target to the newly configured AWS Lambda integration. Leave the method as ANY since the function serves POST and GET requests.

AWS API Gateway - configure routes

Click Next again and then define stages like so:

AWS API Gateway, Define stages

Click Next one more time, confirm your API settings by clicking Create, and your API is ready.

Now that you’ve created a home for your AWS Lambda function, it’s important to understand how Swift and AWS Lambda work together.

Using Swift with Lambda and AWS

AWS Lambda supports a couple programming languages natively. This means you can upload the source code directly and AWS Lambda can compile it on the fly. Unfortunately, this isn’t yet the case for Swift. So, for Swift to run on AWS Lambda, you need to:

  1. Compile the function as an executable file that runs on Amazon Linux 2.
  2. Include all dependencies and libraries with the bootstrap file.

You’ll use Docker to do that, and it’ll provide you with a convenient ZIP file of your function and its dependencies that you upload to AWS Lambda.

AWS Lambda does come with a few limitations:

  • AWS Lambda functions run for a maximum of 15 minutes.
  • A function may take a few extra seconds to run for the first time since AWS Lambda is booting the function. This is also referred to as a “cold start”.
  • AWS Lambda is, by definition, stateless; there’s no shared memory among AWS Lambda functions.
  • AWS Lambda functions can perform a variety of tasks, so not every AWS Lambda function is a public function. Depending on your use case, you may not even need internet access.
  • AWS Lambda functions can use EventLoops, but they’re usually used within a specific context only.

Swift has released the official AWS Lambda runtime, which provides a convenient framework to develop lambda functions. This runtime is the basis for the function you’ll develop in this tutorial.

Testing AWS Lambda Functions Locally

When you develop AWS Lambda functions, testing them is a little different than developing regular applications. All functional tests involve calling the URL: http://localhost:7000/invoke as a POST request. Internally, AWS Lambda is calling the function with parameters depending on the setup. If you’re using AWS Lambda with a trigger from an SQS queue, you’ll deal with different parameters than when using it behind API Gateway.

Note: To enable the local testing server, set an environment variable, LOCAL_LAMBDA_SERVER_ENABLED, with a value of true for the runtime to start in this mode. You can do this by editing your project’s Run scheme. This isn’t necessary to complete this tutorial since you’ll be using your Lambda API Gateway.

Now it’s time to write some code.