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
@@ -80,6 +80,15 @@ By default, the construct will use directory where `cdk` was invoked as director
80
80
81
81
If no `bin` or `package` argument is passed in, it will default to the package name as defined in the main `Cargo.toml`.
82
82
83
+
That is, the above usage should work for a project structure that looks like this:
84
+
85
+
```plaintext
86
+
.
87
+
├── Cargo.toml
88
+
└── src
89
+
└── main.rs
90
+
```
91
+
83
92
Alternatively, `directory` and `bin` can be specified:
84
93
85
94
```ts
@@ -92,6 +101,19 @@ new RustFunction(this, 'MyLambdaFunction', {
92
101
93
102
All other properties of `lambda.Function` are supported, see also the [AWS Lambda construct library](https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-lambda).
94
103
104
+
## How It Works
105
+
106
+
When bundling the code, the `RustFunction` runs the following steps in order:
107
+
108
+
- First it runs `cargo check` to confirm that the Rust code can compile.
109
+
Note that this is an optional step, and [can be disabled](#settings) as mentioned below.
110
+
111
+
- Next it calls `cross build`, and passes in the `--release` and `--target` flags, so it compiles for a Lambda environment - which defaults to the **x86_64-unknown-linux-musl** target, as mentioned above.
112
+
113
+
- Finally, it copies the release app binary from the `target/` folder to a file named `bootstrap`, which the Lambda custom runtime environment looks for. It adds this new file under the _build directory_, which defaults to a `.build/` folder under the directory where `cdk` was invoked.
114
+
115
+
- The directory path to the executable is then passed in to `lambda.Code.fromAsset`, which creates a _zip file_ from the release binary asset.
116
+
95
117
## Multiple Rust Lambdas
96
118
97
119
Assuming you have a CDK project with more than one Rust
@@ -111,7 +133,7 @@ Suppose your project layout looks like this:
111
133
└── lambda2.rs
112
134
```
113
135
114
-
Here's one way to deploy that:
136
+
Here's one way to deploy that via `cdk`:
115
137
116
138
```ts
117
139
new RustFunction(this, 'my-function-1', {
@@ -184,14 +206,15 @@ You can find a more complete project structure in the [rust-workspaces/] CDK sam
184
206
185
207
Below lists some commonly used properties you can pass in to the `RustFunction` construct.
| `target` | Build target to cross-compile to. Defaults to the target for Linux MUSL, `x86_64-unknown-linux-musl`. |
190
-
| `directory` | Entry point where the project's main `Cargo.toml` is located. By default, the construct will use directory where `cdk` was invoked as the directory where Cargo files are located. |
191
-
|`buildDir`| Default Build directory, which defaults to a `.build` folder under the project's root directory. |
192
-
| `bin` | Executable name to pass to `--bin` |
193
-
| `package` | Workspace package name to pass to `--package` |
194
-
| `setupLogging` | Determines whether we want to set up [library logging](https://rust-lang-nursery.github.io/rust-cookbook/development_tools/debugging/config_log.html) - i.e. set the `RUST_LOG` environment variable - for the lambda function. |
| `target` | Build target to cross-compile to. Defaults to the target for Linux MUSL, `x86_64-unknown-linux-musl`. |
212
+
| `directory` | Entry point where the project's main `Cargo.toml` is located. By default, the construct will use directory where `cdk` was invoked as the directory where Cargo files are located. |
213
+
|`buildDir`| Default Build directory, which defaults to a `.build` folder under the project's root directory. |
214
+
| `bin` | Executable name to pass to `--bin` |
215
+
| `package` | Workspace package name to pass to `--package` |
216
+
| `setupLogging` | Determines whether we want to set up [library logging](https://rust-lang-nursery.github.io/rust-cookbook/development_tools/debugging/config_log.html) - i.e. set the `RUST_LOG` environment variable - for the lambda function.<br><br>The format defaults to `warn,module_name=debug`, which means that the default log level is `warn`, and the executable or library's log level is `debug`. |
217
+
||
195
218
196
219
## Settings
197
220
@@ -206,4 +229,7 @@ Below are some useful _global_ defaults which can be set for all Rust Lambda Fun
|`BUILD_INDIVIDUALLY`| Whether to build each executable individually, either via `--bin` or `--package`. |
232
+
|`RUN_CARGO_CHECK`| Whether to run `cargo check` to validate Rust code before building it with `cross`. Defaults to _true_. |
233
+
|`DEFAULT_LOG_LEVEL`| Log Level for non-module libraries. Note that this value is only used when `RustFunctionProps.setupLogging` is enabled. Defaults to `warn`. |
234
+
|`MODULE_LOG_LEVEL`| Log Level for a module (i.e. the executable). Note that this value is only used when `RustFunctionProps.setupLogging` is enabled. Defaults to `debug`. |
209
235
|`workspace_dir`| Sets the root workspace directory. By default, the workspace directory is assumed to be the directory where `cdk` was invoked.<br><br>This directory should contain at the minimum a `Cargo.toml` file which defines the workspace members. |
You may then potentially run into some import errors when deploying the stack via `cdk`.
74
+
75
+
To fix that, run this command from both the project root folder `$root`, and compare the output when running it from within `$root/cdk-examples/my-app`:
76
+
77
+
```shell
78
+
npm list
79
+
```
80
+
81
+
To resolve the import issues, you'll need to ensure that certain package versions are the same between the two directories.
82
+
83
+
For example, here are the important ones you'd need to verify:
0 commit comments