Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lambda-nodejs): local bundling #9632

Merged
merged 19 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,24 @@ new lambda.NodejsFunction(this, 'my-handler', {

The modules listed in `nodeModules` must be present in the `package.json`'s dependencies. The
same version will be used for installation. If a lock file is detected (`package-lock.json` or
`yarn.lock`) it will be used along with the right installer (`npm` or `yarn`). The modules are
installed in a [Lambda compatible Docker container](https://hub.docker.com/r/amazon/aws-sam-cli-build-image-nodejs12.x).
`yarn.lock`) it will be used along with the right installer (`npm` or `yarn`).


### Local bundling
If Parcel v2 is available it will be used to bundle your code in your environment. Otherwise,
eladb marked this conversation as resolved.
Show resolved Hide resolved
bundling will happen in a [Lambda compatible Docker container](https://hub.docker.com/r/amazon/aws-sam-cli-build-image-nodejs12.x).

Parcel v2 can be installed with:

```bash
$ npm install --save-dev parcel@next
```

OR

```bash
$ yarn add --dev @parcel@next
```

To force bundling in a Docker container, set the `forceDockerBundling` prop to `true`. This is useful
when installing node modules with native dependencies.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sentence "This is useful when installing node modules with native dependencies" is a bit cryptic. Elaborate a bit or provide an example.

256 changes: 191 additions & 65 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { spawnSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { PackageJsonManager } from './package-json-manager';
import { findUp } from './util';
import { exec, findUp } from './util';

/**
* Base options for Parcel bundling
Expand All @@ -24,11 +26,11 @@ export interface ParcelBaseOptions {
readonly sourceMaps?: boolean;

/**
* The cache directory
* The cache directory (relative to the project root)
*
* Parcel uses a filesystem cache for fast rebuilds.
*
* @default - `.cache` in the root directory
* @default - `.parcel-cache` in the working directory
*/
readonly cacheDir?: string;

Expand Down Expand Up @@ -78,6 +80,15 @@ export interface ParcelBaseOptions {
* @default - no build arguments are passed
*/
readonly buildArgs?: { [key:string] : string };

/**
* Force bundling in a Docker container even if local bundling is
* possible. This is useful when installing node modules with
* native dependencies.
*
* @default false
*/
readonly forceDockerBundling?: boolean;
}

/**
Expand Down Expand Up @@ -108,15 +119,7 @@ export class Bundling {
if (!projectRoot) {
throw new Error('Cannot find project root. Please specify it with `projectRoot`.');
}

// Bundling image derived from runtime bundling image (AWS SAM docker image)
const image = cdk.BundlingDockerImage.fromAsset(path.join(__dirname, '../parcel'), {
buildArgs: {
...options.buildArgs ?? {},
IMAGE: options.runtime.bundlingDockerImage.image,
PARCEL_VERSION: options.parcelVersion ?? '2.0.0-beta.1',
},
});
const relativeEntryPath = path.relative(projectRoot, path.resolve(options.entry));

const packageJsonManager = new PackageJsonManager(path.dirname(options.entry));

Expand All @@ -135,6 +138,19 @@ export class Bundling {
}
}

let installer = Installer.NPM;
let lockFile: LockFile | undefined;
if (dependencies) {
// Use npm unless we have a yarn.lock.
if (fs.existsSync(path.join(projectRoot, LockFile.YARN))) {
installer = Installer.YARN;
lockFile = LockFile.YARN;
} else if (fs.existsSync(path.join(projectRoot, LockFile.NPM))) {
lockFile = LockFile.NPM;
}
}


// Configure target in package.json for Parcel
packageJsonManager.update({
targets: {
Expand All @@ -150,69 +166,179 @@ export class Bundling {
},
});

// Entry file path relative to container path
const containerEntryPath = path.join(cdk.AssetStaging.BUNDLING_INPUT_DIR, path.relative(projectRoot, path.resolve(options.entry)));
const distFile = path.basename(options.entry).replace(/\.ts$/, '.js');
const parcelCommand = chain([
[
'$(node -p "require.resolve(\'parcel\')")', // Parcel is not globally installed, find its "bin"
'build', containerEntryPath.replace(/\\/g, '/'), // Always use POSIX paths in the container
'--target', 'cdk-lambda',
'--dist-dir', cdk.AssetStaging.BUNDLING_OUTPUT_DIR, // Output bundle in /asset-output (will have the same name as the entry)
'--no-autoinstall',
'--no-scope-hoist',
...options.cacheDir
? ['--cache-dir', '/parcel-cache']
: [],
].join(' '),
// Always rename dist file to index.js because Lambda doesn't support filenames
// with multiple dots and we can end up with multiple dots when using automatic
// entry lookup
`mv ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/${distFile} ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/index.js`,
]);

let installer = Installer.NPM;
let lockfile: string | undefined;
let depsCommand = '';

if (dependencies) {
// Create a dummy package.json for dependencies that we need to install
fs.writeFileSync(
path.join(projectRoot, '.package.json'),
JSON.stringify({ dependencies }),
);

// Use npm unless we have a yarn.lock.
if (fs.existsSync(path.join(projectRoot, LockFile.YARN))) {
installer = Installer.YARN;
lockfile = LockFile.YARN;
} else if (fs.existsSync(path.join(projectRoot, LockFile.NPM))) {
lockfile = LockFile.NPM;
}

// Move dummy package.json and lock file then install
depsCommand = chain([
`mv ${cdk.AssetStaging.BUNDLING_INPUT_DIR}/.package.json ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/package.json`,
lockfile ? `cp ${cdk.AssetStaging.BUNDLING_INPUT_DIR}/${lockfile} ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/${lockfile}` : '',
`cd ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR} && ${installer} install`,
]);
// Local
let localBundler: cdk.ILocalBundling | undefined;
if (!options.forceDockerBundling) {
localBundler = new LocalBundler({
projectRoot,
relativeEntryPath,
parcelOptions: options,
dependencies,
installer,
lockFile,
});
}

// Docker
const dockerBundler = new DockerBundler({
relativeEntryPath,
parcelOptions: options,
dependencies,
installer,
lockFile,
});

return lambda.Code.fromAsset(projectRoot, {
assetHashType: cdk.AssetHashType.BUNDLE,
bundling: {
image,
command: ['bash', '-c', chain([parcelCommand, depsCommand])],
environment: options.parcelEnvironment,
volumes: options.cacheDir
? [{ containerPath: '/parcel-cache', hostPath: options.cacheDir }]
: [],
workingDirectory: path.dirname(containerEntryPath).replace(/\\/g, '/'), // Always use POSIX paths in the container
local: localBundler,
...dockerBundler.bundlingOptions,
},
});
}
}

interface BundlerProps {
relativeEntryPath: string;
parcelOptions: ParcelOptions;
dependencies?: { [key: string]: string };
installer: Installer;
lockFile?: LockFile;
}

interface LocalBundlerProps extends BundlerProps {
projectRoot: string;
}

/**
* Local Parcel bundler
*/
class LocalBundler implements cdk.ILocalBundling {
public static get runsLocally(): boolean {
if (LocalBundler._runsLocally !== undefined) {
return LocalBundler._runsLocally;
}
if (os.platform() === 'win32') { // TODO: add Windows support
return false;
}
try {
const parcel = spawnSync(require.resolve('parcel'), ['--version']);
LocalBundler._runsLocally = /^2/.test(parcel.stdout.toString().trim()); // Cache result to avoid unnecessary spawns
return LocalBundler._runsLocally;
} catch {
return false;
}
}

private static _runsLocally?: boolean;

constructor(private readonly props: LocalBundlerProps) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to pass all the options here? I recall tryBundle's second argument is options which should include all the needed context.

To verify we designed the protocol correctly, let's try to implement tryBundle through a simple "lambda-interface" instead of as a class:

local: {
  tryBundle: (outdir: string, options: ...) => {
    // foo bar
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will give it a shot but it seemed easier/cleaner to implement it with the bundler's option instead of parsing the Docker command. Might have implications to offer Windows support for local bundling also.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. Yeah I wouldn't parse the docker command...

You are right.

Let's leave as is. I am good


public tryBundle(outputDir: string) {
if (!LocalBundler.runsLocally) {
return false;
}

const localCommand = createBundlingCommand({
projectRoot: this.props.projectRoot,
relativeEntryPath: this.props.relativeEntryPath,
outputDir,
dependencies: this.props.dependencies,
installer: this.props.installer,
lockFile: this.props.lockFile,
parcelOptions: this.props.parcelOptions,
});

exec('bash', ['-c', localCommand], {
env: { ...process.env, ...this.props.parcelOptions.parcelEnvironment ?? {} },
stdio: [ // show output
'ignore', // ignore stdio
process.stderr, // redirect stdout to stderr
'inherit', // inherit stderr
],
});
return true;
}
}

/**
* Docker bundler
*/
class DockerBundler {
public readonly bundlingOptions: cdk.BundlingOptions;

constructor(props: BundlerProps) {
const image = LocalBundler.runsLocally && !props.parcelOptions.forceDockerBundling // Do not build an entire image if we run locally
? cdk.BundlingDockerImage.fromRegistry('dummy')
: cdk.BundlingDockerImage.fromAsset(path.join(__dirname, '../parcel'), {
buildArgs: {
...props.parcelOptions.buildArgs ?? {},
IMAGE: props.parcelOptions.runtime.bundlingDockerImage.image,
PARCEL_VERSION: props.parcelOptions.parcelVersion ?? '2.0.0-beta.1',
},
});

const command = createBundlingCommand({
projectRoot: cdk.AssetStaging.BUNDLING_INPUT_DIR, // project root is mounted at /asset-input
relativeEntryPath: props.relativeEntryPath,
outputDir: cdk.AssetStaging.BUNDLING_OUTPUT_DIR,
installer: props.installer,
lockFile: props.lockFile,
dependencies: props.dependencies,
parcelOptions: props.parcelOptions,
});

this.bundlingOptions = {
image,
command: ['bash', '-c', command],
environment: props.parcelOptions.parcelEnvironment,
workingDirectory: path.dirname(path.join(cdk.AssetStaging.BUNDLING_INPUT_DIR, props.relativeEntryPath)),
};
}
}

interface BundlingCommandOptions extends LocalBundlerProps {
outputDir: string;
}

/**
* Generates bundling command
*/
function createBundlingCommand(options: BundlingCommandOptions): string {
const entryPath = path.join(options.projectRoot, options.relativeEntryPath);
const distFile = path.basename(options.parcelOptions.entry).replace(/\.ts$/, '.js');
const parcelCommand: string = chain([
[
'$(node -p "require.resolve(\'parcel\')")', // Parcel is not globally installed, find its "bin"
'build', entryPath.replace(/\\/g, '/'), // Always use POSIX paths in the container
'--target', 'cdk-lambda',
'--dist-dir', options.outputDir, // Output bundle in outputDir (will have the same name as the entry)
'--no-autoinstall',
'--no-scope-hoist',
...options.parcelOptions.cacheDir
? ['--cache-dir', path.join(options.projectRoot, options.parcelOptions.cacheDir)]
: [],
].join(' '),
// Always rename dist file to index.js because Lambda doesn't support filenames
// with multiple dots and we can end up with multiple dots when using automatic
// entry lookup
`mv ${options.outputDir}/${distFile} ${options.outputDir}/index.js`,
]);

let depsCommand = '';
if (options.dependencies) {
// create dummy package.json, move lock file and then install
depsCommand = chain([
`echo '${JSON.stringify({ dependencies: options.dependencies })}' > ${options.outputDir}/package.json`,
options.lockFile ? `cp ${options.projectRoot}/${options.lockFile} ${options.outputDir}/${options.lockFile}` : '',
`cd ${options.outputDir}`,
`${options.installer} install`,
]);
}

return chain([parcelCommand, depsCommand]);
}

enum Installer {
NPM = 'npm',
YARN = 'yarn',
Expand Down
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawnSync, SpawnSyncOptions } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';

Expand Down Expand Up @@ -67,3 +68,23 @@ export function findUp(name: string, directory: string = process.cwd()): string

return findUp(name, path.dirname(absoluteDirectory));
}

/**
* Spawn sync with error handling
*/
export function exec(cmd: string, args: string[], options?: SpawnSyncOptions) {
const proc = spawnSync(cmd, args, options);

if (proc.error) {
throw proc.error;
}

if (proc.status !== 0) {
if (proc.stdout || proc.stderr) {
throw new Error(`[Status ${proc.status}] stdout: ${proc.stdout?.toString().trim()}\n\n\nstderr: ${proc.stderr?.toString().trim()}`);
}
throw new Error(`${cmd} exited with status ${proc.status}`);
}

return proc;
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"cdk-build-tools": "0.0.0",
"cdk-integ-tools": "0.0.0",
"delay": "4.3.0",
"parcel": "2.0.0-beta.1",
"pkglint": "0.0.0"
},
"dependencies": {
Expand Down
Loading