Automate Deployment of PHP Applications to AWS Lambda with Bref, AWS SAM, and Travis CI
Tags: travisci, bref, php, lambda
Published: 2020-02-16 03:24:52

---
title: Automate Deployment of PHP Applications to AWS Lambda with Bref, AWS SAM, and Travis CI
published: true
date: 2019-05-25 12:36:07 UTC
tags: travis-ci,bref,php,lambda
canonical_url: https://medium.com/@guillermoandrae/automate-deployment-of-php-applications-to-aws-lambda-with-bref-aws-sam-and-travis-ci-90231dfae6ff
cover_image: https://cdn-images-1.medium.com/max/1024/1*Lvz897QCIoq8ID_3MPn_cA.jpeg
---
If you’re a PHP developer whose cloud provider of choice is AWS, chances are you’ve suffered through a bit of serverless FOMO due to AWS Lambda’s lack of support for PHP. But thanks to the [AWS Serverless Application Model](https://aws.amazon.com/serverless/sam/) (AWS SAM) and [Bref](https://bref.sh) — both open source projects available on [GitHub](https://github.com) — it’s now possible to deploy your PHP applications to Lambda without having to fiddle with Node.js shims. And with the help of [Travis CI](https://travis-ci.org/), running automated tests and deployments is essentially a cinch. There are, however, a few gotchas.

### I Don’t Know Who or What a “Bref” Is

I didn’t, either, until a few weeks ago. [Matthieu Napoli](https://mnapoli.fr/)’s “Bref” (which is the French word for “brief”) has been around for a while, and has caught the attention of some [prominent PHP-ers](https://akrabat.com/php-architect-serverless-php-with-bref-part-1/). In short:

> Bref provides the tools and documentation to easily deploy and run serverless PHP applications.

You should take a look at the [Bref documentation](https://bref.sh/docs) before you move on to familiarize yourself with the goals of the project, walk through the “Getting Started” steps, and read the recommendations concerning the project’s use (for example, don’t expect to use Bref to deploy your legacy application without experiencing the sort of difficulty you’d normally associate with deploying a legacy application).

### Respect Due

The work detailed in this post rests comfortably on the shoulders of unselfish giants; I would be remiss if I didn’t point you to [this post](https://www.sysengcooking.com/posts/deploy-aws-lambda-functions-with-aws-sam-cli-and-travis-ci-2/) explaining what’s needed to deploy Python applications to AWS Lambda using AWS SAM and Travis CI. You’ll find that a lot of what is covered there was put to use here, and the author ([Mike Vanbuskirk](https://sysengcooking.com)) does a great job covering the [pertinent prerequisites](https://www.sysengcooking.com/posts/deploy-aws-lambda-functions-with-aws-sam-cli-and-travis-ci-1/). In fact, I consider that series to be a prerequisite to this post, and suggest you read it before continuing, unless you’re impatient and just want to get to the goodies.

### So… Travis CI?

Alright. Into the fray. I’ve set up a sample application at [https://github.com/guillermoandrae/bref-hello-world](https://github.com/guillermoandrae/bref-hello-world), and will be using its .travis.yml file to explain this process:

```
language: generic
dist: xenial
before\_install:
  - phpenv global 7.2
install:
  - pip install --user awscli
  - pip install --user aws-sam-cli
script:
  - composer install --optimize-autoloader
  - composer test
  - composer package
deploy:
  provider: script
  script: composer deploy
  skip\_cleanup: true
  on:
    repo: guillermoandrae/bref-hello-world
env:
  global:
  - AWS\_DEFAULT\_REGION=us-east-1
  - secure: <obfuscated AWS Access Key ID>
  - secure: <obfuscated AWS Secret Key>
```

### The Biggest Gotcha: The Build Environment

Let’s start at the tippy top:

```
language: generic
dist: xenial
```

A cursory look at the configuration file reveals two points:

- I’m using [phpenv](https://github.com/phpenv/phpenv), which is a PHP version manager.
- I’m using [pip](https://pip.pypa.io/en/stable/), which is the standard package manager for Python.

The build environment needs to have both PHP and Python installed in order for all of the necessary tasks to be successfully executed. If you’ve used Travis CI for your PHP projects before, you’ve probably specified the language at the top of the YAML file with a line like language: php, which would prompt Travis CI to use a PHP build environment. I needed to use a lesser known language designation that supported my use case.

Travis CI provides two build environments that are not language-specific: the [minimal environment](https://docs.travis-ci.com/user/languages/minimal-and-generic/#minimal) and the [generic environment](https://docs.travis-ci.com/user/languages/minimal-and-generic/#generic). The former is, as the name implies, pretty bare. Aside from gcc, curl, and a few other tools, minimal is not outfitted with much besides Docker and Python. The generic environment, on the other hand, includes everything included in the minimal environment as well as some additional runtimes such as Go, Ruby, and PHP.

If you’re not familiar with it, the dist parameter of a Travis CI configuration file is used to denote the specific distribution of the build environment that you’d like to use. With Ubuntu images, you’ve got three options — trusty (the default, used when no distribution is specified), precise, and xenial —and they all contain specific configuration settings. In the case of the minimal and generic build environments, however, only precise and xenial are available. I’m using the xenial environment, as it’s the one that contains a suitable version of PHP.

### A Little More About PHP Versions

By default, the xenial environment uses the lowest of the [three versions of PHP](https://docs.travis-ci.com/user/reference/xenial/#php-support) that are installed: 5.6. To change that, I used phpenv to specify the version that meets our needs. phpenv is installed on all Travis CI images that run PHP:

```
before\_install:
- phpenv global 7.2
```

### CLI Tool Installation vs. Python Versions

In Mike’s series, the installation of the AWS CLI and AWS SAM CLI tools seemed pretty straight forward, and I had no problem installing them on my laptop. I, however, found myself running into this error using Travis CI:

```
InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
```

I tried using the python parameter, then the TRAVIS\_PYTHON\_VERSION environment variable to specify a newer Python version, but neither brought me joy. So, after some focussed Googling, I was led to a solution that involved adding the --user flag to the installation commands. So I ended up with:

```
install:
- pip install --user awscli
- pip install --user aws-sam-cli
```

### Composer Scripts FTW

I really, really, really, really, really like [Composer scripts](https://getcomposer.org/doc/articles/scripts.md). In the past, I used [Phing](https://www.phing.info/) as a task runner without realizing that I could leverage the scripts feature of Composer to do almost exactly the same thing. I’ve since seen the proverbial light and haven’t turned back:

```
script:
- composer install --optimize-autoloader
- composer test
- composer package
```

A look at an excerpt of the project’s composer.json file will shed some light on exactly what is going on:

{% gist https://gist.github.com/guillermoandrae/4a0137609144ab3df8f849b6422a0fd0.js %}

The test script includes calls to [Squizlabs’ code beautifier and code sniffer](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automatically) as well as a call to run the tests and generate coverage reports in both text format and Clover format (the Clover format is especially useful if you plan to integrate Travis CI with a tool/service that can create code coverage visualizations or store a project’s code coverage history). The package script calls sam package to build the [stack](https://bref.sh/docs/deploy.html#stacks) configuration file.

### Deploying… Finally!

I’m using Travis CI’s script deployment option to call my deployment script and associate the appropriate GitHub repository. The details behind the composer deploy call below are uncovered in the aforementioned composer.json excerpt:

```
deploy:
  provider: script
  script: composer deploy
  skip\_cleanup: true
  on:
    repo: guillermoandrae/bref-hello-world
```

### That’s All, Folks

We’re done. PHP on AWS Lambda with a proper CI/CD pipeline. No fuss. Life is good. If you have any questions or comments, don’t be shy about sharing them.