Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .swiftformatignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Plugins/AWSLambdaInitializer/Template.swift
39 changes: 39 additions & 0 deletions Examples/_MyFirstFunction/create_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

# check if docker is installed
which docker > /dev/null
if [[ $? != 0 ]]; then
echo "Docker is not installed. Please install Docker and try again."
exit 1
fi

# check if user has an access key and secret access key
echo "This script creates and deploys a Lambda function on your AWS Account.

You must have an AWS account and know an AWS access key, secret access key, and an optional session token. These values are read from '~/.aws/credentials' or asked interactively.
"

read -p "Are you ready to create your first Lambda function in Swift? [y/n] " continue
if [[ continue != ^[Yy]$ ]]; then
echo "OK, try again later when you feel ready"
exit 1
fi

echo "⚡️ Create your Swift command line project"
swift package init --type executable --name MyLambda

echo "📦 Add the AWS Lambda Swift runtime to your project"
swift package add-dependency https://github.com/swift-server/swift-aws-lambda-runtime.git --branch main
swift package add-dependency https://github.com/swift-server/swift-aws-lambda-events.git --branch main
swift package add-target-dependency AWSLambdaRuntime MyLambda --package swift-aws-lambda-runtime
swift package add-target-dependency AWSLambdaEvents MyLambda --package swift-aws-lambda-events

echo "📝 Write the Swift code"
swift package lambda-init --allow-writing-to-package-directory

echo "📦 Compile and package the function for deployment"
swift package archive --allow-network-connections docker

echo "🚀 Deploy to AWS Lambda"


100 changes: 93 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@ let package = Package(
name: "swift-aws-lambda-runtime",
products: [
.library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]),

//
// The plugins
// 'lambda-init' creates a new Lambda function
// 'lambda-build' packages the Lambda function
// 'lambda-deploy' deploys the Lambda function
//
// Plugins requires Linux or at least macOS v15
//

// plugin to create a new Lambda function, based on a template
.plugin(name: "AWSLambdaInitializer", targets: ["AWSLambdaInitializer"]),

// plugin to package the lambda, creating an archive that can be uploaded to AWS
// requires Linux or at least macOS v15
.plugin(name: "AWSLambdaPackager", targets: ["AWSLambdaPackager"]),
.plugin(name: "AWSLambdaBuilder", targets: ["AWSLambdaBuilder"]),

// plugin to deploy a Lambda function
.plugin(name: "AWSLambdaDeployer", targets: ["AWSLambdaDeployer"]),
],
traits: [
"FoundationJSONSupport",
Expand Down Expand Up @@ -53,20 +68,84 @@ let package = Package(
swiftSettings: defaultSwiftSettings
),
.plugin(
name: "AWSLambdaPackager",
name: "AWSLambdaInitializer",
capability: .command(
intent: .custom(
verb: "lambda-init",
description:
"Create a new Lambda function in the current project directory."
),
permissions: [
.writeToPackageDirectory(reason: "Create a file with an HelloWorld Lambda function.")
]
),
dependencies: [
.target(name: "AWSLambdaPluginHelper")
]
),
// keep this one (with "archive") to not break workflows
// This will be deprecated at some point in the future
// .plugin(
// name: "AWSLambdaPackager",
// capability: .command(
// intent: .custom(
// verb: "archive",
// description:
// "Archive the Lambda binary and prepare it for uploading to AWS. Requires docker on macOS or non Amazonlinux 2 distributions."
// ),
// permissions: [
// .allowNetworkConnections(
// scope: .docker,
// reason: "This plugin uses Docker to create the AWS Lambda ZIP package."
// )
// ]
// ),
// path: "Plugins/AWSLambdaBuilder" // same sources as the new "lambda-build" plugin
// ),
.plugin(
name: "AWSLambdaBuilder",
capability: .command(
intent: .custom(
verb: "archive",
verb: "lambda-build",
description:
"Archive the Lambda binary and prepare it for uploading to AWS. Requires docker on macOS or non Amazonlinux 2 distributions."
"Compile and archive (zip) the Lambda binary and prepare it for uploading to AWS. Requires docker on macOS or non Amazonlinux 2 distributions."
),
permissions: [
.allowNetworkConnections(
scope: .docker,
reason: "This plugin uses Docker to create the AWS Lambda ZIP package."
reason: "This plugin uses Docker to compile code for Amazon Linux."
)
]
)
),
dependencies: [
.target(name: "AWSLambdaPluginHelper")
]
),
.plugin(
name: "AWSLambdaDeployer",
capability: .command(
intent: .custom(
verb: "lambda-deploy",
description:
"Deploy the Lambda function. You must have an AWS account and an access key and secret access key."
),
permissions: [
.allowNetworkConnections(
scope: .all(ports: [443]),
reason: "This plugin uses the AWS Lambda API to deploy the function."
)
]
),
dependencies: [
.target(name: "AWSLambdaPluginHelper")
]
),
.executableTarget(
name: "AWSLambdaPluginHelper",
dependencies: [
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOCore", package: "swift-nio"),
]
),
.testTarget(
name: "AWSLambdaRuntimeTests",
Expand All @@ -89,5 +168,12 @@ let package = Package(
],
swiftSettings: defaultSwiftSettings
),
.testTarget(
name: "AWSLambdaPluginHelperTests",
dependencies: [
.byName(name: "AWSLambdaPluginHelper")
]
),

]
)
93 changes: 86 additions & 7 deletions Package@swift-6.0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,24 @@ let package = Package(
name: "swift-aws-lambda-runtime",
products: [
.library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]),

