Created
January 22, 2017 21:58
-
-
Save maximveksler/1f47e4a618cdf7e58169f6a2a04f32d6 to your computer and use it in GitHub Desktop.
CI deploy script for: AWS Lambda Swift echo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## credits ## | |
# https://medium.com/@claushoefele/serverless-swift-2e8dce589b68 ππ | |
# https://github.com/choefele/swift-lambda-app/blob/master/Shim/index.js π | |
export BINARY_NAME=Collection | |
# console calls it "Oregon", API calls it us-west-2... thanks amazon for consistency. Mapping here http://docs.aws.amazon.com/general/latest/gr/rande.html#apigateway_region | |
# Lambda and S3 bucket must be same region. | |
export BUCKET_NAME=YOURCOMPANY-lambda-deploy-oregon | |
export BUCKET_REGION=us-west-2 | |
docker run \ | |
--rm \ | |
--volume "$(pwd):/app" \ | |
--workdir /app \ | |
smithmicro/swift:3.0.2 \ | |
swift build -c release --build-path .build/native | |
mkdir -p .build/lambda/libraries | |
docker run \ | |
--rm \ | |
--volume "$(pwd):/app" \ | |
--workdir /app \ | |
smithmicro/swift:3.0.2 \ | |
/bin/bash -c "ldd .build/native/release/${BINARY_NAME} | grep so | sed -e '/^[^\t]/ d' | sed -e 's/\t//' | sed -e 's/.*=..//' | sed -e 's/ (0.*)//' | xargs -i% cp % .build/lambda/libraries" | |
pushd .build/lambda | |
cp ../native/release/${BINARY_NAME} . | |
cp ../../Shim/index.js . | |
zip -r ../lambda.zip . | |
popd | |
## Upload to Lambda | |
aws s3 cp .build/lambda.zip "s3://${BUCKET_NAME}/fbWebhook.zip" | |
aws lambda update-function-code --function-name fbWebhook --s3-bucket "${BUCKET_NAME}" --s3-key fbWebhook.zip --publish --region "$BUCKET_REGION" | |
# cleanup | |
rm .build/lambda.zip | |
rm -rf .build/lambda |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shim/index.js | |
'use strict'; | |
const spawnSync = require('child_process').spawnSync; | |
exports.handler = (event, context, callback) => { | |
const input = JSON.stringify(event, null, 2); | |
console.log('Received event:', input); | |
// Invoke the executable via the provided ld-linux.so β this will | |
// force the right GLIBC version and its dependencies | |
const command = 'libraries/ld-linux-x86-64.so.2'; | |
const options = { input: input }; | |
const childObject = spawnSync(command, ["--library-path", "libraries", "./Collection"], options) | |
if (childObject.error) { | |
callback(childObject.error, null); | |
} else { | |
try { | |
// The executable's raw stdout is the Lambda output | |
var stdout = childObject.stdout.toString('utf8'); | |
var stderr = childObject.stderr.toString('utf8'); | |
// Log process stdout and stderr | |
console.log('stdout: ', stdout); | |
console.error('stderr: ', stderr); | |
var response = JSON.parse(stdout); | |
callback(null, response); | |
} catch(e) { | |
e.message += ": " + stderr | |
callback(e, null); | |
} | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PureJsonSerializer | |
let inputData = FileHandle.standardInput.readDataToEndOfFile() | |
let input = String(data: inputData, encoding: .utf8)! | |
let response: Json = [ | |
"statusCode": 200, | |
"body": Json(input), | |
"headers": ["Content-Type": "application/json"] | |
] | |
FileHandle.standardOutput.write(response.debugDescription.data(using: .utf8)!) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PackageDescription | |
let package = Package( | |
name: "Collection", | |
dependencies: [ | |
.Package(url: "https://github.com/gfx/Swift-PureJsonSerializer", majorVersion: 1) | |
] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment