Using Swift in AWS Lambda

sudo mkdir /usr/local/swift
sudo chown ubuntu /usr/local/swift
mkdir ~/lambda
# Install dependencies
sudo apt-get update
sudo apt-get install clang libicu-dev
cd /usr/local/swift/wget https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-01-b/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gztar xfz swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz --strip 1export PATH="/usr/local/swift/usr/bin/:$PATH"
cd ~/lambdaecho ‘print(“Hello World!”)’ > hello.swift
swift hello.swift
Hello World!
swiftc hello.swift./hello
Hello World!
var exec = require('child_process').exec;exports.handler = function(event, context) {
child = exec("./hello", function(error) {
// Resolve with result of process
context.done(error, 'Process complete!');
});
// Log process stdout and stderr
child.stdout.on('data', console.log);
child.stderr.on('data', console.error);
};
sudo apt-get install zipzip swift.zip hello index.js
...
"errorMessage": "Command failed: ./hello: error while loading shared libraries: libswiftCore.so: cannot open shared object file: No such file or directory\n",
...
# Install libbsd as we'll need it
sudo apt-get install libbsd-dev
swiftc -L /usr/local/swift/usr/lib/swift_static/linux/ -lpthread -ldl -licui18n -licuuc -lbsd hello.swift
./helloString(String(String(String(String(String(String(String(String(Strin...String(String(String(String(String(String(String(String(StringBus error (core dumped)
swiftc hello.swiftmkdir libcp /usr/local/swift/usr/lib/swift/linux/*.so lib/
cp /usr/lib/x86_64-linux-gnu/libicudata.so.52 lib/
cp /usr/lib/x86_64-linux-gnu/libicui18n.so.52 lib/
cp /usr/lib/x86_64-linux-gnu/libicuuc.so.52 lib/
cp /usr/lib/x86_64-linux-gnu/libbsd.so lib/libbsd.so.0
var exec = require('child_process').exec;exports.handler = function(event, context) {
child = exec("./hello", {env: {'LD_LIBRARY_PATH': __dirname + '/lib'}}, function(error) {
// Resolve with result of process
context.done(error, 'Process complete!');
});
// Log process stdout and stderr
child.stdout.on('data', console.log);
child.stderr.on('data', console.error);
};
zip -r swift.zip hello index.js lib/
START RequestId: 1e4f8150-9f8d-11e5-9d12-eb0f0cb63aec Version: $LATEST
2015-12-10T22:26:55.319Z 1e4f8150-9f8d-11e5-9d12-eb0f0cb63aec Hello World!

END RequestId: 1e4f8150-9f8d-11e5-9d12-eb0f0cb63aec
REPORT RequestId: 1e4f8150-9f8d-11e5-9d12-eb0f0cb63aec Duration: 52.94 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 10 MB

--

--

Work on Java and PHP in a big data stack. Also dabble in Swift and Go.

Love podcasts or audiobooks? Learn on the go with our new app.

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
Justin Sanders

Justin Sanders

Work on Java and PHP in a big data stack. Also dabble in Swift and Go.