Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/function/PhpFpmFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(this, region, phpVersion, functionDefaults.platform),
fpmLayer(this, region, phpVersion, props.architecture ?? functionDefaults.architecture),
...layers
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/function/PhpFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ 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(this, region, phpVersion, functionDefaults.platform),
functionLayer(
this,
region,
phpVersion,
props.architecture ?? functionDefaults.architecture
),
...layers
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/function/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { IVpc, SecurityGroup, SubnetSelection, SubnetType } from 'aws-cdk-lib/aws-ec2';
import { VpcForServerlessApp } from '../vpc/VpcForServerlessApp';
import { Architecture } from 'aws-cdk-lib/aws-lambda';

export const functionDefaults = {
path: process.cwd(),
phpVersion: '8.1',
memorySize: 1024,
platform: 'x86',
architecture: Architecture.X86_64,
excludedPhpPaths: ['.git', '.idea', 'cdk.out', 'node_modules', '.bref', '.serverless', 'tests'],
} as const;

Expand Down
10 changes: 7 additions & 3 deletions src/layers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { ILayerVersion, LayerVersion } from 'aws-cdk-lib/aws-lambda';
import { Architecture, ILayerVersion, LayerVersion } from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
import { functionLayerArn, fpmLayerArn, consoleLayerArn } from '@bref.sh/layers';

export function functionLayer(
scope: Construct,
region: string,
phpVersion: string,
platform: 'x86' | 'arm'
architecture: Architecture
): ILayerVersion {
const platform = architecture === Architecture.X86_64 ? 'x86' : 'arm';

return LayerVersion.fromLayerVersionArn(
scope,
'BrefFunctionLayer',
Expand All @@ -19,8 +21,10 @@ export function fpmLayer(
scope: Construct,
region: string,
phpVersion: string,
platform: 'x86' | 'arm'
architecture: Architecture
): ILayerVersion {
const platform = architecture === Architecture.X86_64 ? 'x86' : 'arm';

return LayerVersion.fromLayerVersionArn(
scope,
'BrefFpmLayer',
Expand Down
18 changes: 18 additions & 0 deletions test/function/ConsoleFunction.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, expect, it } from 'vitest';
import { ConsoleFunction } from '../../src';
import { compileTestStack } from '../helper';
import { Architecture } from 'aws-cdk-lib/aws-lambda';
import { mapValues } from 'lodash';

describe('ConsoleFunction', () => {
it('adds the console layer', () => {
Expand All @@ -17,6 +19,22 @@ describe('ConsoleFunction', () => {
expect(layers[1]).to.match(/arn:aws:lambda:us-east-1:534081306603:layer:console:\d+/);
});

it('supports ARM', () => {
const template = compileTestStack((stack) => {
new ConsoleFunction(stack, 'Console', {
handler: 'index.php',
architecture: Architecture.ARM_64,
});
});

mapValues(template.findResources('AWS::Lambda::Function'), (resource) => {
expect(resource.Properties.Architectures).toEqual(['arm64']);
expect(resource.Properties.Layers[0]).matches(
/arn:aws:lambda:us-east-1:534081306603:layer:arm-php-81:\d+/
);
});
});

// https://github.com/brefphp/constructs/issues/1
it('can build multiple functions in the same stack', () => {
const template = compileTestStack((stack) => {
Expand Down
17 changes: 17 additions & 0 deletions test/function/PhpFpmFunction.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, expect, it } from 'vitest';
import { PhpFpmFunction } from '../../src';
import { cleanupTemplate, compileTestStack } from '../helper';
import { Architecture } from 'aws-cdk-lib/aws-lambda';
import { mapValues } from 'lodash';

describe('PhpFpmFunction', () => {
it('builds', () => {
Expand All @@ -11,6 +13,21 @@ describe('PhpFpmFunction', () => {
expect(cleanupTemplate(template).Resources).toMatchSnapshot();
});

it('supports ARM', () => {
const template = compileTestStack((stack) => {
new PhpFpmFunction(stack, 'Function', {
architecture: Architecture.ARM_64,
});
});

mapValues(template.findResources('AWS::Lambda::Function'), (resource) => {
expect(resource.Properties.Architectures).toEqual(['arm64']);
expect(resource.Properties.Layers[0]).matches(
/arn:aws:lambda:us-east-1:534081306603:layer:arm-php-81-fpm:\d+/
);
});
});

// https://github.com/brefphp/constructs/issues/1
it('can build multiple functions in the same stack', () => {
const template = compileTestStack((stack) => {
Expand Down
18 changes: 18 additions & 0 deletions test/function/PhpFunction.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, expect, it } from 'vitest';
import { PhpFunction } from '../../src';
import { cleanupTemplate, compileTestStack } from '../helper';
import { Architecture } from 'aws-cdk-lib/aws-lambda';
import { mapValues } from 'lodash';

describe('PhpFunction', () => {
it('builds', () => {
Expand All @@ -13,6 +15,22 @@ describe('PhpFunction', () => {
expect(cleanupTemplate(template).Resources).toMatchSnapshot();
});

it('supports ARM', () => {
const template = compileTestStack((stack) => {
new PhpFunction(stack, 'Function', {
handler: 'index.php',
architecture: Architecture.ARM_64,
});
});

mapValues(template.findResources('AWS::Lambda::Function'), (resource) => {
expect(resource.Properties.Architectures).toEqual(['arm64']);
expect(resource.Properties.Layers[0]).matches(
/arn:aws:lambda:us-east-1:534081306603:layer:arm-php-81:\d+/
);
});
});

// https://github.com/brefphp/constructs/issues/1
it('can build multiple functions in the same stack', () => {
const template = compileTestStack((stack) => {
Expand Down