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

feat (aws-lambda): Write multiasset function to select diffrent folders as lambda asset #21644

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as iam from '@aws-cdk/aws-iam';
import * as s3 from '@aws-cdk/aws-s3';
import * as s3_assets from '@aws-cdk/aws-s3-assets';
import * as cdk from '@aws-cdk/core';
import { zipDirectories } from './private/archive';
import { Construct } from 'constructs';

/**
Expand Down Expand Up @@ -54,6 +55,18 @@ export abstract class Code {
return new AssetCode(path, options);
}

/**
* Loads the function code from local disk paths.
*
* @param paths an array of directories with the Lambda code bundle
*/
public static async fromMultiAsset(paths: string[], options?: s3_assets.AssetOptions): Promise<AssetCode> {
//create a zip file from paths in paths[]
const outputFileAddress = './cdk.out/directories.zip';
await zipDirectories(paths, outputFileAddress);
return new AssetCode(outputFileAddress, options);
}

/**
* Loads the function code from an asset created by a Docker build.
*
Expand Down
48 changes: 48 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/private/archive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createWriteStream, promises as fs } from 'fs';
import * as path from 'path';
import * as glob from 'glob';

// namespace object imports won't work in the bundle for function exports
// eslint-disable-next-line @typescript-eslint/no-require-imports
const archiver = require('archiver');

export function zipDirectories(directories: string[], outputFile: string): Promise<void> {
return new Promise(async (ok, fail) => {

const output = createWriteStream(outputFile);

const archive = archiver('zip');
archive.on('warning', fail);
archive.on('error', fail);

// archive has been finalized and the output file descriptor has closed, resolve promise
// this has to be done before calling `finalize` since the events may fire immediately after.
// see https://www.npmjs.com/package/archiver
output.once('close', ok);

archive.pipe(output);
for (const directory of directories) {
// The below options are needed to support following symlinks when building zip files:
// - nodir: This will prevent symlinks themselves from being copied into the zip.
// - follow: This will follow symlinks and copy the files within.
const globOptions = {
dot: true,
nodir: true,
follow: true,
cwd: directory,
};
const files = glob.sync('**', globOptions); // The output here is already sorted
// Append files serially to ensure file order
for (const file of files) {
const fullPath = path.resolve(directory, file);
const [data, stat] = await Promise.all([fs.readFile(fullPath), fs.stat(fullPath)]);
archive.append(data, {
name: file,
date: new Date('1980-01-01T00:00:00.000Z'), // reset dates to get the same hash for the same content
mode: stat.mode,
});
}
}
await archive.finalize();
});
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"@aws-cdk/region-info": "0.0.0",
"@types/archiver": "^5.3.1",
"constructs": "^10.0.0"
},
"homepage": "https://github.com/aws/aws-cdk",
Expand Down