diff --git a/src/function/ConsoleFunction.ts b/src/function/ConsoleFunction.ts index 6fd1ee3..139393e 100644 --- a/src/function/ConsoleFunction.ts +++ b/src/function/ConsoleFunction.ts @@ -5,11 +5,8 @@ import { PhpFunction, PhpFunctionProps } from './PhpFunction'; export class ConsoleFunction extends PhpFunction { constructor(scope: Construct, id: string, props: PhpFunctionProps) { - const layers = props.layers ?? []; - layers.unshift(consoleLayer(scope, Stack.of(scope).region)); - super(scope, id, { - ...props, - layers, - }); + super(scope, id, props); + + this.addLayers(consoleLayer(this, Stack.of(scope).region)); } } diff --git a/src/function/PhpFpmFunction.ts b/src/function/PhpFpmFunction.ts index 8816b23..40e412d 100644 --- a/src/function/PhpFpmFunction.ts +++ b/src/function/PhpFpmFunction.ts @@ -33,7 +33,7 @@ export class PhpFpmFunction extends Function { layers: [], }); - // Add layer afterwards so that we can use `this` to resolve the region + // Add layer afterwards so that we can use `this` const region = Stack.of(this).region; if (region.startsWith('${')) { throw new Error( @@ -43,7 +43,7 @@ export class PhpFpmFunction extends Function { const phpVersion = props.phpVersion ?? functionDefaults.phpVersion; this.addLayers( // Add the FPM layer first so that other layers can override it - fpmLayer(scope, region, phpVersion, functionDefaults.platform), + fpmLayer(this, region, phpVersion, functionDefaults.platform), ...layers ); } diff --git a/src/function/PhpFunction.ts b/src/function/PhpFunction.ts index 672f674..f13f9ba 100644 --- a/src/function/PhpFunction.ts +++ b/src/function/PhpFunction.ts @@ -34,7 +34,7 @@ export class PhpFunction extends Function { layers: [], }); - // Add layer afterwards so that we can use `this` to resolve the region + // Add layer afterwards so that we can use `this` const region = Stack.of(this).region; if (region.startsWith('${')) { throw new Error( @@ -44,7 +44,7 @@ export class PhpFunction extends Function { const phpVersion = props.phpVersion ?? functionDefaults.phpVersion; this.addLayers( // Add the function layer first so that other layers can override it - functionLayer(scope, region, phpVersion, functionDefaults.platform), + functionLayer(this, region, phpVersion, functionDefaults.platform), ...layers ); } diff --git a/test/function/ConsoleFunction.test.ts b/test/function/ConsoleFunction.test.ts index 6f4dc77..dcdcb8c 100644 --- a/test/function/ConsoleFunction.test.ts +++ b/test/function/ConsoleFunction.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { ConsoleFunction } from '../../src/function/ConsoleFunction'; +import { ConsoleFunction } from '../../src'; import { compileTestStack } from '../helper'; describe('ConsoleFunction', () => { @@ -16,4 +16,18 @@ describe('ConsoleFunction', () => { expect(layers[0]).to.match(/arn:aws:lambda:us-east-1:534081306603:layer:php-81:\d+/); expect(layers[1]).to.match(/arn:aws:lambda:us-east-1:534081306603:layer:console:\d+/); }); + + // https://github.com/brefphp/constructs/issues/1 + it('can build multiple functions in the same stack', () => { + const template = compileTestStack((stack) => { + new ConsoleFunction(stack, 'Function1', { + handler: 'index.php', + }); + new ConsoleFunction(stack, 'Function2', { + handler: 'index.php', + }); + }); + + template.resourceCountIs('AWS::Lambda::Function', 2); + }); }); diff --git a/test/function/PhpFpmFunction.test.ts b/test/function/PhpFpmFunction.test.ts index c0f8525..187f9b4 100644 --- a/test/function/PhpFpmFunction.test.ts +++ b/test/function/PhpFpmFunction.test.ts @@ -5,11 +5,19 @@ import { cleanupTemplate, compileTestStack } from '../helper'; describe('PhpFpmFunction', () => { it('builds', () => { const template = compileTestStack((stack) => { - new PhpFpmFunction(stack, 'Function', { - handler: 'index.php', - }); + new PhpFpmFunction(stack, 'Function'); }).toJSON(); expect(cleanupTemplate(template).Resources).toMatchSnapshot(); }); + + // https://github.com/brefphp/constructs/issues/1 + it('can build multiple functions in the same stack', () => { + const template = compileTestStack((stack) => { + new PhpFpmFunction(stack, 'Function1'); + new PhpFpmFunction(stack, 'Function2'); + }); + + template.resourceCountIs('AWS::Lambda::Function', 2); + }); }); diff --git a/test/function/PhpFunction.test.ts b/test/function/PhpFunction.test.ts index 4be118f..f5d39ff 100644 --- a/test/function/PhpFunction.test.ts +++ b/test/function/PhpFunction.test.ts @@ -12,4 +12,18 @@ describe('PhpFunction', () => { expect(cleanupTemplate(template).Resources).toMatchSnapshot(); }); + + // https://github.com/brefphp/constructs/issues/1 + it('can build multiple functions in the same stack', () => { + const template = compileTestStack((stack) => { + new PhpFunction(stack, 'Function1', { + handler: 'index.php', + }); + new PhpFunction(stack, 'Function2', { + handler: 'index.php', + }); + }); + + template.resourceCountIs('AWS::Lambda::Function', 2); + }); });