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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
AWS Lambda Tutorial for Swift: Getting Started
20 mins
- Getting Started
- Creating your AWS Lambda Function
- Creating Your API Gateway
- Configuring Your API’s Routes
- Using Swift with Lambda and AWS
- Testing AWS Lambda Functions Locally
- Writing a Swift App for AWS Lambda
- Creating Your Function’s Model
- Writing Your Function’s Body
- Getting the Function Running on AWS
- Uploading Your Function to AWS Lambda
- Using Additional Services With AWS Lambda
- Where to Go From Here?
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.
Run the command from above and wait until the script finishes. Finally, run the following command:
cp .build/lambda/EURCurrencyRate/lambda.zip .
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.
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.
Copy and paste the URL into your browser and append /convert?amount=10.00. You’ll see the desired output like this:
Your first lambda function is live and deployed! :]
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!