|
| 1 | +# AWS CDK constructs for PHP applications on AWS Lambda |
| 2 | + |
| 3 | +The [Bref](https://bref.sh/) CDK constructs let you deploy serverless PHP applications on AWS Lambda using the AWS CDK. |
| 4 | + |
| 5 | +By default, Bref [deploys using the Serverless Framework](https://bref.sh/docs/deploy.html). Using the AWS CDK is an alternative, but be aware that this is an advanced topic. If you are lost, follow the [Bref documentation](https://bref.sh/docs/) instead. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Install the package with NPM: |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install @bref.sh/constructs |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +Simple example to deploy an HTTP application: |
| 18 | + |
| 19 | +```typescript |
| 20 | +import { Construct } from 'constructs'; |
| 21 | +import { App, Stack } from 'aws-cdk-lib'; |
| 22 | +import { PhpFpmFunction } from '@bref.sh/constructs'; |
| 23 | + |
| 24 | +class MyStack extends Stack { |
| 25 | + constructor(scope: Construct, id: string, props?: StackProps) { |
| 26 | + super(scope, id, props); |
| 27 | + |
| 28 | + new PhpFpmFunction(this, 'Hello', { |
| 29 | + handler: 'public/index.php', |
| 30 | + }); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +const app = new App(); |
| 35 | +new MyStack(app, 'test', { |
| 36 | + env: { |
| 37 | + region: 'eu-west-1', |
| 38 | + }, |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +## Constructs |
| 43 | + |
| 44 | +### Functions |
| 45 | + |
| 46 | +#### `PhpFpmFunction` |
| 47 | + |
| 48 | +This construct deploys a PHP function with the [HTTP runtime](https://bref.sh/docs/runtimes/http.html). |
| 49 | + |
| 50 | +```typescript |
| 51 | +new PhpFpmFunction(this, 'MyFunction', { |
| 52 | + handler: 'public/index.php', |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +It inherits from the AWS CDK [`Function` construct](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html) with these options set by default: |
| 57 | + |
| 58 | +- `handler`: `index.php` by default |
| 59 | +- `runtime`: `provided.al2` |
| 60 | +- `code`: the code is automatically zipped from the current directory. |
| 61 | +- `layers`: the Bref layer is automatically added. |
| 62 | +- `memorySize`: `1024` |
| 63 | +- `timeout`: `28` (seconds) |
| 64 | + |
| 65 | +The code is automatically zipped from the current directory. You can override this behavior by setting the `code` property: |
| 66 | + |
| 67 | +```typescript |
| 68 | +import { packagePhpCode } from '@bref.sh/constructs'; |
| 69 | + |
| 70 | +new PhpFpmFunction(this, 'MyFunction', { |
| 71 | + code: packagePhpCode('custom-path', { |
| 72 | + exclude: ['docs'], |
| 73 | + }), |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +The following paths are always excluded: `.git`, `.idea`, `cdk.out`, `node_modules`, `.bref`, `.serverless`, `tests`. |
| 78 | + |
| 79 | +The construct also adds the following options: |
| 80 | + |
| 81 | +- `phpVersion` (default: `8.1`): the PHP version to use. |
| 82 | + |
| 83 | +#### `PhpFunction` |
| 84 | + |
| 85 | +This construct deploys a PHP function with the ["event-driven function" runtime](https://bref.sh/docs/runtimes/function.html). |
| 86 | + |
| 87 | +```typescript |
| 88 | +new PhpFunction(this, 'MyFunction', { |
| 89 | + handler: 'my-handler.php', |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +It inherits from the AWS CDK [`Function` construct](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html) with these options set by default: |
| 94 | + |
| 95 | +- `runtime`: `provided.al2` |
| 96 | +- `code`: the code is automatically zipped from the current directory. |
| 97 | +- `layers`: the Bref layer is automatically added. |
| 98 | +- `memorySize`: `1024` |
| 99 | +- `timeout`: `6` (seconds) |
| 100 | + |
| 101 | +The code is automatically zipped from the current directory. You can override this behavior by setting the `code` property: |
| 102 | + |
| 103 | +```typescript |
| 104 | +import { packagePhpCode } from '@bref.sh/constructs'; |
| 105 | + |
| 106 | +new PhpFunction(this, 'MyFunction', { |
| 107 | + // ... |
| 108 | + code: packagePhpCode('custom-path', { |
| 109 | + exclude: ['docs'], |
| 110 | + }), |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +The following paths are always excluded: `.git`, `.idea`, `cdk.out`, `node_modules`, `.bref`, `.serverless`, `tests`. |
| 115 | + |
| 116 | +The construct also adds the following options: |
| 117 | + |
| 118 | +- `phpVersion` (default: `8.1`): the PHP version to use. |
| 119 | + |
| 120 | +#### `ConsoleFunction` |
| 121 | + |
| 122 | +This construct deploys a PHP function with the ["console" runtime](https://bref.sh/docs/runtimes/console.html). |
| 123 | + |
| 124 | +```typescript |
| 125 | +new ConsoleFunction(this, 'Artisan', { |
| 126 | + handler: 'artisan', |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +It inherits from the AWS CDK [`Function` construct](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html) with these options set by default: |
| 131 | + |
| 132 | +- `runtime`: `provided.al2` |
| 133 | +- `code`: the code is automatically zipped from the current directory. |
| 134 | +- `layers`: the Bref layers are automatically added. |
| 135 | +- `memorySize`: `1024` |
| 136 | +- `timeout`: `6` (seconds) |
| 137 | + |
| 138 | +The code is automatically zipped from the current directory. You can override this behavior by setting the `code` property: |
| 139 | + |
| 140 | +```typescript |
| 141 | +import { packagePhpCode } from '@bref.sh/constructs'; |
| 142 | + |
| 143 | +new ConsoleFunction(this, 'Artisan', { |
| 144 | + // ... |
| 145 | + code: packagePhpCode('custom-path', { |
| 146 | + exclude: ['docs'], |
| 147 | + }), |
| 148 | +}); |
| 149 | +``` |
| 150 | + |
| 151 | +The following paths are always excluded: `.git`, `.idea`, `cdk.out`, `node_modules`, `.bref`, `.serverless`, `tests`. |
| 152 | + |
| 153 | +The construct also adds the following options: |
| 154 | + |
| 155 | +- `phpVersion` (default: `8.1`): the PHP version to use. |
0 commit comments