Skip to content

Commit

Permalink
Merge pull request #13 from lifeomic/synthetics-canary
Browse files Browse the repository at this point in the history
feat: new option artifactPrefix
  • Loading branch information
Shawn Zhu authored Jul 3, 2023
2 parents 78ef7e8 + 6e41141 commit e63bf8c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ bundle({
});
```

### Build for Synthetics Canary

When deploying a lambda as Synthetics Canary, it requires the packaged js file
saved under `nodejs/node_modules` within the packaged zip file. Please use the
option `artifactPrefix` to specify the directory structure within the packaged
zip file:

```bash
yarn blamda --entries src/my-service.ts --outdir build-output \
--artifact-prefix "nodejs/node_modules" --node 14
```

The output:

```
build-output/
my-service.zip # Upload this to Synthetics Canary!
my-service/ # Or, package + upload this directory yourself.
nodejs/
node_modules/
my-service.js
```

## Why Use This

As a pattern, bundling Lambda code provides a handful of benefits, especially in an
Expand Down
8 changes: 7 additions & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as yargs from 'yargs';
import { bundle } from '../bundle';

const main = async () => {
const { entries, outdir, node, includeAwsSdk } = await yargs(
const { entries, outdir, node, includeAwsSdk, artifactPrefix } = await yargs(
process.argv.slice(2),
)
.option('entries', {
Expand All @@ -27,6 +27,11 @@ const main = async () => {
description: 'Allow opting out from excluding the aws sdk',
default: false,
})
.option('artifact-prefix', {
type: 'string',
description: 'The artifact prefix within the built zip file',
default: '',
})
.strict()
.parse();

Expand All @@ -36,6 +41,7 @@ const main = async () => {
node,
cwd: process.cwd(),
includeAwsSdk,
artifactPrefix,
});
};

Expand Down
50 changes: 46 additions & 4 deletions src/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,48 @@ test('bundle works with an array of entries', async () => {
]);
});

test('bundle with artifactPrefix option', async () => {
writeTestInputFile(
'first-file.ts',
`
type DummyType = {
property: string
}
export const handler = (): DummyType => {
return { property: 'something' };
};
`,
);

const artifactPrefix = 'nodejs/node_modules';

await bundle({
entries: `${TEST_INPUT_DIR}/first-file.ts`,
outdir: TEST_OUTPUT_DIR,
artifactPrefix,
node: 12,
});

const outputFiles = readdirSync(TEST_OUTPUT_DIR);
expect(outputFiles).toEqual(['first-file', 'first-file.zip']);

expect(
readdirSync(`${TEST_OUTPUT_DIR}/first-file/${artifactPrefix}`),
).toEqual(['first-file.js']);

const zipEntries = new AdmZip(
`${TEST_OUTPUT_DIR}/first-file.zip`,
).getEntries();
console.log('zipEntries', zipEntries);
expect(zipEntries).toEqual(
expect.arrayContaining([
expect.objectContaining({
entryName: `${artifactPrefix}/first-file.js`,
}),
]),
);
});

describe('AWS SDK bundling behavior', () => {
test('allows for ignoring AWS SDK exclusion behavior', async () => {
writeTestInputFile('first-file.js', `export const TMP = 'TMP';`);
Expand Down Expand Up @@ -316,7 +358,7 @@ describe('AWS SDK bundling behavior', () => {
node,
code: `
import { Lambda } from 'aws-sdk';
export const handler = () => {
new Lambda();
return {};
Expand All @@ -338,7 +380,7 @@ describe('AWS SDK bundling behavior', () => {
node,
code: `
import { Lambda } from '@aws-sdk/client-lambda';
export const handler = () => {
new Lambda();
return {};
Expand All @@ -362,7 +404,7 @@ describe('AWS SDK bundling behavior', () => {
node: 18,
code: `
import { Lambda } from '@aws-sdk/client-lambda';
export const handler = () => {
new Lambda();
return {};
Expand All @@ -384,7 +426,7 @@ describe('AWS SDK bundling behavior', () => {
node: 18,
code: `
import { Lambda } from 'aws-sdk';
export const handler = () => {
new Lambda();
return {};
Expand Down
10 changes: 8 additions & 2 deletions src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export type BundleOptions = {
*/
includeAwsSdk?: boolean;

/**
* the output directories of build artifact.
*/
artifactPrefix?: string;

/**
* Override options to pass to esbuild. These override any options generated
* by other internal settings.
Expand All @@ -50,6 +55,7 @@ export const bundle = async ({
node: nodeVersion,
cwd,
includeAwsSdk = false,
artifactPrefix = '',
esbuild: { external = [], ...esbuild } = {},
}: BundleOptions) => {
const entryPoints = (typeof entries === 'string' ? [entries] : entries)
Expand Down Expand Up @@ -104,11 +110,11 @@ export const bundle = async ({

await Promise.all(
outFilenames.map(async (filename) => {
const dest = `${outdir}/${filename}`;
const dest = `${outdir}/${filename}/${artifactPrefix}`;
// Make a single directory for the artifacts
await fs.mkdir(dest, { recursive: true });
// Move the bundle into the directory
await fs.rename(`${dest}.js`, `${dest}/${filename}.js`);
await fs.rename(`${outdir}/${filename}.js`, `${dest}/${filename}.js`);
}),
);

Expand Down

0 comments on commit e63bf8c

Please sign in to comment.