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
@@ -9,9 +9,32 @@ This package makes it easy to run AWS Lambda Functions written in Rust. This wor
9
9
-[](https://docs.rs/lambda-extension)**`lambda-extension`** is a library that makes it easy to write Lambda Runtime Extensions in Rust.
10
10
-[](https://docs.rs/lambda_runtime_api_client)**`lambda-runtime-api-client`** is a shared library between the lambda runtime and lambda extension libraries that includes a common API client to talk with the AWS Lambda Runtime API.
11
11
12
-
## Example function
12
+
## Getting started
13
13
14
-
The code below creates a simple function that receives an event with a `firstName` field and returns a message to the caller. Notice: this crate is tested against latest stable Rust.
14
+
The easiest way to start writing Lambda functions with Rust is by using Cargo-Lambda. This Cargo subcommand provides several commands to help you in your journey with Rust on AWS Lambda.
15
+
16
+
You can install `cargo-lambda` with a package manager like Homebrew:
17
+
18
+
```bash
19
+
brew tap cargo-lambda/cargo-lambda
20
+
brew install cargo-lambda
21
+
```
22
+
23
+
Or by compiling it from source:
24
+
25
+
```bash
26
+
cargo install cargo-lambda
27
+
```
28
+
29
+
See other installation options in [the cargo-lambda documentation](https://github.com/cargo-lambda/cargo-lambda#installation).
30
+
31
+
### Your first function
32
+
33
+
To create your first function, type `cargo lambda new` in your terminal, and follow the prompts. This command will generate a Rust package with the initial source code for your function.
34
+
35
+
### Example function
36
+
37
+
If you'd like to manually create your first function, the code below shows you a simple function that receives an event with a `firstName` field and returns a message to the caller.
15
38
16
39
```rust,no_run
17
40
use lambda_runtime::{service_fn, LambdaEvent, Error};
There are currently multiple ways of building this package: manually with the AWS CLI, with [AWS SAM](https://github.com/aws/aws-sam-cli), and with the [Serverless framework](https://serverless.com/framework/).
38
-
39
-
### 1. Cross-compiling your Lambda functions
60
+
If you already have `cargo-lambda` installed in your machine, run the next command to build your function:
40
61
41
-
At the time of writing, the ability to cross compile to a different architecture is limited. For example, you might experience issues if you are compiling from a MacOS machine, or from a Linux machine with a different architecture (e.g. compiling to Arm64 from x86_64). The most robust way we've found is using [`cargo-zigbuild`](https://github.com/messense/cargo-zigbuild) to compile for the target architecture.
42
-
43
-
#### 1.1. Setup the cross-compilation environment
44
-
45
-
_You can skip this step if you are compiling for the same target as your host architecture (e.g. x86_64 Linux to x86_64 Linux), unless you're building for an Amazon Linux 1 runtime._
46
-
47
-
Run this script once to add your desired target:
48
-
49
-
```bash
50
-
# For Arm64 Lambda functions
51
-
rustup target add aarch64-unknown-linux-gnu
52
-
# For x86_64 Lambda functions
53
-
rustup target add x86_64-unknown-linux-gnu
62
+
```
63
+
cargo lambda build --release
54
64
```
55
65
56
-
Once this is done, install `cargo-lambda`:
66
+
There are other ways of building your function: manually with the AWS CLI, with [AWS SAM](https://github.com/aws/aws-sam-cli), and with the [Serverless framework](https://serverless.com/framework/).
57
67
58
-
```bash
59
-
cargo install cargo-lambda
60
-
```
61
68
62
-
This Cargo subcommand will give you the option to install [Zig](https://ziglang.org/) to use as the linker. You can also install [Zig](https://ziglang.org/) using the instructions in their [installation guide](https://ziglang.org/learn/getting-started/#installing-zig).
69
+
### 1. Cross-compiling your Lambda functions
70
+
71
+
By default, `cargo-lambda` builds your functions to run on x86_64 architectures. If you'd like to use a different architecture, use the options described below.
63
72
64
73
#### 1.2. Build your Lambda functions
65
74
@@ -68,13 +77,12 @@ __Amazon Linux 2__
68
77
We recommend you to use Amazon Linux 2 runtimes (such as `provided.al2`) as much as possible for building Lambda functions in Rust. To build your Lambda functions for Amazon Linux 2 runtimes, run:
69
78
70
79
```bash
71
-
# Note: replace "aarch64" with "x86_64" if you are building for x86_64
Amazon Linux 1 uses glibc version 2.17, while Rust binaries need glibc version 2.18 or later by default. However, with `cargo-zigbuild`, you can specify a different version of glibc.
85
+
Amazon Linux 1 uses glibc version 2.17, while Rust binaries need glibc version 2.18 or later by default. However, with `cargo-lambda`, you can specify a different version of glibc.
78
86
79
87
If you are building for Amazon Linux 1, or you want to support both Amazon Linux 2 and 1, run:
80
88
@@ -89,15 +97,50 @@ For [a custom runtime](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-cus
89
97
90
98
You can find the `bootstrap` binary for your function under the `target/lambda` directory.
91
99
92
-
#### 2.1. Deploying with the AWS CLI
100
+
#### 2.2. Deploying with cargo-lambda
101
+
102
+
You can use `cargo-lambda` for simple function deployments. Once you've built your code with one of the options described earlier, use the `deploy` subcommand to upload your function to AWS:
> See other deployment options in [the cargo-lambda documentation](https://github.com/cargo-lambda/cargo-lambda#deploy).
93
124
94
-
First, you will need to create a ZIP archive of your Lambda function. Cargo-lambda can do that for you automatically when it builds your binary if you add the `output-format` flag. For example, if you are using the `basic` example and aarch64-unknown-linux-gnu as your target, you can run:
125
+
You can test the function with `cargo-lambda`'s invoke subcommand:
95
126
96
127
```bash
97
-
cargo lambda build --release --target aarch64-unknown-linux-gnu --output-format zip
128
+
cargo lambda invoke --remote \
129
+
--data-ascii '{"command": "hi"}' \
130
+
--output-format json \
131
+
my-first-lambda-function
98
132
```
99
133
100
-
Now that we have a deployment package (`target/lambda/basic/bootstrap.zip`), we can use the [AWS CLI](https://aws.amazon.com/cli/) to create a new Lambda function. Make sure to replace the execution role with an existing role in your account!
134
+
#### 2.2. Deploying with the AWS CLI
135
+
136
+
You can also use the AWS CLI to deploy your Rust functions. First, you will need to create a ZIP archive of your function. Cargo-lambda can do that for you automatically when it builds your binary if you add the `output-format` flag:
137
+
138
+
```bash
139
+
cargo lambda build --release --arm64 --output-format zip
140
+
```
141
+
142
+
You can find the resulting zip file in `target/lambda/YOUR_PACKAGE/bootstrap.zip`. Use that file path to deploy your function with the [AWS CLI](https://aws.amazon.com/cli/):
**Note:**`--cli-binary-format raw-in-base64-out` is a required
124
170
argument when using the AWS CLI version 2. [More Information](https://docs.aws.amazon.com/cli/latest/userguide/cliv2-migration.html#cliv2-migration-binaryparam)
125
171
126
-
#### 2.2. AWS Serverless Application Model (SAM)
172
+
#### 2.3. AWS Serverless Application Model (SAM)
127
173
128
174
You can use Lambda functions built in Rust with the [AWS Serverless Application Model (SAM)](https://aws.amazon.com/serverless/sam/). To do so, you will need to install the [AWS SAM CLI](https://github.com/aws/aws-sam-cli), which will help you package and deploy your Lambda functions in your AWS account.
129
175
@@ -167,7 +213,7 @@ $ aws lambda invoke
167
213
$ cat output.json # Prints: {"msg": "Command Say Hi! executed."}
168
214
```
169
215
170
-
#### 2.3. Serverless Framework
216
+
#### 2.4. Serverless Framework
171
217
172
218
Alternatively, you can build a Rust-based Lambda function declaratively using the [Serverless framework Rust plugin](https://github.com/softprops/serverless-rust).
173
219
@@ -201,7 +247,7 @@ Invoke it using serverless framework or a configured AWS integrated trigger sour
Alternatively, you can build a Rust-based Lambda function in a [docker mirror of the AWS Lambda provided runtime with the Rust toolchain preinstalled](https://github.com/rust-serverless/lambda-rust).
207
253
@@ -237,6 +283,40 @@ $ unzip -o \
237
283
238
284
## Local development and testing
239
285
286
+
### Testing your code with unit and integration tests
287
+
288
+
AWS Lambda events are plain structures deserialized from JSON objects.
289
+
If your function handler uses the standard runtime, you can use `serde` to deserialize
290
+
your text fixtures into the structures, and call your handler directly:
291
+
292
+
```rust,no_run
293
+
#[test]
294
+
fn test_my_lambda_handler() {
295
+
let input = include_str!("apigw_proxy_request.json");
296
+
let input = serde_json::from_str("{\"command\": \"Say Hi!\"}").expect("failed to parse event");
297
+
let context = lambda_runtime::types::Context::default();
298
+
299
+
let event = lambda_runtime::types::LambdaEvent::new(input, context);
300
+
301
+
my_lambda_handler(event).expect("failed to handle event");
302
+
}
303
+
```
304
+
305
+
If you're using `lambda_http` to receive HTTP events, you can also create `http_lambda::Request`
306
+
structures from plain text fixtures:
307
+
308
+
```rust,no_run
309
+
#[test]
310
+
fn test_my_lambda_handler() {
311
+
let input = include_str!("apigw_proxy_request.json");
312
+
313
+
let request = lambda_http::request::from_str(input)
314
+
.expect("failed to create request");
315
+
316
+
let response = my_lambda_handler(request).expect("failed to handle request");
317
+
}
318
+
```
319
+
240
320
### Cargo Lambda
241
321
242
322
[Cargo Lambda](https://crates.io/crates/cargo-lambda) provides a local server that emulates the AWS Lambda control plane. This server works on Windows, Linux, and MacOS. In the root of your Lambda project, run the subcommand `cargo lambda start` to start the server. Your function will be compiled when the server receives the first request to process. Use the subcommand `cargo lambda invoke` to send requests to your function. The `start` subcommand will watch your function's code for changes, and it will compile it every time you save the source after making changes.
@@ -261,7 +341,7 @@ This project does not currently include Lambda event struct definitions. Instead
261
341
262
342
To serialize and deserialize events and responses, we suggest using the use the [`serde`](https://github.com/serde-rs/serde) library. To receive custom events, annotate your structure with Serde's macros:
0 commit comments