Connecting Swift AWS Lambda with SQLite

Tirta Adi Gunawan
Jan 26 · 5 min read
Photo by Dmitry Mashkin on Unsplash

Last year in 2020 Swift supported AWS lambda officially and apple made video on WWDC 2020 Swift works with AWS Lambda.

Currently on my working project, we have rich iOS app, by the mean of rich is all the functions for user is written in iOS swift, generating PDF, business validation, calculation, etc. It’s not like client-server application like the other used to make.

So with this news, we can try to move those function into AWS Lambda or maybe into a backend service, so those function is available for other to use it. Of course there will be some obstacles, but I share those problems in here and this article is focus on integrating swift SQLite on AWS Lambda, what the thing we need to consider before make our swift code base support multi platform (iOS, Mac, Linux, android 😝).

In this article as mentioned at the title I’ll share how to Swift AWS Lambda supporting SQLite for source code you can check out at my github.

First we need to consider, what is the purpose of using SQLite on your project, is it for adding record like ordinary RDBMS? if that so, you need to think twice because AWS lambda storage is only available for 512MB only, my suggestion is you need to erase that idea and using other service like AWS DynamoDB or RDS.

if you only need to read only, because so many static data inside the DB and the size is less than 512MB, there you are, you can continue your swift code base to be available on AWS Lambda, after you deploy it to AWS Lambda and hit some function, you need to considering the performance cost as well, my suggestion is first step is targeting for easiest function and see the performance of that function if that result is passed the estimation of your users data, you can continue to proceed with lambda.


  1. Xcode 12, because the swift package manager supporting for resource it start from xcode 12, to be precise it needs swift-tools-version:5.3
  2. Docker, we need this to make container for our lambda.

I’ll explain about package.swift

products: it have executable with name AccountServiceAWS, this need to match with the Source folder directory, you may also need to see library tag for your project, for executable meaning that the result of execution will be running program, if library the result would be library to use.

Noted : The executable product unsupported to have unit test, you may one to create library for your business code and executable for the things that depends on AWS lambda run-time, as for sample purpose I not including this as consideration here.

dependencies: all required dependencies is here, for as now it only need for AWS lambda runtime, library for AWS lambda and Soto library for connecting to other AWS service for this sample is S3.

targets: Targets are the basic building blocks of a package, so in here it only containing 1 target with all dependencies and resources needed, systemLibrary is library installed on the system in our case we have SQLite3 this is C library and we need to install it on our docker image later, as you can see there is folder CSQLite3 on Sources folder, testTarget for unit test.

if your using Xcode you can run the project directly and verify it with curl :

$ curl --header "Content-Type: application/json" \
--request POST \
--data '{"id": 1}' \

it will response like this


this because we have account.db as a resource in Package.swift and because we run it on mac, but if we deploy to AWS Lambda with this state, we will got error for resource_bundle_accessor, so between mac and AWS lambda having different behaviour.


To cope with this we need to upload accountData.db into S3 bucket and download it and save it into /tmp folder on AWS Lambda which is having only 512MB storage limit.

in order to connecting our function to AWS S3 we need to have Soto and S3 access key, after you have it you can put it on SharedResource.swift

static public let s3BucketName = "calculationservice"

s3BucketName is your stored accountData.db bucket and you need to replace as well accessKeyId and secretAccessKey with yours to be able access the accountData.db.

after all this step you can go to docker part.

first you need to build docker container with this.

$ docker run \
--rm \
--volume "$(pwd)/:/src" \
--workdir "/src/" \
swift:5.3.1-amazonlinux2 \
/bin/bash -c "yum -y install sqlite-devel && swift build --product AccountServiceAWS -c release -Xswiftc -static-stdlib"

so it using swift:5.3.1-amazonlinux2 with installed sqlite-devel, AccountServiceAWS is the product we have.

after this success we need to running this script

$ ./scripts/ AccountServiceAWS

this one is creating package for upload it to AWS Lambda.

after that done, we can see the result on .build/lambda/AccountServiceAWS/

this you need to upload it to S3 because the maximum direct upload to AWS lambda is 10MB, and our is 30MB.

after uploaded you can back to AWS lambda and go to function code, upload a file from amazon S3.

if this success, you can test the function with Test Events and put it request like this.

Hit the Test and you will see the result same as you see on your machine terminal.


Done and thank you 🙏

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To stay up to date on other topics, follow us on LinkedIn.

Tirta Adi Gunawan

Written by

📱 iOS Developer |

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To stay up to date on other topics, follow us on LinkedIn.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store