Skip to content

Commit dcdc0ae

Browse files
committed
Add function and console constructs
1 parent 3686590 commit dcdc0ae

8 files changed

Lines changed: 99 additions & 11 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bref.sh/constructs",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"main": "dist/index.js",
55
"scripts": {
66
"build": "tsc",

src/function/ConsoleFunction.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Stack } from 'aws-cdk-lib';
2+
import { Construct } from 'constructs';
3+
import { consoleLayer } from '../layers';
4+
import { PhpFunction, PhpFunctionProps } from './PhpFunction';
5+
6+
export class ConsoleFunction extends PhpFunction {
7+
constructor(scope: Construct, id: string, props: PhpFunctionProps) {
8+
const layers = props.layers ?? [];
9+
layers.unshift(consoleLayer(scope, Stack.of(scope).region));
10+
super(scope, id, {
11+
...props,
12+
layers,
13+
});
14+
}
15+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { Function, FunctionProps, Runtime } from 'aws-cdk-lib/aws-lambda';
22
import { Duration, Stack } from 'aws-cdk-lib';
33
import { Construct } from 'constructs';
44
import { functionDefaults } from './defaults';
5-
import { fpmLayer } from './layers';
6-
import { packagePhpCode } from './package';
5+
import { fpmLayer } from '../layers';
6+
import { packagePhpCode } from '../package';
77

8-
export type FpmFunctionProps = Partial<FunctionProps> & {
8+
export type PhpFpmFunctionProps = Partial<FunctionProps> & {
99
phpVersion?: '8.0' | '8.1' | '8.2';
1010
};
1111

12-
export class FpmFunction extends Function {
13-
constructor(scope: Construct, id: string, props: FpmFunctionProps = {}) {
12+
export class PhpFpmFunction extends Function {
13+
constructor(scope: Construct, id: string, props: PhpFpmFunctionProps = {}) {
1414
const defaults = {
1515
description: 'HTTP application',
1616
runtime: Runtime.PROVIDED_AL2,

src/function/PhpFunction.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Function, FunctionProps, Runtime } from 'aws-cdk-lib/aws-lambda';
2+
import { Duration, Stack } from 'aws-cdk-lib';
3+
import { Construct } from 'constructs';
4+
import { functionDefaults } from './defaults';
5+
import { functionLayer } from '../layers';
6+
import { packagePhpCode } from '../package';
7+
8+
export type PhpFunctionProps = Partial<FunctionProps> & {
9+
phpVersion?: '8.0' | '8.1' | '8.2';
10+
handler: string;
11+
};
12+
13+
export class PhpFunction extends Function {
14+
constructor(scope: Construct, id: string, props: PhpFunctionProps) {
15+
const defaults = {
16+
runtime: Runtime.PROVIDED_AL2,
17+
code: props.code ?? packagePhpCode(),
18+
timeout: Duration.seconds(6),
19+
memorySize: functionDefaults.memorySize,
20+
};
21+
22+
// Put the layers aside for now, we set them after `super()`
23+
const layers = props.layers ?? [];
24+
25+
super(scope, id, {
26+
...defaults,
27+
// Provided props override defaults
28+
...props,
29+
// But we force the layers to an empty array because we define them below
30+
layers: [],
31+
});
32+
33+
// Add layer afterwards so that we can use `this` to resolve the region
34+
const region = Stack.of(this).region;
35+
if (region.startsWith('${')) {
36+
throw new Error(
37+
'Cannot detect the AWS region. Bref needs the AWS region to know which AWS layer to use. In order to use Bref layers, you must explicitly set the region for the stack. See https://gist.github.com/mnapoli/7c7eab49444637426ce66a24df715a63'
38+
);
39+
}
40+
const phpVersion = props.phpVersion ?? functionDefaults.phpVersion;
41+
this.addLayers(
42+
// Add the function layer first so that other layers can override it
43+
functionLayer(scope, region, phpVersion, functionDefaults.platform),
44+
...layers
45+
);
46+
}
47+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ export const functionDefaults = {
33
phpVersion: '8.1',
44
memorySize: 1024,
55
platform: 'x86',
6-
excludedPhpPaths: ['.git', 'cdk.out', 'node_modules', '.bref', 'tests'],
6+
excludedPhpPaths: ['.git', '.idea', 'cdk.out', 'node_modules', '.bref', '.serverless', 'tests'],
77
} as const;

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { FpmFunction, FpmFunctionProps } from './FpmFunction';
1+
import { ConsoleFunction } from './function/ConsoleFunction';
2+
import { PhpFpmFunction, PhpFpmFunctionProps } from './function/PhpFpmFunction';
3+
import { PhpFunction, PhpFunctionProps } from './function/PhpFunction';
24
import { packagePhpCode } from './package';
35

4-
export { FpmFunction, FpmFunctionProps, packagePhpCode };
6+
export {
7+
PhpFunction,
8+
PhpFunctionProps,
9+
PhpFpmFunction,
10+
PhpFpmFunctionProps,
11+
ConsoleFunction,
12+
packagePhpCode,
13+
};

src/layers.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import { ILayerVersion, LayerVersion } from 'aws-cdk-lib/aws-lambda';
22
import { Construct } from 'constructs';
3-
import { fpmLayerArn } from '@bref.sh/layers';
3+
import { functionLayerArn, fpmLayerArn, consoleLayerArn } from '@bref.sh/layers';
4+
5+
export function functionLayer(
6+
scope: Construct,
7+
region: string,
8+
phpVersion: string,
9+
platform: 'x86' | 'arm'
10+
): ILayerVersion {
11+
return LayerVersion.fromLayerVersionArn(
12+
scope,
13+
'BrefFunctionLayer',
14+
functionLayerArn(region, phpVersion, platform)
15+
);
16+
}
417

518
export function fpmLayer(
619
scope: Construct,
@@ -14,3 +27,7 @@ export function fpmLayer(
1427
fpmLayerArn(region, phpVersion, platform)
1528
);
1629
}
30+
31+
export function consoleLayer(scope: Construct, region: string): ILayerVersion {
32+
return LayerVersion.fromLayerVersionArn(scope, 'BrefConsoleLayer', consoleLayerArn(region));
33+
}

src/package.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AssetOptions } from 'aws-cdk-lib/aws-s3-assets';
22
import { Code } from 'aws-cdk-lib/aws-lambda';
3-
import { functionDefaults } from './defaults';
3+
import { functionDefaults } from './function/defaults';
44

55
/**
66
* @param path Defaults to the current working directory.

0 commit comments

Comments
 (0)