Skip to content

Commit

Permalink
Merge pull request #4 from fourTheorem/feat/support-extensionless-fun…
Browse files Browse the repository at this point in the history
…ction-index

feat(function): support extensionless function index
  • Loading branch information
eoinsha authored Oct 21, 2024
2 parents 3a25e51 + e78bc5f commit b7ffb01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export class PythonFunction extends Function {
const architecture = props.architecture ?? Architecture.ARM_64;
const rootDir = path.resolve(props.rootDir);

const resolvedHandler = `${index.slice(0, -3)}.${handler}`.replace(/\//g, '.');
// Strip .py from the end of handler if it exists
const strippedIndex = index.endsWith('.py') ? index.slice(0, -3) : index;

const resolvedHandler = `${strippedIndex}.${handler}`.replace(/\//g, '.');

if (runtime.family !== RuntimeFamily.PYTHON) {
throw new Error('Only Python runtimes are supported');
Expand Down
25 changes: 23 additions & 2 deletions test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ test('Create a function from basic_app', async () => {
});

const template = Template.fromStack(stack);
console.log(JSON.stringify(template.toJSON(), null, ' '));

template.hasResourceProperties('AWS::Lambda::Function', {
Handler: 'handler.lambda_handler',
Expand All @@ -55,6 +54,29 @@ test('Create a function from basic_app', async () => {
expect(contents).toContain('handler.py');
});

test('Create a function from basic_app with no .py index extension', async () => {
const app = new App({});
const stack = new Stack(app, 'test');
new PythonFunction(stack, 'basic_app', {
rootDir: path.join(resourcesPath, 'basic_app'),
index: 'handler',
handler: 'lambda_handler',
runtime: Runtime.PYTHON_3_12,
architecture: await getDockerHostArch(),
});

const template = Template.fromStack(stack);

template.hasResourceProperties('AWS::Lambda::Function', {
Handler: 'handler.lambda_handler',
Runtime: 'python3.12',
Code: {
S3Bucket: Match.anyValue(),
S3Key: Match.anyValue(),
},
});
});

test('Create a function with workspaces_app', async () => {
const app = new App({});
const stack = new Stack(app, 'wstest');
Expand All @@ -68,7 +90,6 @@ test('Create a function with workspaces_app', async () => {
});

const template = Template.fromStack(stack);
console.log(JSON.stringify(template.toJSON(), null, ' '));

template.hasResourceProperties('AWS::Lambda::Function', {
Handler: 'app_handler.handle_event',
Expand Down

0 comments on commit b7ffb01

Please sign in to comment.