//
// The plugins
// 'lambda-init' creates a new Lambda function
// 'lambda-build' packages the Lambda function
// 'lambda-deploy' deploys the Lambda function
//
// Plugins requires Linux or at least macOS v15
//

// plugin to create a new Lambda function, based on a template
.plugin(name: "AWSLambdaInitializer", targets: ["AWSLambdaInitializer"]),

// plugin to package the lambda, creating an archive that can be uploaded to AWS
// requires Linux or at least macOS v15
.plugin(name: "AWSLambdaPackager", targets: ["AWSLambdaPackager"]),
.plugin(name: "AWSLambdaBuilder", targets: ["AWSLambdaBuilder"]),

// plugin to deploy a Lambda function
.plugin(name: "AWSLambdaDeployer", targets: ["AWSLambdaDeployer"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.81.0"),
Expand All @@ -39,20 +54,84 @@ let package = Package(
swiftSettings: defaultSwiftSettings
),
.plugin(
name: "AWSLambdaPackager",
name: "AWSLambdaInitializer",
capability: .command(
intent: .custom(
verb: "archive",
verb: "lambda-init",
description:
"Archive the Lambda binary and prepare it for uploading to AWS. Requires docker on macOS or non Amazonlinux 2 distributions."
"Create a new Lambda function in the current project directory."
),
permissions: [
.writeToPackageDirectory(reason: "Create a file with an HelloWorld Lambda function.")
]
),
dependencies: [
.target(name: "AWSLambdaPluginHelper")
]
),
// keep this one (with "archive") to not break workflows
// This will be deprecated at some point in the future
// .plugin(
// name: "AWSLambdaPackager",
// capability: .command(
// intent: .custom(
// verb: "archive",
// description:
// "Archive the Lambda binary and prepare it for uploading to AWS. Requires docker on macOS or non Amazonlinux 2 distributions."
// ),
// permissions: [
// .allowNetworkConnections(
// scope: .docker,
// reason: "This plugin uses Docker to create the AWS Lambda ZIP package."
// )
// ]
// ),
// path: "Plugins/AWSLambdaBuilder" // same sources as the new "lambda-build" plugin
// ),
.plugin(
name: "AWSLambdaBuilder",
capability: .command(
intent: .custom(
verb: "lambda-build",
description:
"Compile and archive (zip) the Lambda binary and prepare it for uploading to AWS. Requires docker on macOS or non Amazonlinux 2 distributions."
),
permissions: [
.allowNetworkConnections(
scope: .docker,
reason: "This plugin uses Docker to create the AWS Lambda ZIP package."
reason: "This plugin uses Docker to compile code for Amazon Linux."
)
]
),
dependencies: [
.target(name: "AWSLambdaPluginHelper")
]
),
.plugin(
name: "AWSLambdaDeployer",
capability: .command(
intent: .custom(
verb: "lambda-deploy",
description:
"Deploy the Lambda function. You must have an AWS account and an access key and secret access key."
),
permissions: [
.allowNetworkConnections(
scope: .all(ports: [443]),
reason: "This plugin uses the AWS Lambda API to deploy the function."
)
]
)
),
dependencies: [
.target(name: "AWSLambdaPluginHelper")
]
),
.executableTarget(
name: "AWSLambdaPluginHelper",
dependencies: [
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOCore", package: "swift-nio"),
]
),
.testTarget(
name: "AWSLambdaRuntimeTests",
Expand Down
Loading
Loading