Skip to content

Commit

Permalink
feat: minimum tweak to use bundling for building
Browse files Browse the repository at this point in the history
- This work is done according to the comment:
  rnag#10 (comment)
  • Loading branch information
kikuomax committed Mar 8, 2023
1 parent 465567c commit 04f16a3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
40 changes: 33 additions & 7 deletions lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ import {
Code,
Runtime,
} from 'aws-cdk-lib/aws-lambda';
import { dirname } from 'path';
import * as fs from 'fs';
import * as path from 'path';
import { build } from './build';

/**
* Options for bundling
*/
export interface BundlingProps extends DockerRunOptions {
/**
* CDK output (staging) directory for the lambda function
* Path to the directory that contains the project to be built; i.e., the
* directory containing `Cargo.toml`.
*/
readonly handlerDir: string;
readonly entry: string;

/**
* Executable name.
*/
readonly bin?: string;

/**
* The runtime of the lambda function
Expand All @@ -26,6 +34,11 @@ export interface BundlingProps extends DockerRunOptions {
* The system architecture of the lambda function
*/
readonly architecture: Architecture;

/**
* Target of `cargo build`.
*/
readonly target: string;
}

/**
Expand All @@ -35,7 +48,7 @@ export class Bundling implements cdk.BundlingOptions {
public static bundle(options: BundlingProps): AssetCode {
const bundling = new Bundling(options);

return Code.fromAsset(dirname(options.handlerDir), {
return Code.fromAsset(options.entry, {
assetHashType: cdk.AssetHashType.OUTPUT,
bundling: {
image: bundling.image,
Expand All @@ -54,9 +67,22 @@ export class Bundling implements cdk.BundlingOptions {
this.image = cdk.DockerImage.fromRegistry('dummy'); // Do not build if we don't need to

this.local = {
tryBundle(outputDir: string) {
// TODO
console.log(`BUNDLING...: ${outputDir}`);
tryBundle(outDir: string) {
console.log(`BUNDLING...: ${outDir}`);
build({
entry: props.entry,
bin: props.bin,
target: props.target,
outDir,
});
// moves <bin>/bootstrap → ./bootstrap
// and cleans up the empty folder
const binDir = path.join(outDir, props.bin!);
fs.renameSync(
path.join(binDir, 'bootstrap'),
path.join(outDir, 'bootstrap')
);
fs.rmdirSync(binDir);

return true;
},
Expand Down
38 changes: 9 additions & 29 deletions lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Construct } from 'constructs';
import { join } from 'path';
import { performance } from 'perf_hooks';
import { Settings } from '.';
import { BaseBuildProps, build } from './build';
// import { Bundling } from './bundling';
import { BaseBuildProps } from './build';
import { Bundling } from './bundling';
import {
Code,
Function,
Expand Down Expand Up @@ -99,27 +99,6 @@ export class RustFunction extends Function {
'T'
);

// Build with `cargo-lambda`
if (shouldBuild && !Settings.SKIP_BUILD) {
let start = performance.now();

build({
...props,
entry,
bin: binName,
target: target,
outDir: buildDir,
});

logTime(start, `🎯 Cross-compile \`${executable}\``);
}
// Else, skip the build (or bundle) step.
//
// At a minimum, we need to ensure the output directory
// exists -- otherwise, CDK complains that it can't
// locate the asset.
else ensureDirExists(handlerDir);

let lambdaEnv = props.environment;
// Sets up logging if needed.
// Ref: https://rust-lang-nursery.github.io/rust-cookbook/development_tools/debugging/config_log.html
Expand All @@ -136,12 +115,13 @@ export class RustFunction extends Function {
...props,
runtime: Settings.RUNTIME,
architecture: arch,
code: Code.fromAsset(handlerDir),
// code: Bundling.bundle({
// handlerDir,
// runtime: Settings.RUNTIME,
// architecture: arch,
// }),
code: Bundling.bundle({
entry,
bin: binName,
runtime: Settings.RUNTIME,
architecture: arch,
target,
}),
handler: handler,
environment: lambdaEnv,
});
Expand Down

0 comments on commit 04f16a3

Please sign in to comment.