Package: cargo-lambda-cdk
Cargo Lambda CDK constructThis library provides constructs for Rust Lambda functions built with Cargo LambdaTo use this module you will either need to haveCargo Lambda installed(0.12.0or later), orDockerinstalled.
SeeLocal Bundling/Docker Bundlingfor more information.Rust FunctionDefine aRustFunction:import{RustFunction}from'cargo-lambda-cdk';newRustFunction(stack,'Rust function',{manifestPath:'path/to/package/directory/with/Cargo.toml',});The layout for this Rust project could look like this:lambda-project
├──Cargo.toml
└──src└──main.rsRuntimeTheRustFunctionuses theprovided.al2023runtime. If you want to change it, you can use the propertyruntime. The only other valid option isprovided.al2:```ts
import { RustFunction } from 'cargo-lambda-cdk';

new RustFunction(stack, 'Rust function', {
  manifestPath: 'path/to/package/directory/with/Cargo.toml',
  runtime: 'provided.al2',
});Rust ExtensionDefine aRustExtensionthat get's deployed as a layer to use it with any other function later.import{RustExtension,RustFunction}from'cargo-lambda-cdk';constextensionLayer=newRustExtension(this,'Rust extension',{manifestPath:'path/to/package/directory/with/Cargo.toml',});newRustFunction(this,'Rust function',{manifestPath:'path/to/package/directory/with/Cargo.toml',layers:[extensionLayer],});EnvironmentUse theenvironmentprop to define additional environment variables when Cargo Lambda runs:import{RustFunction}from'cargo-lambda-cdk';newRustFunction(this,'Rust function',{manifestPath:'path/to/package/directory/with/Cargo.toml',bundling:{environment:{HELLO:'WORLD',},},});Local BundlingIfCargo Lambdais installed locally then it will be used to bundle your code in your environment. Otherwise, bundling will happen in a Lambda compatible Docker container with the Docker platform based on the target architecture of the Lambda function.DockerTo force bundling in a docker container even ifCargo Lambdais available in your environment, set theforcedDockerBundlingprop totrue. This is useful if you want to make sure that your function is built in a consistent Lambda compatible environment.By default, these constructs useghcr.io/cargo-lambda/cargo-lambdaas the image to build with. Use thebundling.dockerImageprop to use a custom bundling image:import{RustFunction}from'cargo-lambda-cdk';newRustFunction(this,'Rust function',{manifestPath:'path/to/package/directory/with/Cargo.toml',bundling:{dockerImage:DockerImage.fromBuild('/path/to/Dockerfile'),},});Command hooksIt is  possible to run additional commands by specifying thecommandHooksprop:import{RustFunction}from'cargo-lambda-cdk';newRustFunction(this,'Rust function',{manifestPath:'path/to/package/directory/with/Cargo.toml',bundling:{commandHooks:{//runtestsbeforeBundling(inputDir:string,_outputDir:string):string[]{return['cargo test'];},},},});The following hooks are available:beforeBundling: runs before all bundling commandsafterBundling: runs after all bundling commandsThey all receive the directory containing theCargo.tomlfile (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.Additional considerationsDepending on how you structure your Rust application, you may want to change theassetHashTypeparameter.
By default this parameter is set toAssetHashType.OUTPUTwhich means that the CDK will calculate the asset hash
(and determine whether or not your code has changed) based on the Rust executable that is created.If you specifyAssetHashType.SOURCE, the CDK will calculate the asset hash by looking at the folder
that contains yourCargo.tomlfile. If you are deploying a single Lambda function, or you want to redeploy
all of your functions if anything changes, thenAssetHashType.SOURCEwill probaby work.LICENSEThis software is released under MIT license.