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

fix(lambda-python): bundle asset files correctly #18335

Merged
merged 6 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-python/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Bundling implements CdkBundlingOptions {
if (packaging.dependenciesFile) {
bundlingCommands.push(`python -m pip install -r ${DependenciesFile.PIP} -t ${options.outputDir}`);
}
bundlingCommands.push(`cp -R ${options.inputDir}/ ${options.outputDir}`);
bundlingCommands.push(`cp -a ${options.inputDir}/* ${options.outputDir}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Including the * still drops hidden files. This needs to be removed.

Suggested change
bundlingCommands.push(`cp -a ${options.inputDir}/* ${options.outputDir}`);
bundlingCommands.push(`cp -a ${options.inputDir}/ ${options.outputDir}`);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Urm, I added a validation for it within the test, which passes.

return bundlingCommands;
}
}
Expand Down
46 changes: 34 additions & 12 deletions packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import * as path from 'path';
import { Architecture, Code, Runtime } from '@aws-cdk/aws-lambda';
import { DockerImage } from '@aws-cdk/core';
Expand Down Expand Up @@ -25,7 +26,7 @@ beforeEach(() => {

test('Bundling a function without dependencies', () => {
const entry = path.join(__dirname, 'lambda-handler-nodeps');
Bundling.bundle({
const assetCode = Bundling.bundle({
entry: entry,
runtime: Runtime.PYTHON_3_7,
architecture: Architecture.X86_64,
Expand All @@ -36,7 +37,7 @@ test('Bundling a function without dependencies', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'cp -R /asset-input/ /asset-output',
'cp -a /asset-input/* /asset-output',
],
}),
}));
Expand All @@ -47,11 +48,14 @@ test('Bundling a function without dependencies', () => {
}),
platform: 'linux/amd64',
}));

const files = fs.readdirSync(assetCode.path);
expect(files).toContain('index.py');
});

test('Bundling a function with requirements.txt', () => {
const entry = path.join(__dirname, 'lambda-handler');
Bundling.bundle({
const assetCode = Bundling.bundle({
entry: entry,
runtime: Runtime.PYTHON_3_7,
architecture: Architecture.X86_64,
Expand All @@ -62,10 +66,14 @@ test('Bundling a function with requirements.txt', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'python -m pip install -r requirements.txt -t /asset-output && cp -R /asset-input/ /asset-output',
'python -m pip install -r requirements.txt -t /asset-output && cp -a /asset-input/* /asset-output',
],
}),
}));

const files = fs.readdirSync(assetCode.path);
expect(files).toContain('index.py');
expect(files).toContain('requirements.txt');
});

test('Bundling Python 2.7 with requirements.txt installed', () => {
Expand All @@ -81,7 +89,7 @@ test('Bundling Python 2.7 with requirements.txt installed', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'python -m pip install -r requirements.txt -t /asset-output && cp -R /asset-input/ /asset-output',
'python -m pip install -r requirements.txt -t /asset-output && cp -a /asset-input/* /asset-output',
],
}),
}));
Expand All @@ -101,7 +109,7 @@ test('Bundling a layer with dependencies', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'python -m pip install -r requirements.txt -t /asset-output/python && cp -R /asset-input/ /asset-output/python',
'python -m pip install -r requirements.txt -t /asset-output/python && cp -a /asset-input/* /asset-output/python',
],
}),
}));
Expand All @@ -121,7 +129,7 @@ test('Bundling a python code layer', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'cp -R /asset-input/ /asset-output/python',
'cp -a /asset-input/* /asset-output/python',
],
}),
}));
Expand All @@ -130,7 +138,7 @@ test('Bundling a python code layer', () => {
test('Bundling a function with pipenv dependencies', () => {
const entry = path.join(__dirname, 'lambda-handler-pipenv');

Bundling.bundle({
const assetCode = Bundling.bundle({
entry: path.join(entry, '.'),
runtime: Runtime.PYTHON_3_9,
architecture: Architecture.X86_64,
Expand All @@ -141,16 +149,23 @@ test('Bundling a function with pipenv dependencies', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'PIPENV_VENV_IN_PROJECT=1 pipenv lock -r > requirements.txt && rm -rf .venv && python -m pip install -r requirements.txt -t /asset-output/python && cp -R /asset-input/ /asset-output/python',
'PIPENV_VENV_IN_PROJECT=1 pipenv lock -r > requirements.txt && rm -rf .venv && python -m pip install -r requirements.txt -t /asset-output/python && cp -a /asset-input/* /asset-output/python',
],
}),
}));

const files = fs.readdirSync(assetCode.path);
expect(files).toContain('index.py');
expect(files).toContain('Pipfile');
expect(files).toContain('Pipfile.lock');
// Contains hidden files.
expect(files).toContain('.gitignore');
});

test('Bundling a function with poetry dependencies', () => {
const entry = path.join(__dirname, 'lambda-handler-poetry');

Bundling.bundle({
const assetCode = Bundling.bundle({
entry: path.join(entry, '.'),
runtime: Runtime.PYTHON_3_9,
architecture: Architecture.X86_64,
Expand All @@ -161,10 +176,17 @@ test('Bundling a function with poetry dependencies', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'poetry export --with-credentials --format requirements.txt --output requirements.txt && python -m pip install -r requirements.txt -t /asset-output/python && cp -R /asset-input/ /asset-output/python',
'poetry export --with-credentials --format requirements.txt --output requirements.txt && python -m pip install -r requirements.txt -t /asset-output/python && cp -a /asset-input/* /asset-output/python',
],
}),
}));

const files = fs.readdirSync(assetCode.path);
expect(files).toContain('index.py');
expect(files).toContain('pyproject.toml');
expect(files).toContain('poetry.lock');
// Contains hidden files.
expect(files).toContain('.gitignore');
});

test('Bundling a function with custom bundling image', () => {
Expand All @@ -184,7 +206,7 @@ test('Bundling a function with custom bundling image', () => {
image,
command: [
'bash', '-c',
'python -m pip install -r requirements.txt -t /asset-output/python && cp -R /asset-input/ /asset-output/python',
'python -m pip install -r requirements.txt -t /asset-output/python && cp -a /asset-input/* /asset-output/python',
],
}),
}));
Expand Down