Package: aws-cdk.aws-lambda-rust
Amazon Lambda Rust Library---The APIs of higher level constructs in this module are experimental and under active development.
They are subject to non-backward compatible changes or removal in any future version. These are
not subject to theSemantic Versioningmodel and breaking changes will be
announced in the release notes. This means that while you may use them, you may need to update
your source code when upgrading to a newer version of this package.This library provides constructs for Rust Lambda functions.Therust.RustFunctionconstruct creates a Lambda function with automatic building and bundling of Rust code.To use this module you will either need to havecargo-lambdainstalled or Docker installed.SeeLocal Bundling/Docker Bundlingfor more information.Rust Function.
├── my-construct.ts
├── myconstruct.my_function
│    ├── Cargo.toml
│    └── src
│        └── main.rsBy default, the construct will use the name of the defining file and the construct's id to look up the entry file by following this patterndefining-file.id/Cargo.toml. Inmy-construct.tsabove we have:my_function=rust.RustFunction(self,"my_function")Alternatively,entryandbinaryNameproperties can be specified to override this default behavior. If nobinaryNameargument is passed in, it will default to the package name as defined in the mainCargo.toml.rust.RustFunction(self,"my_function1",entry="/path/to/directory/with/Cargo.toml",binary_name="my_function")# You can also specify entry path directory which will contain your Cargo.toml filerust.RustFunction(self,"my_function2",entry="/path/to/directory",binary_name="my_function")For more complex project structure combining multiple Rust Lambda function, the construct offer the ability to use Binaries orWorkspacesdefined in your Cargo project.Given a sample project structure:.
├── Cargo.toml
├── my_lambda_1
│   ├── Cargo.toml
│   └── src
│       └── main.rs
└── my_lambda_2
    ├── Cargo.toml
    └── src
        └── main.rsAnd with a givenCargo.toml:[workspace]members=["my_lambda_1","my_lambda_2"]Rust functions can be declared usingbinaryNameproperty:rust.RustFunction(self,"FirstFunction",binary_name="my_lambda_1")rust.RustFunction(self,"SecondFunction",binary_name="my_lambda_2")How It WorksWhen bundling the code, therust.RustFunctionruns will build and bundle your binary by passing--releaseand--targetflags to the Cargo build command, so it compiles for a Lambda environment - which defaults to theaarch64-unknown-linux-gnutarget.Usecrossfor DeploymentIf you instead prefer to useDockerandCrossfor deployment, as outlined
in theofficial AWS docs, you can define thepackageManagerproperty:rust.RustFunction(self,"FirstFunction",binary_name="my_lambda_1",bundling=rust.BundlingOptions(package_manager_type=rust.PackageManagerType.CROSS))Customizing the underlying Lambda functionAll properties oflambda.Functioncan be used to customize the underlyinglambda.Function.See also theAWS Lambda construct library.Local BundlingBy default, Cargo Lambda uses theZig toolchainto cross compile your code.
This is the most convenient cross compilation mechanism because it comes built in, and it works for the majority of use cases.
Any pure Rust Lambda function should compile correctly with this toolchain.By default, Construct will compile the code for Linux X86-64 architectures, but you can compile for ARM-64 architectures by providing the right property if needed:fromaws_cdk.aws_lambdaimportArchitecturerust.RustFunction(self,"my_function",architecture=Architecture.ARM_64)If bundling is made locally forARM-64orX86-64, make sure to install the dedicated target:rustuptargetaddaarch64-unknown-linux-gnu
rustuptargetaddx86_64-unknown-linux-gnuCustomizing Docker bundlingTo force bundling in a Docker container even ifZig toolchainis available in your environment, setbundling.forceDockerBundlingto true.Usebundling.environmentto define environments variables whencargoruns:rust.RustFunction(self,"my_function",bundling=rust.BundlingOptions(environment={"HELLO":"world"}))Use thebundling.buildArgsto pass build arguments tocargo:rust.RustFunction(self,"my_function",bundling=rust.BundlingOptions(extra_build_args=["--all-features"]))Use thebundling.dockerImageto use a custom Docker bundling image:rust.RustFunction(self,"my_function",bundling=rust.BundlingOptions(docker_image=DockerImage.from_build("/path/to/Dockerfile")))You can set additional Docker options to configure the build environment:rust.RustFunction(self,"my_function",bundling=rust.BundlingOptions(network="host",security_opt="no-new-privileges",user="user:group",volumes_from=["777f7dc92da7"],volumes=[DockerVolume(host_path="/host-path",container_path="/container-path")]))Command hooksIt is possible to run additional commands by specifying the commandHooks property.The following hooks are available:beforeBundling: runs before all bundling commandsafterBundling: runs after all bundling commandsThey all receive the directory containing the lock file (inputDir) and the directory where the bundled asset will be output (outputDir).
They must return an array of commands to run. Commands are chained with &&.The commands will run in the environment in which bundling occurs: inside the container for Docker bundling or on the host OS for local bundling.