Skip to content

Commit 622b829

Browse files
authored
Merge pull request #2116 from brefphp/docs-laravel-environments
Document deploying multiple environments with Laravel
2 parents 87b441f + 62cb7a2 commit 622b829

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

docs/laravel/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"getting-started": "Getting started",
3+
"environments": "Environments",
34
"file-storage": "",
45
"queues": "Laravel Queues",
56
"octane": "Laravel Octane",

docs/laravel/environments.mdx

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { Callout, Tab, Tabs } from 'nextra/components';
2+
import { NextSeo } from 'next-seo';
3+
4+
<NextSeo description="Configure Laravel environments and deployment environments with Bref." />
5+
6+
# Laravel environments
7+
8+
Bref lets you deploy the same application multiple times in separate [environments](../deploy.mdx#environments), for example `dev`, `staging`, `prod`, or one environment per pull request.
9+
10+
The Bref Cloud CLI calls them **environments** (`bref deploy --env=prod`), while the `serverless` CLI calls them **stages** (`serverless deploy --stage=prod`). This is just a vocabulary difference.
11+
12+
Laravel also has an *application environment*, configured with the `APP_ENV` environment variable. These concepts are related, but they do not have to use the exact same name.
13+
14+
<Tabs items={['Bref Cloud', 'Serverless CLI']}>
15+
<Tab>
16+
```bash
17+
bref deploy --env=prod
18+
```
19+
</Tab>
20+
<Tab>
21+
```bash
22+
serverless deploy --stage=prod
23+
```
24+
</Tab>
25+
</Tabs>
26+
27+
In `serverless.yml`, the deployment environment is available as `${sls:stage}` for both CLIs:
28+
29+
```yml filename="serverless.yml"
30+
provider:
31+
environment:
32+
APP_ENV: ${param:appEnv}
33+
34+
params:
35+
default:
36+
appEnv: ${sls:stage}
37+
prod:
38+
appEnv: production
39+
```
40+
41+
In this example, most deployment environments use their name as `APP_ENV`, but the `prod` deployment environment is mapped to Laravel's `production` environment.
42+
43+
<Callout type="warning">
44+
Laravel treats `production` as the production environment. For example, `app()->isProduction()` returns `true` only when `APP_ENV=production`.
45+
46+
Using `prod` as a deployment environment name is common because it is short and appears in AWS resource names such as Lambda functions and CloudFormation stacks. If you use `prod`, map it to `APP_ENV=production` for Laravel.
47+
</Callout>
48+
49+
## Configure each environment
50+
51+
[Parameters](../environment/serverless-yml.mdx#stage-parameters) are a convenient way to configure values that change between deployment environments.
52+
53+
You can use them for Laravel environment variables:
54+
55+
```yml filename="serverless.yml"
56+
provider:
57+
environment:
58+
APP_ENV: ${param:appEnv}
59+
APP_DEBUG: ${param:debug}
60+
61+
DB_CONNECTION: mysql
62+
DB_HOST: ${param:databaseHost}
63+
DB_DATABASE: ${param:databaseName}
64+
DB_USERNAME: root
65+
DB_PASSWORD: ${ssm:/my-app/${sls:stage}/db-password}
66+
67+
params:
68+
default:
69+
appEnv: ${sls:stage}
70+
debug: 0
71+
databaseHost: shared-dev.cluster-abc123.eu-west-1.rds.amazonaws.com
72+
databaseName: my_app_dev
73+
74+
staging:
75+
databaseName: my_app_staging
76+
77+
prod:
78+
appEnv: production
79+
databaseHost: prod.cluster-def456.eu-west-1.rds.amazonaws.com
80+
databaseName: my_app_prod
81+
```
82+
83+
Parameters can also configure non-environment-variable values in `serverless.yml`, such as custom domains, certificates, VPC settings, or existing resource names:
84+
85+
```yml filename="serverless.yml"
86+
provider:
87+
vpc: ${param:vpc, ''}
88+
89+
constructs:
90+
website:
91+
type: server-side-website
92+
domain: ${param:domain, ''}
93+
certificate: ${param:certificate, ''}
94+
95+
params:
96+
default:
97+
domain: ''
98+
certificate: ''
99+
prod:
100+
domain: example.com
101+
certificate: arn:aws:acm:us-east-1:123456789012:certificate/...
102+
vpc:
103+
securityGroupIds:
104+
- sg-0123456789abcdef0
105+
subnetIds:
106+
- subnet-0123456789abcdef0
107+
- subnet-abcdef0123456789
108+
```
109+
110+
This keeps the configuration in one file while making the differences between environments explicit.
111+
112+
## Production isolation
113+
114+
It is possible, and often recommended, to deploy production in a separate AWS account from development and staging environments.
115+
116+
This gives production stronger isolation:
117+
118+
- production data and resources are separated from test environments
119+
- permissions can be stricter for production deployments
120+
- experimental environments are less likely to affect production quotas or resources
121+
122+
If you use Bref Cloud, you can connect multiple AWS accounts and deploy each environment to the right account without dealing with multiple credentials.
123+
124+
## Reusing resources
125+
126+
Each deployment environment creates its own Lambda functions and CloudFormation stack. However, not every environment needs its own database, VPC, NAT gateway, or other expensive infrastructure.
127+
128+
For staging, development, and preview environments, you can reuse shared resources by referencing them through parameters:
129+
130+
```yml filename="serverless.yml"
131+
params:
132+
default:
133+
databaseHost: shared-dev.cluster-abc123.eu-west-1.rds.amazonaws.com
134+
vpc:
135+
securityGroupIds:
136+
- sg-shareddev
137+
subnetIds:
138+
- subnet-shareddev-a
139+
- subnet-shareddev-b
140+
141+
prod:
142+
databaseHost: prod.cluster-def456.eu-west-1.rds.amazonaws.com
143+
vpc:
144+
securityGroupIds:
145+
- sg-production
146+
subnetIds:
147+
- subnet-production-a
148+
- subnet-production-b
149+
```
150+
151+
Preview environments can also share a development database cluster while using a different database name, schema, or prefix per environment.
152+
153+
This approach reduces AWS costs and makes deployments faster because Serverless does not need to create a full VPC and database for every temporary environment.

0 commit comments

Comments
 (0)