Skip to content

Commit 3791f25

Browse files
committed
add community feedback
1 parent 8aaf872 commit 3791f25

File tree

1 file changed

+100
-18
lines changed

1 file changed

+100
-18
lines changed

Plugins/Documentation.docc/Proposals/0001-v2-plugins.md

Lines changed: 100 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
# v2 Plugin Proposal for swift-aws-lambda-runtime
22

3-
`swift-aws-lambda-runtime` is an important library for the Swift on Server ecosystem. The initial versions of the library focused on the API, enabling developers to write Lambda functions in the Swift programming language. The library provided developers with basic support for building and packaging their functions.
3+
`swift-aws-lambda-runtime` is a library for the Swift on Server ecosystem. The initial version of the library focused on the API, enabling developers to write Lambda functions in the Swift programming language. The library provided developers with basic support for building and packaging their functions.
44

5-
We believe it is time to consider the end-to-end developer experience, from project scaffolding to deployment, taking into account the needs of Swift developers new to AWS and Lambda.
5+
We believe it is time to consider the end-to-end developer experience, from project scaffolding to deployment, taking into account the needs of Swift developers that are new to AWS and Lambda.
66

7-
This document describes the proposal for the v2 plugins for `swift-aws-lambda-runtime`. The plugins will focus on project scaffolding, building, archiving, and deployment of Lambda functions.
7+
This document describes a proposal for the v2 plugins for `swift-aws-lambda-runtime`. The plugins will focus on project scaffolding, building, archiving, and deployment of Lambda functions.
88

99
## Overview
1010

1111
Versions:
1212

1313
* v1 (2024-12-25): Initial version
14+
* v2 (2025-03-13):
15+
- Include [comments from the community](https://forums.swift.org/t/lambda-plugins-for-v2/76859).
16+
- [init] Add the templates for `main.swift`
17+
- [build] Add the section **Cross-compiling options**
18+
- [deploy] Add details about locating AWS Credentials.
19+
- [deploy] Add `--input-path` parameter.
20+
- [deploy] Add details how the function name is computed.
21+
- [deploy] Add `--architecture` option and details how the default is computed.
1422

1523
## Motivation
1624

1725
The current version of `swift-aws-lambda-runtime` provides a solid foundation for Swift developers to write Lambda functions. However, the developer experience can be improved. For example, the current version does not provide any support for project scaffolding or deployment of Lambda functions.
1826

1927
This creates a high barrier to entry for Swift developers new to AWS and Lambda, as well as for AWS professionals learning Swift. We propose to lower this barrier by providing a set of plugins that will assist developers in creating, building, packaging, and deploying Lambda functions.
2028

21-
As a source of inspiration, we looked to the Rust community, which created Cargo-Lambda ([https://www.cargo-lambda.info/guide/what-is-cargo-lambda.html](https://www.cargo-lambda.info/guide/what-is-cargo-lambda.html)). Cargo-Lambda helps developers deploy Rust Lambda functions. We aim to provide a similar experience for Swift developers.
29+
As a source of inspiration, we looked at the Rust community, which created Cargo-Lambda ([https://www.cargo-lambda.info/guide/what-is-cargo-lambda.html](https://www.cargo-lambda.info/guide/what-is-cargo-lambda.html)). Cargo-Lambda helps developers deploy Rust Lambda functions. We aim to provide a similar experience for Swift developers.
2230

2331
### Current Limitations
2432

25-
The current version of `swift-aws-lambda-runtime` does not provide any support for project **scaffolding** or **deployment** of Lambda functions. This makes it difficult for Swift developers new to AWS and Lambda, or AWS Professionals new to Swift, to get started.
33+
The current version of the `archive` plugin support the following tasks:
2634

27-
The main limitations of the current version of the `archive` plugin are as follows:
35+
* The cross-compilation using Docker.
36+
* The archiving of the Lambda function and it's resources as a ZIP file.
2837

29-
* It only handles cross-compilation using Docker.
30-
* It only supports archiving of the Lambda function as a ZIP file.
38+
The current version of `swift-aws-lambda-runtime` does not provide support for project **scaffolding** or **deployment** of Lambda functions. This makes it difficult for Swift developers new to AWS and Lambda, or AWS Professionals new to Swift, to get started.
3139

3240
### New Plugins
3341

@@ -84,11 +92,66 @@ OPTIONS:
8492

8593
The initial implementation will use hardcoded templates. In a future release, we might consider fetching the templates from a GitHub repository and allowing developers to create custom templates.
8694

95+
The default templates are currently implemented in the [sebsto/new-plugins branch of this repo](https://github.com/sebsto/swift-aws-lambda-runtime/blob/sebsto/new-plugins/Sources/AWSLambdaPluginHelper/lambda-init/Template.swift).
96+
97+
### Default template
98+
99+
```swift
100+
import AWSLambdaRuntime
101+
102+
// the data structure to represent the input parameter
103+
struct HelloRequest: Decodable {
104+
let name: String
105+
let age: Int
106+
}
107+
108+
// the data structure to represent the output response
109+
struct HelloResponse: Encodable {
110+
let greetings: String
111+
}
112+
113+
// in this example we receive a HelloRequest JSON and we return a HelloResponse JSON
114+
115+
// the Lambda runtime
116+
let runtime = LambdaRuntime {
117+
(event: HelloRequest, context: LambdaContext) in
118+
119+
HelloResponse(
120+
greetings: "Hello \(event.name). You look \(event.age > 30 ? "younger" : "older") than your age."
121+
)
122+
}
123+
124+
// start the loop
125+
try await runtime.run()
126+
```
127+
128+
### URL Template
129+
130+
```swift
131+
import AWSLambdaRuntime
132+
import AWSLambdaEvents
133+
134+
// in this example we receive a FunctionURLRequest and we return a FunctionURLResponse
135+
// https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads
136+
137+
let runtime = LambdaRuntime {
138+
(event: FunctionURLRequest, context: LambdaContext) -> FunctionURLResponse in
139+
140+
guard let name = event.queryStringParameters?["name"] else {
141+
return FunctionURLResponse(statusCode: .badRequest)
142+
}
143+
144+
return FunctionURLResponse(statusCode: .ok, body: #"{ "message" : "Hello \#\#(name)" } "#)
145+
}
146+
147+
try await runtime.run()
148+
```
149+
87150
### Build and Package (lambda-build)
88151

89152
The `lambda-build` plugin will assist developers in building and packaging their Lambda function. It will allow for multiple cross-compilation options. We will retain the current Docker-based cross-compilation but also provide a way to cross-compile without Docker, such as using the Swift Static Linux SDK (with musl) or a custom Swift SDK for Amazon Linux.
90153

91-
We also propose to automatically strip the binary of debug symbols to reduce the size of the ZIP file. Our tests showed that this can reduce the size by up to 50%. An option to disable stripping will be provided.
154+
We also propose to automatically strip the binary of debug symbols (`-Xlinker -s`) to reduce the size of the ZIP file. Our tests showed that this can reduce the size by up to 50%. An option to disable stripping will be provided.
92155

93156
The `lambda-build` plugin is similar to the existing `archive` plugin. We propose to keep the same interface to facilitate migration of existing projects and CI chains. If technically feasible, we will also consider keeping the `archive` plugin as an alias to the `lambda-build` plugin.
94157

@@ -131,16 +194,29 @@ OPTIONS:
131194
(default: docker) Accepted values are: docker, swift-static-sdk, custom-sdk
132195
```
133196

197+
#### Cross compiling options
198+
199+
We propose to release an initial version based on the current `archive` plugin implementation, which uses docker. But for the future, we would like to explore the possibility to cross compile with a custom Swift SDK for Amazon Linux. Our [initial tests](https://github.com/swiftlang/swift-sdk-generator/issues/138#issuecomment-2719540021) demonstrated it is possible to build such an SDK using the Swift SDK Generator project.
200+
201+
For an ideal developer experience, we would imagine the following sequence:
202+
203+
- developer runs `swift package build --cross-compile custom-sdk`
204+
- the plugin checks if the custom sdk is installed on the machine (`swift sdk list`) [questions : is it possible to call `swift` from a package ? Should we check the file systems instead ? Should this work on multiple OSes, such as macOS and Linux? ]
205+
- if not installed or outdated, the plugin downloads a custom SDK from a safe source and installs it [questions : who should maintain such SDK binaries? Where to host them? We must have a kind of signature to ensure the SDK has not been modified. How to manage Swift version and align with the local toolchain?]
206+
- the plugin build the archive using the custom sdk
207+
134208
### Deploy (lambda-deploy)
135209

136210
The `lambda-deploy` plugin will assist developers in deploying their Lambda function to AWS. It will handle the deployment process, including creating the IAM role, the Lambda function itself, and optionally configuring a Lambda function URL.
137211

138212
The plugin will not depends on nay third-party library. It will interact directly with the AWS REST API, without using the AWS SDK fro Swift or Soto.
139213

140-
Users will need to provide AWS access key and secret access key credentials. The plugin will attempt to locate these credentials in standard locations, such as environment variables or the `~/.aws/credentials` file.
214+
Users will need to provide AWS access key and secret access key credentials. The plugin will attempt to locate these credentials in standard locations. It will first check for the `~/.aws/credentials` file, then the environment variables `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and (optional) `AWS_SESSION_TOKEN`. Finally, it will check the [meta data service v2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html) in case the plugin runs from a virtual machine (Amazon EC2) or a container (Amazon ECS or AMazon EKS).
141215

142216
The plugin supports deployment through either the REST and Base64 payload or by uploading the code to a temporary S3 bucket. Refer to [the `Function Code` section](https://docs.aws.amazon.com/lambda/latest/api/API_FunctionCode.html) of the [CreateFunction](https://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html) API for more details.
143217

218+
The plugin will use teh function name as defined in the `executableTarget` in `Package.swift`. This approach is similar to how the `archive` plugin works today.
219+
144220
The plugin can deploy to multiple regions. Users can specify the desired region as a command-line argument.
145221

146222
In addition to deploying the Lambda function, the plugin can also create an IAM role for it. Users can specify the IAM role name as a command-line argument. If no role name is provided, the plugin will create a new IAM role with the necessary permissions for the Lambda function.
@@ -163,17 +239,23 @@ USAGE: swift package lambda-deploy
163239
[--help] [--verbose]
164240
165241
OPTIONS:
166-
--region The AWS region to deploy the Lambda function to.
167-
(default is us-east-1)
168-
--iam-role The name of the IAM role to use for the Lambda function.
169-
when none is provided, a new role will be created.
170-
--with-url Add an URL to access the Lambda function
171-
--delete Delete the Lambda function and its associated IAM role
172-
--verbose Produce verbose output for debugging.
173-
--help Show help information.
242+
--region The AWS region to deploy the Lambda function to.
243+
(default is us-east-1)
244+
--iam-role The name of the IAM role to use for the Lambda function.
245+
when none is provided, a new role will be created.
246+
--input-directory <path> The path of the binary package (zip file) to deploy
247+
(default: .build/plugins/AWSLambdaPackager/outputs/...)
248+
--architecture x64 | arm64 The target architecture of the Lambda function
249+
(default: the architecture of the machine where the plugin runs)
250+
--with-url Add an URL to access the Lambda function
251+
--delete Delete the Lambda function and its associated IAM role
252+
--verbose Produce verbose output for debugging.
253+
--help Show help information.
174254
"""
175255
```
176256

257+
In a future version, we might consider adding an `--export` option that would easily migrate the current deployment to an infrastructure as code (IaC) tool, such as AWS SAM, AWS CDK, or Swift Cloud.
258+
177259
### Dependencies
178260

179261
One of the design objective of the Swift AWS Lambda Runtime is to minimize its dependencies on other libraries.

0 commit comments

Comments
 (0)