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
- Updated default target from `x86_64-unknown-linux-musl` -> `x86_64-unknown-linux-gnu`; technically this is not a _breaking_ change, but it will require the target to be added via `rustup`.
35
+
36
+
### Bug Fixes
37
+
38
+
- Ensure that the Lambda architecture is correctly set based on the `target` to cross-compile to.
39
+
- Do not pass `--target` when running `cargo check`, as this can result in errors in some cases.
Copy file name to clipboardExpand all lines: README.md
+71-13
Original file line number
Diff line number
Diff line change
@@ -46,10 +46,10 @@ The `RustFunction` construct creates a Lambda function with automatic bundling a
46
46
$ cargo install cross
47
47
```
48
48
49
-
3. Install the **x86_64-unknown-linux-musl** toolchain with Rustup by running:
49
+
3. Install the **x86_64-unknown-linux-gnu** toolchain with Rustup by running:
50
50
51
51
```shell
52
-
$ rustup target add x86_64-unknown-linux-musl
52
+
$ rustup target add x86_64-unknown-linux-gnu
53
53
```
54
54
55
55
Finally, ensure you have [Docker] installed and running, as it will be used by `cross` to compile Rust code for deployment.
@@ -108,7 +108,7 @@ When bundling the code, the `RustFunction` runs the following steps in order:
108
108
- First it runs `cargo check` to confirm that the Rust code can compile.
109
109
Note that this is an optional step, and [can be disabled](#settings) as mentioned below.
110
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.
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-gnu** target, as mentioned above.
112
112
113
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
114
@@ -202,19 +202,74 @@ You can find a more complete project structure in the [rust-workspaces/] CDK sam
A common use case is building Rust code with enabled [features], and compile-
208
+
time environment variables that can be used with the [`env!`] macro.
209
+
210
+
For example, we might want to run different logic in our code for _development_ and _production_ environments, or call a different API endpoint depending on which environment we are deploying code to.
211
+
212
+
For a sample CDK app that demonstrate such usage, check out the [rust-bins/] example.
213
+
214
+
### Enabling Features
215
+
216
+
You can achieve conditional compilation by [introducing features](https://stackoverflow.com/a/27634313/10237506) which can later be enabled in Rust code.
217
+
218
+
In the `Cargo.toml`, create a new `features` section:
219
+
220
+
```toml
221
+
[features]
222
+
my-feature = [] # feature has no explicit dependencies
223
+
```
224
+
225
+
In your code, add the line `#[cfg(feature="my-feature")]` before a function declaration, or before a statement to execute.
226
+
227
+
In your CDK code in the `lib/` folder, add the following line:
228
+
229
+
```ts
230
+
// Enable features at compile or build time.
231
+
Settings.FEATURES = ['my-feature'];
232
+
```
233
+
234
+
### Build Environment Variables
235
+
236
+
You can also introduce environment variables which are resolved at build or compile time. These values can be used in code via the [`env!`] macro in Rust.
237
+
238
+
In your code, add a call to the `env!()` macro:
239
+
240
+
```rust
241
+
// Retrieve an environment variable set at build (compile) time.
242
+
let build_value = env!("MY_BUILD_VAR");
243
+
```
244
+
245
+
In your CDK code in the `lib/` folder, add the following line:
246
+
247
+
```ts
248
+
// Enable environment variables at compile or build time.
| `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`. |
| `target` | Build target to cross-compile to. Defaults to the target for the **x86_64** architecture, `x86_64-unknown-linux-gnu`. |
264
+
| `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. |
265
+
|`buildDir`| Default Build directory, which defaults to a `.build` folder under the project's root directory. |
266
+
| `bin` | Executable name to pass to `--bin` |
267
+
| `package` | Workspace package name to pass to `--package` |
268
+
| `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`. |
269
+
||
270
+
|`features`| A list of features to activate when compiling Rust code. These must also be added to the `Cargo.toml` file. |
271
+
|`buildEnvironment`| Key-value pairs that are passed in at compile time, i.e. to `cargo build` or `cross build`. This differs from `environment`, which determines the environment variables which are set on the AWS Lambda functionitself.|
272
+
|`extraBuildArgs`| Additional arguments that are passed in at build time to both `cargo check` and `cross build`. For example, [`--all-features`]. |
218
273
219
274
## Settings
220
275
@@ -232,4 +287,7 @@ Below are some useful _global_ defaults which can be set for all Rust Lambda Fun
232
287
|`RUN_CARGO_CHECK`| Whether to run `cargo check` to validate Rust code before building it with `cross`. Defaults to _true_. |
233
288
|`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
289
|`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`. |
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. |
290
+
|`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. |
291
+
|`FEATURES`| A list of features to activate when compiling Rust code. These must also be added to the `Cargo.toml` file. |
292
+
|`BUILD_ENVIRONMENT`| Key-value pairs that are passed in at compile time, i.e. to `cargo build` or `cross build`. This differs from `environment`, which determines the environment variables which are set on the AWS Lambda functionitself.|
293
+
|`EXTRA_BUILD_ARGS`| Additional arguments that are passed in at build time to both `cargo check` and `cross build`. For example, [`--all-features`]. |
0 commit comments