import { Callout, Tab, Tabs } from 'nextra/components';
Bref is designed out of the box to deploy using the Serverless Framework.
Bref can also work with any other deployment tool: Terraform, CloudFormation, SAM, AWS CDK, Pulumi… However, the documentation and user experience is optimized for Serverless Framework.
To deploy to AWS an application configured with serverless.yml, run:
<Tabs items={['Serverless CLI', 'Bref Cloud']}>
bash serverless deploy
bash bref deploy
A .serverless/ directory will be created. You can add it to .gitignore.
In the previous step, we deployed the project installed on your machine. This is probably a development version.
For production, we usually don't want to deploy:
- dev dependencies
- dev configuration
- etc.
Instead, let's remove development dependencies and optimize Composer's autoloader for production:
composer install --prefer-dist --optimize-autoloader --no-devNow is also the best time to configure your project for production, as well as build any file cache if necessary.
Once your project is ready, you can deploy via the following command:
<Tabs items={['Serverless CLI', 'Bref Cloud']}>
bash serverless deploy
bash bref deploy
AWS Lambda has a 250MB size limit for deployed applications (unzipped). Large Composer dependencies can push your project over this limit and also lead to slower cold starts.
The most common offender is aws/aws-sdk-php: it ships clients for every AWS service, adding over 100MB to your vendor directory. If you only use a few services (e.g. S3, SQS, DynamoDB), you can remove the rest.
Add the following to your composer.json:
{
"scripts": {
"pre-autoload-dump": [
"Aws\\Script\\Composer\\Composer::removeUnusedServices"
]
},
"extra": {
"aws/aws-sdk-php": [
"Lambda",
"S3",
"Sqs",
"DynamoDb"
]
}
}Then run composer install (or composer update) to apply the changes. Adjust the list to keep only the AWS services your application actually uses. Note: S3, Kms, SSO, and Sts cannot be removed as they are required by the SDK core.
Read more in the aws/aws-sdk-php documentation.
Using google/apiclient? A similar approach is available: see the documentation to remove unused Google API services.
To further reduce deployment size, exclude non-essential files (tests, assets, node_modules) from the package. Read more in the serverless.yml exclusions documentation.
If your application still exceeds the 250MB limit, you can deploy via Docker images instead.
We can deploy the same application multiple times in completely separated environments (also called "stages" by the Serverless CLI).
<Tabs items={['Serverless CLI', 'Bref Cloud']}>
bash serverless deploy --stage=prod
```bash
bref deploy --env=prod
# or
bref deploy -e prod
```
</Tab>
The default environment is dev. The example above deploys a prod environment.
Each environment is a separate CloudFormation stack, with completely separate AWS resources (Lambda functions, logs, permissions, etc.). All AWS resources are prefixed with the service and environment name (for example myapp-dev-api), which avoids any collision between environments.
It is possible to deploy different environments in different AWS accounts (to lock down permissions), and to deploy one environment per git branch, pull request, or even developer in the team.
If you are using Bref Cloud, you can easily set up automatic deployments from CI/CD tools.
Read the documentation on deploying with Bref Cloud for more information.
If you are using GitHub Actions, Gitlab CI, CircleCI, or any tool of the sort you will want to automate the deployment to something like this:
# Install Composer dependencies optimized for production
composer install --prefer-dist --optimize-autoloader --no-dev
# Perform extra tasks for your framework of choice
# (e.g. generate the framework cache)
# [...]
# Deploy
serverless deployThat will also mean creating AWS access keys so that the continuous integration is allowed to deploy.
You can find configuration examples for CI/CD tools in the Bref examples repository.
AWS runs applications in different regions. The default region is us-east-1 (North Virginia, USA).
If you want to use a different region (for example to host your application closer to your visitors) you can configure it in your serverless.yml:
provider:
region: eu-west-1 # Ireland, Europe
...You can delete a deployed environment using the remove command.
<Tabs items={['Serverless CLI', 'Bref Cloud']}> ```bash serverless remove
# or remove a specific environment
serverless remove --stage=prod
```
Note that because of the way Serverless Framework works, you will need to delete the contents of AWS S3 buckets manually before running this command.
</Tab>
<Tab>
```bash
bref remove
# or remove a specific environment
bref remove --env=prod
```
Bref Cloud will automatically delete the contents of AWS S3 buckets.
</Tab>
Deleting an environment destroys the AWS resources that were created for that environment.
If you want to delete all environments of an application, you can do so in the Bref Cloud dashboard. If you don't use Bref Cloud, you will need to delete each environment one by one.
Under the hood, Bref will deploy everything to AWS as a CloudFormation stack. A "stack" is nothing more than a bunch of things that compose an application:
- Lambda functions
- HTTP endpoints
- S3 buckets
- databases
- etc.
Stacks make it easy to group those resources together: the whole stack is updated at once on deployments, and if you delete the stack all the resources inside are deleted together too. Clean and simple.
CloudFormation deploys using the blue/green deployment strategy.
This means that when you deploy, a new version of your code is deployed alongside the old one. Once the new version is ready, the traffic switches to the new version. If the deployment fails at any point, the traffic stays on the old version and the deployment is rolled back.
As soon as you introduce asynchronous behaviors (e.g. background jobs with SQS, event-driven microservices…) you may have in-flight messages (SQS jobs, EventBridge events…) created by the old version of your code that will be processed by the new version of your code.
Code that handles asynchronous events must be able to handle messages created by older versions of the code.
Zero-downtime deployments mean that database migrations must run when code is running in production. That means either before or after the deployment (traffic switch) happens, and having a DB migration strategy compatible with that.
Serverless Framework offers a simple configuration format. This is what you are using if you use Bref. That configuration is written in your project in a serverless.yml file.
You can learn more about that configuration format here.
Read more about serverless deploy in the official documentation.