From 39f2210c14ad7861f0cecb9d630702fdfed10915 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Fri, 3 May 2024 12:18:49 -0700 Subject: [PATCH 1/3] Allow to set extra build flags. Cargo Lambda Build has additional flags that can be set to modify the build output. This new bundling option allows to set extra flags that are not provided by other options already. Signed-off-by: David Calavera --- README.md | 22 ++++++++++++++++++++++ src/bundling.ts | 31 +++++++++++++++++++++---------- src/function.ts | 2 +- test/bundlingOptions.test.ts | 12 ++++++++++++ 4 files changed, 56 insertions(+), 11 deletions(-) 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..89f2c53 100644 --- a/src/bundling.ts +++ b/src/bundling.ts @@ -40,9 +40,15 @@ export interface BundlingProps extends BundlingOptions { readonly lambdaExtension?: boolean; /** + * @deprecated Use the `cargoLambdaFlags` option instead. * Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda). */ readonly disableOptimizations?: boolean; + + /** + * Set a list of flags to pass to `cargo lambda build`. + */ + readonly cargoLambdaFlags?: string[]; } interface CommandOptions { @@ -52,7 +58,7 @@ interface CommandOptions { readonly osPlatform: NodeJS.Platform; readonly architecture?: Architecture; readonly lambdaExtension?: boolean; - readonly disableOptimizations?: boolean; + readonly cargoLambdaFlags: string[]; readonly manifest: Manifest; } @@ -108,16 +114,21 @@ export class Bundling implements cdk.BundlingOptions { const manifest = getManifest(props.manifestPath); + const cargoLambdaFlags = props.cargoLambdaFlags ?? []; + if (props.disableOptimizations) { + cargoLambdaFlags.push('--disable-optimizations'); + } + 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 +141,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 +185,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 +235,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..96b48a9 100644 --- a/src/function.ts +++ b/src/function.ts @@ -41,6 +41,7 @@ export interface RustFunctionProps extends FunctionOptions { readonly bundling?: BundlingOptions; /** + * @deprecated Use BundlingOptions#cargoLambdaFlags instead * Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda). */ readonly disableOptimizations?: boolean; @@ -64,7 +65,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..14a7ec0 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); + }) }); From 6e3eabf44c856a5e8c4823c261db8480219124e3 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Fri, 3 May 2024 12:42:49 -0700 Subject: [PATCH 2/3] Remove option that was never released. Signed-off-by: David Calavera --- src/bundling.ts | 9 --------- src/function.ts | 6 ------ test/function.test.ts | 18 ------------------ 3 files changed, 33 deletions(-) diff --git a/src/bundling.ts b/src/bundling.ts index 89f2c53..1a54cde 100644 --- a/src/bundling.ts +++ b/src/bundling.ts @@ -39,12 +39,6 @@ export interface BundlingProps extends BundlingOptions { */ readonly lambdaExtension?: boolean; - /** - * @deprecated Use the `cargoLambdaFlags` option instead. - * Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda). - */ - readonly disableOptimizations?: boolean; - /** * Set a list of flags to pass to `cargo lambda build`. */ @@ -115,9 +109,6 @@ export class Bundling implements cdk.BundlingOptions { const manifest = getManifest(props.manifestPath); const cargoLambdaFlags = props.cargoLambdaFlags ?? []; - if (props.disableOptimizations) { - cargoLambdaFlags.push('--disable-optimizations'); - } const osPlatform = platform(); const bundlingCommand = this.createBundlingCommand({ diff --git a/src/function.ts b/src/function.ts index 96b48a9..5df6039 100644 --- a/src/function.ts +++ b/src/function.ts @@ -39,12 +39,6 @@ export interface RustFunctionProps extends FunctionOptions { * @default - use default bundling options */ readonly bundling?: BundlingOptions; - - /** - * @deprecated Use BundlingOptions#cargoLambdaFlags instead - * Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda). - */ - readonly disableOptimizations?: boolean; } /** 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(); - }); - }); }); From 8c427f4a023484ca03e97ab3d7aa8c1377385e78 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Fri, 3 May 2024 13:06:06 -0700 Subject: [PATCH 3/3] Update docs. Signed-off-by: David Calavera --- API.md | 35 ++++++++++++++++++++++------------- test/bundlingOptions.test.ts | 2 +- 2 files changed, 23 insertions(+), 14 deletions(-) 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/test/bundlingOptions.test.ts b/test/bundlingOptions.test.ts index 14a7ec0..ca07370 100644 --- a/test/bundlingOptions.test.ts +++ b/test/bundlingOptions.test.ts @@ -144,5 +144,5 @@ describe('bundlingOptionsOverrideDefaults', () => { const command = 'cargo lambda build --lambda-dir /asset-output --release --flatten simple-package --disable-optimizations'; expect((bundlingOptions as any).options.bundling.command).toContain(command); - }) + }); });