diff --git a/API.md b/API.md index d62fe52..9a02c81 100644 --- a/API.md +++ b/API.md @@ -85,6 +85,28 @@ new RustFunction(this, 'Rust function', { }); ``` +### Cargo Lambda Build flags + +Use the `cargoLambdaFlags` option to add additional flags to the `cargo lambda build` command that's executed to bundle your function. You don't need to use this flag to set options like the target architecture or the binary to compile, since the construct infers those from other props. + +If these flags include a `--target` flag, it will override the `architecture` option. If these flags include a `--release` or `--debug` flag, it will override the CDK's debug option. + +```ts +import { RustFunction } from 'cargo-lambda-cdk'; + +new RustFunction(this, 'Rust function', { + manifestPath: 'path/to/package/directory/with/Cargo.toml', + bundling: { + cargoLambdaFlags: [ + '--target', + 'x86_64-unknown-linux-musl', + '--debug', + '--disable-optimizations', + ], + }, +}); +``` + ### Docker To force bundling in a docker container even if `Cargo Lambda` is available in your environment, set the `forcedDockerBundling` prop to `true`. This is useful if you want to make sure that your function is built in a consistent Lambda compatible environment. @@ -2040,7 +2062,6 @@ const rustFunctionProps: RustFunctionProps = { ... } | vpcSubnets | aws-cdk-lib.aws_ec2.SubnetSelection | Where to place the network interfaces within the VPC. | | binaryName | string | The name of the binary to build, in case that's different than the package's name. | | bundling | BundlingOptions | Bundling options. | -| disableOptimizations | boolean | Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda). | | manifestPath | string | Path to a directory containing your Cargo.toml file, or to your Cargo.toml directly. | | runtime | string | The Lambda runtime to deploy this function. | @@ -2616,18 +2637,6 @@ Bundling options. --- -##### `disableOptimizations`Optional - -```typescript -public readonly disableOptimizations: boolean; -``` - -- *Type:* boolean - -Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda). - ---- - ##### `manifestPath`Optional ```typescript diff --git a/README.md b/README.md index 1053105..9ae080a 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,28 @@ new RustFunction(this, 'Rust function', { }); ``` +### Cargo Lambda Build flags + +Use the `cargoLambdaFlags` option to add additional flags to the `cargo lambda build` command that's executed to bundle your function. You don't need to use this flag to set options like the target architecture or the binary to compile, since the construct infers those from other props. + +If these flags include a `--target` flag, it will override the `architecture` option. If these flags include a `--release` or `--debug` flag, it will override the CDK's debug option. + +```ts +import { RustFunction } from 'cargo-lambda-cdk'; + +new RustFunction(this, 'Rust function', { + manifestPath: 'path/to/package/directory/with/Cargo.toml', + bundling: { + cargoLambdaFlags: [ + '--target', + 'x86_64-unknown-linux-musl', + '--debug', + '--disable-optimizations', + ], + }, +}); +``` + ### Docker To force bundling in a docker container even if `Cargo Lambda` is available in your environment, set the `forcedDockerBundling` prop to `true`. This is useful if you want to make sure that your function is built in a consistent Lambda compatible environment. diff --git a/src/bundling.ts b/src/bundling.ts index 25d9384..1a54cde 100644 --- a/src/bundling.ts +++ b/src/bundling.ts @@ -40,9 +40,9 @@ export interface BundlingProps extends BundlingOptions { readonly lambdaExtension?: boolean; /** - * Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda). + * Set a list of flags to pass to `cargo lambda build`. */ - readonly disableOptimizations?: boolean; + readonly cargoLambdaFlags?: string[]; } interface CommandOptions { @@ -52,7 +52,7 @@ interface CommandOptions { readonly osPlatform: NodeJS.Platform; readonly architecture?: Architecture; readonly lambdaExtension?: boolean; - readonly disableOptimizations?: boolean; + readonly cargoLambdaFlags: string[]; readonly manifest: Manifest; } @@ -108,16 +108,18 @@ export class Bundling implements cdk.BundlingOptions { const manifest = getManifest(props.manifestPath); + const cargoLambdaFlags = props.cargoLambdaFlags ?? []; + const osPlatform = platform(); const bundlingCommand = this.createBundlingCommand({ osPlatform, manifest, + cargoLambdaFlags, outputDir: cdk.AssetStaging.BUNDLING_OUTPUT_DIR, inputDir: cdk.AssetStaging.BUNDLING_INPUT_DIR, binaryName: props.binaryName, architecture: props.architecture, lambdaExtension: props.lambdaExtension, - disableOptimizations: props.disableOptimizations, }); this.command = ['bash', '-c', bundlingCommand]; @@ -130,11 +132,11 @@ export class Bundling implements cdk.BundlingOptions { osPlatform, manifest, outputDir, + cargoLambdaFlags, inputDir: projectRoot, binaryName: props.binaryName, architecture: props.architecture, lambdaExtension: props.lambdaExtension, - disableOptimizations: props.disableOptimizations, }); }; @@ -174,20 +176,20 @@ export class Bundling implements cdk.BundlingOptions { 'cargo', 'lambda', 'build', - '--release', '--lambda-dir', props.outputDir, ]; - if (props.lambdaExtension) { - buildBinary.push('--extension'); + if (!props.cargoLambdaFlags.includes('--release') && + !props.cargoLambdaFlags.includes('--debug')) { + buildBinary.push('--release'); } - if (props.disableOptimizations) { - buildBinary.push('--disable-optimizations'); + if (props.lambdaExtension) { + buildBinary.push('--extension'); } - if (props.architecture) { + if (props.architecture && !props.cargoLambdaFlags.includes('--target')) { const targetFlag = props.architecture.name == Architecture.ARM_64.name ? '--arm64' : '--x86-64'; buildBinary.push(targetFlag); } @@ -224,7 +226,7 @@ export class Bundling implements cdk.BundlingOptions { buildBinary.push(packageName); } - const command = buildBinary.join(' '); + const command = buildBinary.concat(props.cargoLambdaFlags).join(' '); return chain([ ...this.props.commandHooks?.beforeBundling(props.inputDir, props.outputDir) ?? [], diff --git a/src/function.ts b/src/function.ts index 37e26af..5df6039 100644 --- a/src/function.ts +++ b/src/function.ts @@ -39,11 +39,6 @@ export interface RustFunctionProps extends FunctionOptions { * @default - use default bundling options */ readonly bundling?: BundlingOptions; - - /** - * Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda). - */ - readonly disableOptimizations?: boolean; } /** @@ -64,7 +59,6 @@ export class RustFunction extends Function { ...bundling, manifestPath, binaryName: props?.binaryName, - disableOptimizations: props?.disableOptimizations, }), handler: 'bootstrap', }); diff --git a/test/bundlingOptions.test.ts b/test/bundlingOptions.test.ts index ae93cde..ca07370 100644 --- a/test/bundlingOptions.test.ts +++ b/test/bundlingOptions.test.ts @@ -133,4 +133,16 @@ describe('bundlingOptionsOverrideDefaults', () => { expect((bundlingOptions as any).options.bundling.bundlingFileAccess).toEqual(cdk.BundlingFileAccess.VOLUME_COPY); }); + + describe('Add Cargo Lambda Build flags', () => { + const bundlingOptions = Bundling.bundle({ + manifestPath: getTestManifestPath(), + forcedDockerBundling: true, + cargoLambdaFlags: ['--disable-optimizations'], + }); + + const command = 'cargo lambda build --lambda-dir /asset-output --release --flatten simple-package --disable-optimizations'; + + expect((bundlingOptions as any).options.bundling.command).toContain(command); + }); }); diff --git a/test/function.test.ts b/test/function.test.ts index 1b4f991..a7e88ce 100644 --- a/test/function.test.ts +++ b/test/function.test.ts @@ -66,22 +66,4 @@ describe('CargoLambda.RustFunction', () => { app.synth(); }); }); - - describe('With disableOptimizations', () => { - const app = new App(); - const stack = new Stack(app); - const testSource = join(__dirname, 'fixtures/single-package'); - - new RustFunction(stack, 'rust function', { - manifestPath: testSource, - bundling: { - forcedDockerBundling, - }, - disableOptimizations: true, - }); - - test('bundle function', () => { - app.synth(); - }); - }); });