Skip to content

Instantly share code, notes, and snippets.

@viteinfinite
Last active December 2, 2020 09:41
Show Gist options
  • Save viteinfinite/e2d9497186d28984e0a254e95cfe3134 to your computer and use it in GitHub Desktop.
Save viteinfinite/e2d9497186d28984e0a254e95cfe3134 to your computer and use it in GitHub Desktop.
Swift Lambda build and package scripts
# Install the jq tool (https://stedolan.github.io/jq/)
# It'll be used to parse the response of the aws cli commands
JQ_PATH=./scripts/jq
if [ ! -f "$JQ_PATH" ]; then
echo "Installing jq"
curl -L https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64 --output $JQ_PATH
chmod +x $JQ_PATH
fi
# Create an AWS IAM Role
# IAM Roles are used to define policies for operating with AWS resources.
# AWS Lambdas must be associated with a role allowing them to interact
# with other AWS services, for instance CloudWatch (for writing logs).
# This action is performed in two steps:
# 1. Create a role and save its identifier (aka Arn) to $role_arn for later use
# 2. Attach a given policy to the role
echo "Creating IAM role"
role_arn=( $(aws iam create-role \
--role-name swift-lambda-execution \
--assume-role-policy-document file://scripts/trust-policy.json \
| $JQ_PATH -r .Role.Arn) \
)
echo "Created new IAM role"
# The AWSLambdaBasicExecutionRole grants permission to upload logs to CloudWatch.
# https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html
echo "Attaching a basic Lambda policy"
aws iam attach-role-policy \
--role-name swift-lambda-execution \
--policy-arn "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" \
>/dev/null
sleep 5
# Create the lambda function and upload its code in zipped form
# The `runtime` value is `provided`, to specify we'll be using a custom runtime.
echo "Creating and uploading the Lambda function"
aws lambda create-function \
--function-name MyLambda \
--runtime provided \
--role $role_arn \
--handler "handler" \
--zip-file fileb://.build/lambda/MyLambda/lambda.zip
#!/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
executable=$1
echo "-------------------------------------------------------------------------"
echo "building \"$executable\" lambda"
echo "-------------------------------------------------------------------------"
docker run --rm -v "$(pwd)":/workspace -w /workspace builder bash -cl "swift build --product $executable -c release -Xswiftc -g"
echo "done"
echo "-------------------------------------------------------------------------"
echo "packaging \"$executable\" lambda"
echo "-------------------------------------------------------------------------"
docker run --rm -v "$(pwd)":/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable"
#!/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
executable=$1
target=.build/lambda/$executable
rm -rf "$target"
mkdir -p "$target"
cp ".build/release/$executable" "$target/"
cp -Pv /usr/lib/swift/linux/lib*so* "$target"
cd "$target"
ln -s "$executable" "bootstrap"
zip --symlinks lambda.zip *
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment