You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`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.
4
4
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.
6
6
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.
8
8
9
9
## Overview
10
10
11
11
Versions:
12
12
13
13
* 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.
14
22
15
23
## Motivation
16
24
17
25
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.
18
26
19
27
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.
20
28
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.
22
30
23
31
### Current Limitations
24
32
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:
26
34
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.
28
37
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.
31
39
32
40
### New Plugins
33
41
@@ -84,11 +92,66 @@ OPTIONS:
84
92
85
93
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.
86
94
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
+
importAWSLambdaRuntime
101
+
102
+
// the data structure to represent the input parameter
103
+
structHelloRequest: Decodable {
104
+
let name: String
105
+
let age: Int
106
+
}
107
+
108
+
// the data structure to represent the output response
109
+
structHelloResponse: 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
+
tryawait runtime.run()
126
+
```
127
+
128
+
### URL Template
129
+
130
+
```swift
131
+
importAWSLambdaRuntime
132
+
importAWSLambdaEvents
133
+
134
+
// in this example we receive a FunctionURLRequest and we return a FunctionURLResponse
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.
90
153
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.
92
155
93
156
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.
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:
- 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
+
134
208
### Deploy (lambda-deploy)
135
209
136
210
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.
137
211
138
212
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.
139
213
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).
141
215
142
216
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.
143
217
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
+
144
220
The plugin can deploy to multiple regions. Users can specify the desired region as a command-line argument.
145
221
146
222
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
163
239
[--help] [--verbose]
164
240
165
241
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
--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.
174
254
"""
175
255
```
176
256
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
+
177
259
### Dependencies
178
260
179
261
One of the design objective of the Swift AWS Lambda Runtime is to minimize its dependencies on other libraries.
0 commit comments