swift-serverless-rest-api-aws-lambda-swift-runtime-amzlinux2-arm64
Swift Server Implementation - RESTful APIs, AWS Lambda Serverless For Swift Runtime amazonlinux: AWS Lambda + API Gateway deployed on Graviton arm64 build **swift:5.6.2-amazonlinux2-docker image**
Using Xcode 13+, Utilizing Latest features of Swift Programming Language
Utilizing New AsyncLambdaHandler feature to support latest structured concurrency pattern:
**"async/await"** (Swift 5.5+)
For any detail about Server-Side-Swift and Lambda-Serverless solutions, Please check [here](https://docs.google.com/document/d/1GlGv0avpbpE6lqJbpxz5iHgaiPMC5E543rYGg5Ionbw/edit?usp=sharing)
Just upload "lambda.zip" to AWS Lambda via S3 bucket file (direct lambda upload limit is 10MB)
AWS CLI command to update (IAM Role needs to be configured with required lambda & s3 permissions ):
aws lambda update-function-code --function "$lambda_function_name" --s3-bucket "$s3_bucket_name" --region=us-east-1 --s3-key lambda.zip
Test the welcome route: In Lambda Test Tab with following APIGateway Event
{ "routeKey": "GET /welcome", "version": "2.0", "rawPath": "/welcome", "requestContext": { "accountId": "", "apiId": "", "domainName": "", "domainPrefix": "", "stage": "", "requestId": "", "http": { "path": "/welcome", "method": "GET", "protocol": "HTTP/1.1", "sourceIp": "", "userAgent": "" }, "time": "", "timeEpoch": 0 }, "isBase64Encoded": false, "rawQueryString": "", "headers": {} }
GET /welcome -> json resonse -> {"message": "swift rest-api server is running"}
Set Environment variable for MongoDB Cloud database via Lambda configuration -> Environments Variables: DATABASE_URL "mongodb:url_connect_database"
We can use MongoDB Atlas Cloud managed solution - Free Shared Instance
e-Route
Connect an API gateway with entity-based Route:
[ ANY ] /api/e/{entity}
[ ANY ] /api/e/{entity}/{id}
CRUD: Any Entity as json request can be created/updated/read/deleted, to/from the MongoDB database
exp:
Make CRUD Operations simpler, with options for custom object validation. APIGateway also provide custom json object mapping for request validation
[ ANY ] /api/e/{entity}
[ POST ] /api/e/users
[ GET ] /api/e/users?sort=created_at&order=desc&limit=20
[ GET ] /api/e/users/id
[ PUT ] /api/e/users/id
[ DELETE ] /api/e/users/id
JWT Authorization:
[ POST ] /api/login -> {"access_token": "eyD7uitr4em......."}
[ GET ] /api/user/id :::: headers: { "Authorization": "Bearer eyD7uitr4em.......", "content-type": "application/json" }
- Strongly typed feature of Swift for stable and faster development
- Easy deployment to AWS Serverless Lambda without any server maintenance
- Build in security by using APIGateway to access the server RESTful APIs
- MongoDB NoSql Schema-less database integration to consume json-based services
- No DataModeling used: So No data migration changes require when adding new (key,value) pair to a collection / table.
- Any json can be added to any collection - completely dynamic system
- Any custom validations can be used for any specific resources like: users,bookings, etc.
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:
- Compile the function as an executable file that runs on Amazon Linux 2.
- Include all dependencies and libraries with the bootstrap file.
- You’ll use Docker to do that, and i have provided 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.
Working On
- JWT authorization via:
- APIGateway Authorizer for any specific route
- Custom implementation is pretty much dynamic. Using JWT Library dependency but it may add an additional cold start time for boot up.
CI/CD for one-click/command deploy updates to Lambda func- Verify Apple claims in WWDC: Static Linking for Linux Optimize Lambda Performance
- Static Linking for Linux to achieve:
- 33% faster cold start time on AWS Lambda
- 40% faster invocation time for APIGateway Lambda
- 33% faster cold start time on AWS Lambda
- Static Linking for Linux to achieve:
Any Feedback, suggestion, improvisation is welcomed
Created By,
Furqan
(Software Developer / Solution Architect)
Email: furqan.cloud.dev@gmail.com