Skip to content

Commit 4228c82

Browse files
authored
Merge pull request #3 from brefphp/arm
2 parents 294e7b8 + 5ac7c7d commit 4228c82

File tree

7 files changed

+69
-6
lines changed

7 files changed

+69
-6
lines changed

src/function/PhpFpmFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class PhpFpmFunction extends Function {
4343
const phpVersion = props.phpVersion ?? functionDefaults.phpVersion;
4444
this.addLayers(
4545
// Add the FPM layer first so that other layers can override it
46-
fpmLayer(this, region, phpVersion, functionDefaults.platform),
46+
fpmLayer(this, region, phpVersion, props.architecture ?? functionDefaults.architecture),
4747
...layers
4848
);
4949
}

src/function/PhpFunction.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ export class PhpFunction extends Function {
4444
const phpVersion = props.phpVersion ?? functionDefaults.phpVersion;
4545
this.addLayers(
4646
// Add the function layer first so that other layers can override it
47-
functionLayer(this, region, phpVersion, functionDefaults.platform),
47+
functionLayer(
48+
this,
49+
region,
50+
phpVersion,
51+
props.architecture ?? functionDefaults.architecture
52+
),
4853
...layers
4954
);
5055
}

src/function/defaults.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { IVpc, SecurityGroup, SubnetSelection, SubnetType } from 'aws-cdk-lib/aws-ec2';
22
import { VpcForServerlessApp } from '../vpc/VpcForServerlessApp';
3+
import { Architecture } from 'aws-cdk-lib/aws-lambda';
34

45
export const functionDefaults = {
56
path: process.cwd(),
67
phpVersion: '8.1',
78
memorySize: 1024,
8-
platform: 'x86',
9+
architecture: Architecture.X86_64,
910
excludedPhpPaths: ['.git', '.idea', 'cdk.out', 'node_modules', '.bref', '.serverless', 'tests'],
1011
} as const;
1112

src/layers.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { ILayerVersion, LayerVersion } from 'aws-cdk-lib/aws-lambda';
1+
import { Architecture, ILayerVersion, LayerVersion } from 'aws-cdk-lib/aws-lambda';
22
import { Construct } from 'constructs';
33
import { functionLayerArn, fpmLayerArn, consoleLayerArn } from '@bref.sh/layers';
44

55
export function functionLayer(
66
scope: Construct,
77
region: string,
88
phpVersion: string,
9-
platform: 'x86' | 'arm'
9+
architecture: Architecture
1010
): ILayerVersion {
11+
const platform = architecture === Architecture.X86_64 ? 'x86' : 'arm';
12+
1113
return LayerVersion.fromLayerVersionArn(
1214
scope,
1315
'BrefFunctionLayer',
@@ -19,8 +21,10 @@ export function fpmLayer(
1921
scope: Construct,
2022
region: string,
2123
phpVersion: string,
22-
platform: 'x86' | 'arm'
24+
architecture: Architecture
2325
): ILayerVersion {
26+
const platform = architecture === Architecture.X86_64 ? 'x86' : 'arm';
27+
2428
return LayerVersion.fromLayerVersionArn(
2529
scope,
2630
'BrefFpmLayer',

test/function/ConsoleFunction.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, expect, it } from 'vitest';
22
import { ConsoleFunction } from '../../src';
33
import { compileTestStack } from '../helper';
4+
import { Architecture } from 'aws-cdk-lib/aws-lambda';
5+
import { mapValues } from 'lodash';
46

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

22+
it('supports ARM', () => {
23+
const template = compileTestStack((stack) => {
24+
new ConsoleFunction(stack, 'Console', {
25+
handler: 'index.php',
26+
architecture: Architecture.ARM_64,
27+
});
28+
});
29+
30+
mapValues(template.findResources('AWS::Lambda::Function'), (resource) => {
31+
expect(resource.Properties.Architectures).toEqual(['arm64']);
32+
expect(resource.Properties.Layers[0]).matches(
33+
/arn:aws:lambda:us-east-1:534081306603:layer:arm-php-81:\d+/
34+
);
35+
});
36+
});
37+
2038
// https://github.com/brefphp/constructs/issues/1
2139
it('can build multiple functions in the same stack', () => {
2240
const template = compileTestStack((stack) => {

test/function/PhpFpmFunction.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, expect, it } from 'vitest';
22
import { PhpFpmFunction } from '../../src';
33
import { cleanupTemplate, compileTestStack } from '../helper';
4+
import { Architecture } from 'aws-cdk-lib/aws-lambda';
5+
import { mapValues } from 'lodash';
46

57
describe('PhpFpmFunction', () => {
68
it('builds', () => {
@@ -11,6 +13,21 @@ describe('PhpFpmFunction', () => {
1113
expect(cleanupTemplate(template).Resources).toMatchSnapshot();
1214
});
1315

16+
it('supports ARM', () => {
17+
const template = compileTestStack((stack) => {
18+
new PhpFpmFunction(stack, 'Function', {
19+
architecture: Architecture.ARM_64,
20+
});
21+
});
22+
23+
mapValues(template.findResources('AWS::Lambda::Function'), (resource) => {
24+
expect(resource.Properties.Architectures).toEqual(['arm64']);
25+
expect(resource.Properties.Layers[0]).matches(
26+
/arn:aws:lambda:us-east-1:534081306603:layer:arm-php-81-fpm:\d+/
27+
);
28+
});
29+
});
30+
1431
// https://github.com/brefphp/constructs/issues/1
1532
it('can build multiple functions in the same stack', () => {
1633
const template = compileTestStack((stack) => {

test/function/PhpFunction.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, expect, it } from 'vitest';
22
import { PhpFunction } from '../../src';
33
import { cleanupTemplate, compileTestStack } from '../helper';
4+
import { Architecture } from 'aws-cdk-lib/aws-lambda';
5+
import { mapValues } from 'lodash';
46

57
describe('PhpFunction', () => {
68
it('builds', () => {
@@ -13,6 +15,22 @@ describe('PhpFunction', () => {
1315
expect(cleanupTemplate(template).Resources).toMatchSnapshot();
1416
});
1517

18+
it('supports ARM', () => {
19+
const template = compileTestStack((stack) => {
20+
new PhpFunction(stack, 'Function', {
21+
handler: 'index.php',
22+
architecture: Architecture.ARM_64,
23+
});
24+
});
25+
26+
mapValues(template.findResources('AWS::Lambda::Function'), (resource) => {
27+
expect(resource.Properties.Architectures).toEqual(['arm64']);
28+
expect(resource.Properties.Layers[0]).matches(
29+
/arn:aws:lambda:us-east-1:534081306603:layer:arm-php-81:\d+/
30+
);
31+
});
32+
});
33+
1634
// https://github.com/brefphp/constructs/issues/1
1735
it('can build multiple functions in the same stack', () => {
1836
const template = compileTestStack((stack) => {

0 commit comments

Comments
 (0)