Symfony on a lambda: conclusion
Tags: php, symfony, lambda, serverless
Published: 2020-04-10 07:16:17

---
title: Symfony on a lambda: conclusion
published: true
description: Conclusion of our serie of articles
tags: PHP, Symfony, Lambda, Serverless
series: Symfony on a lambda
---

#Symfony on a lambda: Conclusion

If you are lost, you can find the project on Github: [link](https://github.com/hyoa/symfony-on-lambda)
Each branch will match a chapter.

### Performance

Okay, if you did some tests you may have notice that the first hit is really slow. That's because we create the cache at each cold start. Not great (not great at all). We can do some update to reduce the time required for the cold start.

First, need to remove the modification we made in **src/Kernel.php** :

```php
public function getCacheDir(): string
{
  	if (getenv('LAMBDA_TASK_ROOT') !== false) {
    		return '/tmp/cache/'.$this->environment;
  	}

  	return parent::getCacheDir();
}
```

Should become :

```php
public function getCacheDir(): string
{
  	return parent::getCacheDir();
}
```

The application will now use the **cache** that you provide when uploading the lambda.

Second, because Twig still want to write cache, we have to tell him to do it somewhere else.
In **config/packages/twig.yaml**, add the following line:

```yaml
twig:
    default_path: '%kernel.project_dir%/templates'
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    cache: '/tmp/cache/twig'

```

With this, Twig will continue to write in the `/tmp` directory

This will reduce by more than 1 seconde the duration of the cold start. It could be lower if Twig was not part of the application (for an api for example) as we wouldn't have to write anything at all.

#### Deployement

Deployement can be simplified with a script that will run all of the others. Here an example (I'm bad with shell scripts so please be indulgent):

```sh
#!/bin/sh

echo "Creating build directory"
mkdir -p $PWD/lambda_build

echo "Cleaning build directory"
rm -rf $PWD/lambda_build/*

echo "Copying code to build directory"
cp -r $PWD/assets $PWD/lambda_build/assets
cp -r $PWD/bin $PWD/lambda_build/bin
cp -r $PWD/config $PWD/lambda_build/config
cp -r $PWD/public $PWD/lambda_build/public
cp -r $PWD/src $PWD/lambda_build/src
cp -r $PWD/templates $PWD/lambda_build/templates
cp -r $PWD/translations $PWD/lambda_build/translations
mkdir $PWD/lambda_build/var
cp $PWD/composer.json $PWD/lambda_build/composer.json
cp $PWD/composer.lock $PWD/lambda_build/composer.lock
cp $PWD/package.json $PWD/lambda_build/package.json
cp $PWD/package-lock.json $PWD/lambda_build/package-lock.json
cp $PWD/serverless.yml $PWD/lambda_build/serverless.yml
cp $PWD/symfony.lock $PWD/lambda_build/symfony.lock
cp $PWD/webpack.config.js $PWD/lambda_build/webpack.config.js
cp $PWD/postcss.config.js $PWD/lambda_build/postcss.config.js
cp $PWD/.env $PWD/lambda_build/.env

echo "CD in build directory"
cd $PWD/lambda_build

echo "Pulling dependencies"
APP_ENV=prod composer install --no-dev --optimize-autoloader
npm install

echo "Building assets"
npm run build

echo "Sync assets"
aws s3 sync public/build s3://cloud-project-eu-west-2-dev-assets --profile default

echo "Remove css and js"
rm public/build/*.js
rm public/build/*.css

echo "Remove node_modules"
rm -rf node_modules

echo "Clear cache"
php bin/console cache:clear --env=prod
php bin/console cache:warmup --env=prod

echo "Deploying"
serverless deploy

echo "Finished"
cd ..

```

Idea is to run all the commands in an other directory to not pollute the dev environment.

#### Conclusion

Should you run all of your application Symfony on a lambda ? Absolutely not !

Lambda are great for some task and I wanted to show here that it can also work with PHP, even with a framework used for a real website. Coding on a lambda does not change the way we code usually (except for `/tmp` directory) and therefore, we shouldn't be afraid to use it if it match our need.

Lambdas are of course more practical for jobs or apis. The peformance can be disturbing but with Symfony 5 (or 4.4) without twig we have a cold start below 300ms, which is really reasonable.

So try it out, have fun with them and long live to PHP :P