Skip to content

Commit 04f16a3

Browse files
committed
feat: minimum tweak to use bundling for building
- This work is done according to the comment: rnag#10 (comment)
1 parent 465567c commit 04f16a3

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

lib/bundling.ts

+33-7
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ import {
66
Code,
77
Runtime,
88
} from 'aws-cdk-lib/aws-lambda';
9-
import { dirname } from 'path';
9+
import * as fs from 'fs';
10+
import * as path from 'path';
11+
import { build } from './build';
1012

1113
/**
1214
* Options for bundling
1315
*/
1416
export interface BundlingProps extends DockerRunOptions {
1517
/**
16-
* CDK output (staging) directory for the lambda function
18+
* Path to the directory that contains the project to be built; i.e., the
19+
* directory containing `Cargo.toml`.
1720
*/
18-
readonly handlerDir: string;
21+
readonly entry: string;
22+
23+
/**
24+
* Executable name.
25+
*/
26+
readonly bin?: string;
1927

2028
/**
2129
* The runtime of the lambda function
@@ -26,6 +34,11 @@ export interface BundlingProps extends DockerRunOptions {
2634
* The system architecture of the lambda function
2735
*/
2836
readonly architecture: Architecture;
37+
38+
/**
39+
* Target of `cargo build`.
40+
*/
41+
readonly target: string;
2942
}
3043

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

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

5669
this.local = {
57-
tryBundle(outputDir: string) {
58-
// TODO
59-
console.log(`BUNDLING...: ${outputDir}`);
70+
tryBundle(outDir: string) {
71+
console.log(`BUNDLING...: ${outDir}`);
72+
build({
73+
entry: props.entry,
74+
bin: props.bin,
75+
target: props.target,
76+
outDir,
77+
});
78+
// moves <bin>/bootstrap → ./bootstrap
79+
// and cleans up the empty folder
80+
const binDir = path.join(outDir, props.bin!);
81+
fs.renameSync(
82+
path.join(binDir, 'bootstrap'),
83+
path.join(outDir, 'bootstrap')
84+
);
85+
fs.rmdirSync(binDir);
6086

6187
return true;
6288
},

lib/function.ts

+9-29
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Construct } from 'constructs';
33
import { join } from 'path';
44
import { performance } from 'perf_hooks';
55
import { Settings } from '.';
6-
import { BaseBuildProps, build } from './build';
7-
// import { Bundling } from './bundling';
6+
import { BaseBuildProps } from './build';
7+
import { Bundling } from './bundling';
88
import {
99
Code,
1010
Function,
@@ -99,27 +99,6 @@ export class RustFunction extends Function {
9999
'T'
100100
);
101101

102-
// Build with `cargo-lambda`
103-
if (shouldBuild && !Settings.SKIP_BUILD) {
104-
let start = performance.now();
105-
106-
build({
107-
...props,
108-
entry,
109-
bin: binName,
110-
target: target,
111-
outDir: buildDir,
112-
});
113-
114-
logTime(start, `🎯 Cross-compile \`${executable}\``);
115-
}
116-
// Else, skip the build (or bundle) step.
117-
//
118-
// At a minimum, we need to ensure the output directory
119-
// exists -- otherwise, CDK complains that it can't
120-
// locate the asset.
121-
else ensureDirExists(handlerDir);
122-
123102
let lambdaEnv = props.environment;
124103
// Sets up logging if needed.
125104
// Ref: https://rust-lang-nursery.github.io/rust-cookbook/development_tools/debugging/config_log.html
@@ -136,12 +115,13 @@ export class RustFunction extends Function {
136115
...props,
137116
runtime: Settings.RUNTIME,
138117
architecture: arch,
139-
code: Code.fromAsset(handlerDir),
140-
// code: Bundling.bundle({
141-
// handlerDir,
142-
// runtime: Settings.RUNTIME,
143-
// architecture: arch,
144-
// }),
118+
code: Bundling.bundle({
119+
entry,
120+
bin: binName,
121+
runtime: Settings.RUNTIME,
122+
architecture: arch,
123+
target,
124+
}),
145125
handler: handler,
146126
environment: lambdaEnv,
147127
});

0 commit comments

Comments
 (0)