top of page

Swift Lambda Applications λ

This will be part II of CI/CD deployment:


To recall the reference used in there:

But you and I cannot agree more that UI Consoles are a bit annoying and 🐌x2. There are solutions for it:

- Serverless Framework <-- ✅ x5⭐️

brew install serverless
code serverless.yml
serverless deploy --config "./serverless.yml" --stage dev 

Well, sort of... let me catch my breath and `map` it .. this is very exciting, and it's going so fast... did you know Swift Docker images support ARM already?



😮‍💨

You will need the following:

Dockerfile

FROM swift:5.6-amazonlinux2
RUN yum -y install git jq tar zip openssl-devel

serverless.yml

provider:
  name: aws
  runtime: provided.al2 # <--- Important!
  region: us-east-2
  architecture: arm64 # <--- Important!

keep in mind that I'm leaving out the upload package format, which can be .zip or a docker image hosted in AWS ECR


I used the same build scripts found in swift-aws-lambda-runtime/Examples/Deployment


I modified the following lines, though

docker run --rm -v "$workspace":/workspace -w /workspace/Examples/Deployment builder \       
bash -cl "swift build --product $executable -c release"
echo "done"

echo"-------------------------------------------------------------------------"echo"packaging \"$executable\" lambda"echo"-------------------------------------------------------------------------"

docker run --rm -v "$workspace":/workspace -w /workspace/Examples/Deployment builder \       
bash -cl "./scripts/package.sh $executable"

to

docker run --rm -v "$workspace":/workspace -w /workspace/ builder \
       bash -cl "swift build --product $executable -c release"
       
docker run --rm -v "$workspace":/workspace -w /workspace/ builder \
       bash -cl "./scripts/package.sh $executable" 

and now you can run the command below from your MacBook M1

./scripts/serverless.sh

side_note: Before swift:5.6-amazonlinux2 I set the scripts within a GitHub Action.. but the scripts were building the image on an Ubuntu machine, and the arch was set for arm64, just keep an 👀 in which computer you are running


the above highlights where I spent some time reading, for which I enjoy figuring out how things work. I hope this is one of those reads you will find as trying to accomplish the same as me: Build Swift Serverless functions for any Cloud ☁️


Demo... 📽


I implemented a Serverless application to send a push notification to an iOS application.


Update

The above was a high-level demo of code, implementation, and integration of a Lambda function. With that said, everyone has their use case, and I'm just starting to grasp and simplify the process of the common use case. Per AWS, the following seems to be the common use case:


An AWS Lambda application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. You can use AWS CloudFormation and other tools to collect your application's components into a single package that can be deployed and managed as one resource. Applications make your Lambda projects portable and enable you to integrate with additional developer tools... - AWS Docs

For myself, as a developer, this is how I interpreted it:


I would like to have different functions related to the same project and deploy them as an UPSERT strategy.

For the Swift Lambda application, this could look like the following:

- Single repository

- Many executables map to Lambda Functions

- Pushing the stack

a. Single stack application grouping those executables, which would do the UPSERT

b. Linking the Lambdas to an existing stack (which could be part of a team)


It was great to learn how to perform the above as part of Cloud DevOps on my projects.


Here is a slide modification of Fabian's scripts:

Note: you still have to write/map your serverless.yml file

#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftAWSLambdaRuntime open source project
##
## Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

set -eu

DIR="$(cd "$(dirname "$0")" && pwd)"

executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') )

if [[ ${#executables[@]} = 0 ]]; then
    echo "no executables found"
    exit 1
fi 

echo "multiple executables found:"
for executable in ${executables[@]}; do
    $DIR/build-and-package.sh "$executable"
done

serverless deploy --config "./serverless.yml" --stage dev --verbose

Comments


bottom of page