Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save profburke/77978cccd3fab7af6ac6745ddd37b418 to your computer and use it in GitHub Desktop.
Save profburke/77978cccd3fab7af6ac6745ddd37b418 to your computer and use it in GitHub Desktop.
== How to Implement an AWS Lambda function using Swift 4
1. Use the appropriate docker image to build a Swift 4 executable.
docker run -it -v "$(PWD):/app" doctorimpossible/swift4ubuntu bash
mkdir demo; cd demo
swift package init --type executable
:: edit Sources/demo/main.swift
swift build -c release --build-path .build/native
# grab copies of all necessary dynamic libraries
mkdir -p .build/libraries
ldd .build/native/release/demo | grep so | | sed -e '/^[^\t]/ d' | sed -e 's/\t//' | sed -e 's/.*=..//' | sed -e 's/ (0.*)//' | xargs -i% cp % .build/libraries
2. exit out of the docker image and package up the files
Create init.js with the following contents:
const spawnSync = require('child_process').spawnSync;
exports.handler = (event, context, callback) => {
const command = 'libraries/ld-linux-x86-64.so.2';
const childObject = spawnSync(command, ["--library-path", "libraries", "./demo"], {input: JSON.stringify(event)})
// The executable's raw stdout is the Lambda output
var stdout = childObject.stdout.toString('utf8');
callback(null, stdout);
};
mkdir deploy
cp init.js deploy
cp .build/native/release/demo deploy
cp -r .build/libraries deploy
cd deploy
zip -r lambda.zip *
3. create a Lambda function via the AWS console
create a new Lambda function with custom blueprint and no trigger. Use the Node 6 runtime.
As code entry type: "Upload zip file"
Click on Test.
4. Bask in Riches and Adulation
5. ???
6. Profit
# Inspired by https://medium.com/@claushoefele/serverless-swift-2e8dce589b68
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment