Skip to content

Commit 3152f14

Browse files
authored
v1.0.0: use cargo zigbuild (#4)
* Checkin changes * Checkin changes * Change default target to `aarch64-unknown-linux-gnu` * Build with `cargo zigbuild` instead of `cross` * Remove logic to run `cargo check`, as it appears `zigbuild` already does this 👍 * Remove `checkCode` function as its not needed now * Rename `cross` to `cargo-zigbuild` * Update docs * Update docs * Update docs * Fix letter * Fix letter * Update docs * Update docs * Update docs * Update docs * Update docs * Update docs * Update docs * Update stuff * Update CHANGELOG.md * Update docs to mention how to install with `cross` * Update links * Bump version
1 parent 7c29e76 commit 3152f14

File tree

13 files changed

+137
-177
lines changed

13 files changed

+137
-177
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ Possible header types:
1111

1212
## [Unreleased]
1313

14+
## v1.0.0 (2022-03-21)
15+
16+
## Breaking Changes
17+
18+
- Switch to use [`cargo-zigbuild`] -- instead of `cross` -- for building Rust code.
19+
- Switch the default build architecture from `x86_64` to `arm64`; this package now uses **aarch64-unknown-linux-gnu** as the default build target for AWS Lambda functions, mainly as I've found this architecture to be slightly more performant in general use cases.
20+
- Do not run `cargo check`, as it appears _cargo-zigbuild_ automatically runs this check.
21+
- Update `cdk-examples/` with an example of how to conditionally run a code block (more than one statement) when a feature is enabled.
22+
23+
[`cargo-zigbuild`]: https://github.com/messense/cargo-zigbuild
24+
1425
## v0.4.0 (2022-03-08)
1526

1627
### Features

README.md

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,55 @@
2121

2222
This library provides a construct for a Rust Lambda function.
2323

24-
It uses [Docker] and [`cross`] under the hood, and follows best practices as outlined
24+
It uses [`cargo-zigbuild`] under the hood, and follows best practices as outlined
2525
in the [official AWS documentation].
2626

27-
[docker]: https://www.docker.com/get-started
28-
[`cross`]: https://github.com/rust-embedded/cross
29-
[official aws documentation]: https://docs.aws.amazon.com/sdk-for-rust/latest/dg/lambda.html
27+
[`cargo-zigbuild`]: https://github.com/messense/cargo-zigbuild
28+
[official aws documentation]: https://github.com/awslabs/aws-lambda-rust-runtime#building-and-deploying-your-lambda-functions
3029

3130
## Rust Function
3231

3332
The `RustFunction` construct creates a Lambda function with automatic bundling and compilation of Rust code.
3433

3534
## Getting Started
3635

37-
1. Install the [npm](https://nodejs.org/) package:
36+
1. Install the [npm] package:
3837

3938
```shell
4039
$ npm i rust.aws-cdk-lambda
4140
```
4241

43-
2) Use [`cargo`] to install _rust-embedded/cross_:
42+
2. Install the **aarch64-unknown-linux-gnu** toolchain with Rustup by running:
4443

4544
```shell
46-
$ cargo install cross
45+
$ rustup target add aarch64-unknown-linux-gnu
4746
```
4847

49-
3. Install the **x86_64-unknown-linux-gnu** toolchain with Rustup by running:
48+
3) Install [Zig], using the instructions in their [installation guide] or via one of the options below.
49+
50+
1. Using [pip] (Python 3 required):
51+
52+
```shell
53+
$ pip install ziglang
54+
```
55+
56+
2. Using [npm]:
57+
58+
```shell
59+
$ npm install -g @ziglang/cli
60+
```
61+
62+
4) Use [`cargo`] to install _messense/cargo-zigbuild_:
5063

5164
```shell
52-
$ rustup target add x86_64-unknown-linux-gnu
65+
$ cargo install cargo-zigbuild
5366
```
5467

55-
Finally, ensure you have [Docker] installed and running, as it will be used by `cross` to compile Rust code for deployment.
56-
68+
[npm]: https://nodejs.org/
69+
[pip]: https://pip.pypa.io/en/stable/
5770
[`cargo`]: https://www.rust-lang.org/
71+
[zig]: https://ziglang.org/
72+
[installation guide]: https://ziglang.org/learn/getting-started/#installing-zig
5873

5974
## Examples
6075

@@ -105,15 +120,26 @@ All other properties of `lambda.Function` are supported, see also the [AWS Lambd
105120
106121
When bundling the code, the `RustFunction` runs the following steps in order:
107122
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.
123+
- First it runs `cargo zigbuild`, and passes in the `--release` and `--target` flags, so it compiles for a Lambda environment - which defaults to the **aarch64-unknown-linux-gnu** target, as mentioned above. Note that _zigbuild_ does initially confirm that the Rust code can compile.
110124
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-
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.
125+
- Next, 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.
114126
115127
- The directory path to the executable is then passed in to `lambda.Code.fromAsset`, which creates a _zip file_ from the release binary asset.
116128
129+
## Use `cross` for Deployment
130+
131+
If you instead prefer to use [Docker] and [`cross`] for deployment, as outlined
132+
in the [official AWS docs], you can install and use the [latest `v0.x`] release instead:
133+
134+
```shell
135+
$ npm i rust.aws-cdk-lambda@0.4.0
136+
```
137+
138+
[docker]: https://www.docker.com/get-started
139+
[`cross`]: https://github.com/rust-embedded/cross
140+
[official aws docs]: https://docs.aws.amazon.com/sdk-for-rust/latest/dg/lambda.html
141+
[latest `v0.x`]: https://github.com/rnag/rust.aws-cdk-lambda/releases/tag/0.4.0
142+
117143
## Multiple Rust Lambdas
118144
119145
Assuming you have a CDK project with more than one Rust
@@ -222,7 +248,7 @@ In the `Cargo.toml`, create a new `features` section:
222248
my-feature = [] # feature has no explicit dependencies
223249
```
224250
225-
In your code, add the line `#[cfg(feature="my-feature")]` before a function declaration, or before a statement to execute.
251+
In your code, add the line `#[cfg(feature = "my-feature")]` before a function declaration, or before a statement to execute.
226252
227253
In your CDK code in the `lib/` folder, add the following line:
228254
@@ -239,7 +265,7 @@ In your code, add a call to the `env!()` macro:
239265
240266
```rust
241267
// Retrieve an environment variable set at build (compile) time.
242-
let build_value = env!("MY_BUILD_VAR");
268+
const BUILD_VALUE: &str = env!("MY_BUILD_VAR");
243269
```
244270
245271
In your CDK code in the `lib/` folder, add the following line:
@@ -260,16 +286,16 @@ Below lists some commonly used properties you can pass in to the `RustFunction`
260286
261287
| Name | Description |
262288
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
263-
| `target` | Build target to cross-compile to. Defaults to the target for the **x86_64** architecture, `x86_64-unknown-linux-gnu`. |
289+
| `target` | Build target to cross-compile to. Defaults to the target for the **arm64** architecture, `aarch64-unknown-linux-gnu`. |
264290
| `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. |
265291
| `buildDir` | Default Build directory, which defaults to a `.build` folder under the project's root directory. |
266292
| `bin` | Executable name to pass to `--bin` |
267293
| `package` | Workspace package name to pass to `--package` |
268294
| `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`. |
269295
| |
270296
| `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 function itself. |
272-
| `extraBuildArgs` | Additional arguments that are passed in at build time to both `cargo check` and `cross build`. For example, [`--all-features`]. |
297+
| `buildEnvironment` | Key-value pairs that are passed in at compile time, i.e. to `cargo build` or `cargo zigbuild`. This differs from `environment`, which determines the environment variables which are set on the AWS Lambda function itself. |
298+
| `extraBuildArgs` | Additional arguments that are passed in at build time to `cargo zigbuild`. For example, [`--all-features`]. |
273299
274300
## Settings
275301
@@ -284,10 +310,9 @@ Below are some useful _global_ defaults which can be set for all Rust Lambda Fun
284310
| Name | Description |
285311
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
286312
| `BUILD_INDIVIDUALLY` | Whether to build each executable individually, either via `--bin` or `--package`. |
287-
| `RUN_CARGO_CHECK` | Whether to run `cargo check` to validate Rust code before building it with `cross`. Defaults to _true_. |
288313
| `DEFAULT_LOG_LEVEL` | Log Level for non-module libraries. Note that this value is only used when `RustFunctionProps.setupLogging` is enabled. Defaults to `warn`. |
289314
| `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`. |
290315
| `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. |
291316
| `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 function itself. |
293-
| `EXTRA_BUILD_ARGS` | Additional arguments that are passed in at build time to both `cargo check` and `cross build`. For example, [`--all-features`]. |
317+
| `BUILD_ENVIRONMENT` | Key-value pairs that are passed in at compile time, i.e. to `cargo build` or `cargo zigbuild`. This differs from `environment`, which determines the environment variables which are set on the AWS Lambda function itself. |
318+
| `EXTRA_BUILD_ARGS` | Additional arguments that are passed in at build time to both `cargo zigbuild`. For example, [`--all-features`]. |

cdk-examples/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ cd my-app
1717
npm i
1818
```
1919

20-
Next, ensure that you have [Docker](https://www.docker.com/get-started) running. You will need this to deploy with [`cross`](https://github.com/cross-rs/cross).
21-
If you don't have `cross` installed, refer to the [Getting Started](https://github.com/rnag/rust.aws-cdk-lambda#getting-started) section in docs for more info on getting set up.
20+
Next, ensure that you have [`cargo-zigbuild`](https://github.com/messense/cargo-zigbuild) installed. You will need this to cross-compile Rust code for deployment via `cdk`.
21+
If you don't have `cargo-zigbuild` installed, refer to the [Getting Started](https://github.com/rnag/rust.aws-cdk-lambda#getting-started) section in docs for more info on getting set up.
2222

2323
Now you can deploy the app with `cdk`:
2424

25-
> Note: on an initial run, it may take a _really_ long time on the compilation step with `cross`. In my case, it sometimes took up to _12 minutes_! But don't worry, any subsequent deployments should be overall faster.
25+
> Note: on an initial run, it may take a _really_ long time on the compilation step with `cargo-zigbuild`. In my case, it sometimes took up to _12 minutes_! But don't worry, any subsequent deployments should be overall faster.
2626
2727
```shell
2828
npx cdk deploy

cdk-examples/rust-bins/lib/rust-bins-stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class RustBinsStack extends Stack {
2525
// Settings.BUILD_INDIVIDUALLY = true;
2626

2727
// Uncomment to cross-compile Rust code to a different Lambda architecture.
28-
// Settings.TARGET = 'aarch64-unknown-linux-gnu';
28+
// Settings.TARGET = 'x86_64-unknown-linux-gnu';
2929

3030
const bucket = new s3.Bucket(this, 'RustBinsBucket', {
3131
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,

cdk-examples/rust-bins/src/bin/my_lambda1/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ pub(crate) async fn my_handler(event: Request, ctx: Context) -> Response {
3333
// in the CDK code in the `lib/` folder.
3434
utils::log_enabled_features();
3535

36+
#[cfg(feature = "my-prod-feature")]
37+
{
38+
debug!("Now running in a PROD environment (my-prod-feature). Beep... Boop.");
39+
let answer = systems::system_a::do_stuff_from_system_a();
40+
info!(
41+
"The answer to life, the universe, and everything is: {}",
42+
answer
43+
);
44+
}
45+
3646
let bucket_name = var("BUCKET_NAME")
3747
.expect("A BUCKET_NAME must be set in this app's Lambda environment variables.");
3848

cdk-examples/rust-standalone/src/main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,21 @@ pub(crate) async fn my_handler(event: Request, ctx: Context) -> Response {
7575
info!("Congrats! The 1st feature (my-first-feature) is enabled.");
7676

7777
#[cfg(feature = "my-second-feature")]
78-
info!("Congrats! The 2nd feature (my-second-feature) is enabled.");
78+
{
79+
info!("Congrats! The 2nd feature (my-second-feature) is enabled.");
80+
81+
debug!(
82+
"This is a debug message. This line is included to show that we can \
83+
conditionally run more than one statement when a feature is enabled."
84+
);
85+
}
7986

8087
// retrieve an environment variable set at build (compile) time.
8188
let build_env_var = env!("MY_BUILD_ENV_VAR");
82-
debug!("Resolved compile-time value of `MY_BUILD_ENV_VAR`: {}", build_env_var);
89+
debug!(
90+
"Resolved compile-time value of `MY_BUILD_ENV_VAR`: {}",
91+
build_env_var
92+
);
8393

8494
// retrieve an environment variable set in the lambda.
8595
let bucket_name = var("BUCKET_NAME")

cdk-examples/rust-workspaces/lib/rust-workspaces-stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class RustWorkspacesStack extends Stack {
2828
// Settings.BUILD_INDIVIDUALLY = true;
2929

3030
// Uncomment to cross-compile Rust code to a different Lambda architecture.
31-
// Settings.TARGET = 'aarch64-unknown-linux-gnu';
31+
// Settings.TARGET = 'x86_64-unknown-linux-gnu';
3232

3333
const bucket = new s3.Bucket(this, 'RustWorkspacesBucket', {
3434
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,

cdk-examples/rust-workspaces/my_lambdas/my_lambda1/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ pub(crate) async fn my_handler(event: Request, ctx: Context) -> Response {
3333
// in the CDK code in the `lib/` folder.
3434
utils::log_enabled_features();
3535

36+
#[cfg(feature = "my-prod-feature")]
37+
{
38+
debug!("Now running in a PROD environment (my-prod-feature). Beep... Boop.");
39+
let answer = systems::system_a::do_stuff_from_system_a();
40+
info!(
41+
"The answer to life, the universe, and everything is: {}",
42+
answer
43+
);
44+
}
45+
3646
let bucket_name = var("BUCKET_NAME")
3747
.expect("A BUCKET_NAME must be set in this app's Lambda environment variables.");
3848

0 commit comments

Comments
 (0)