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
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Getting the Function Running on AWS

OK, it’s time to get the function up and running. To deploy to AWS Lambda, compile your application into a binary file that can run on AWS Lambda. The best way to do this is by compiling it on the correct Linux distribution, which you can do through Docker. Amazon Linux 2, the runtime behind AWS Lambda, is available for Swift, and so the command to build your application using Docker looks like this:

docker run \
  --rm \
  --volume "$(pwd)/:/src" \
  --workdir "/src/" \
  swift:5.2-amazonlinux2 \
  /bin/bash -c "yum -y install libuuid-devel libicu-devel libedit-devel libxml2-devel sqlite-devel python-devel ncurses-devel curl-devel openssl-devel libtool jq tar zip && swift build --product EURCurrencyRate -c release && scripts/package.sh EURCurrencyRate"

This commands does the following:

  • run a Docker container. --rm tells Docker to delete the container when the container finishes.
  • Use your src folder in the working directory ($(pwd)) as a volume.
  • Set /src/ as the work directory (–workdir).
  • Use the Amazon Linux 2 image with Swift pre-installed.
  • Then run commands to install required dependencies and compile the code. Afterward, run a small script, package.sh, that Apple provided as part of the AWS Lambda runtime framework. It creates a ZIP file that contains all the files AWS Lambda needs.

Compiling using Docker complete!

Run the command from above and wait until the script finishes. Finally, run the following command:

cp .build/lambda/EURCurrencyRate/lambda.zip .
Note: This copies the resulting lambda.zip file into your main folder so you can grab it from there.

Uploading Your Function to AWS Lambda

Now head back over to AWS Lambda and click on the function to open it there. Select Upload a .zip file in the Function code section. Choose the ZIP file you created and upload it. And that’s it! Your lambda function is ready now.

Upload a zip file to the lambda function.

Go back to the API Gateway you created by clicking services and searching for “API Gateway.” Open the API Gateway resource you created and find the “Invoke URL” section.

Open the invoke URL in your API

Copy and paste the URL into your browser and append /convert?amount=10.00. You’ll see the desired output like this:

The output you expect from the AWS Lambda function.

Your first lambda function is live and deployed! :]

Note: If you receive an internal server error, there may be an error with your account’s permissions. This can usually be addressed by following the troubleshooting steps in the Amazon Knowledge Center.

Using Additional Services With AWS Lambda

If you’ve worked with AWS before, you’re well aware of other services you might have an interest in. The ones that are often used in connection with AWS Lambda are SNS, SQS and S3. For those — but also for many other AWS services — check out Soto. It’s the AWS SDK for Swift and can integrate nicely with the Swift Lambda Runtime.

It’s likely you’ll need some configuration via stored credentials in your AWS Lambda function. The easiest way to do this is by pulling these credentials from a Parameter Store through Soto. That way, your credentials aren’t saved in any files but stay in the cloud.

You may also want to use AWS Lambda functions in a different context — for example, as background workers. You can use most of the Server-Side Swift packages available to connect to databases.

Where to Go From Here?

You can download the final project by clicking the Download Materials button at the top and bottom of this page.

In this tutorial, you learned how to create an AWS Lambda function using Swift. Swift has a growing ecosystem of packages and projects, and AWS Lambda is compatible with many of them. If you want to explore writing web applications that might run on AWS Lambda with a more holistic framework, check out Server-Side Swift with Vapor.

If you’re new to web development but have experience with Swift, you’ll find it’s easy to create robust, fully featured web apps and web APIs with Vapor 4.

If you have any questions or comments, please join the forum discussion below!