|
| 1 | +============================================ |
| 2 | +Deploy to Serverless AWS Lambda with BrefPHP |
| 3 | +============================================ |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. versionadded:: 1.17 |
| 14 | + |
| 15 | +Overview |
| 16 | +-------- |
| 17 | + |
| 18 | +`Bref <https://bref.sh>` allows to deploy serverless PHP applications on AWS Lambda. |
| 19 | +In this tutorial, you will deploy a simple PHP application with the MongoDB PHP extension, |
| 20 | +and connect to an Atlas cluster using AWS IAM authentication. |
| 21 | + |
| 22 | +Pre-requisites |
| 23 | +-------------- |
| 24 | + |
| 25 | +Before you begin, you must install bref on your machine. You can follow the |
| 26 | +`official documentation to setup bref <https://bref.sh/docs/setup>`_. |
| 27 | + |
| 28 | +Install the mongodb extension |
| 29 | +----------------------------- |
| 30 | + |
| 31 | +By default, the bref layer is compiled with PHP and a few extensions. Additional extensions |
| 32 | +are provided in additional layers. |
| 33 | + |
| 34 | +Start by creating a new directory for your project and install the required mongodb |
| 35 | +and bref dependencies. This project will be a bare minimum PHP web application that |
| 36 | +connects to a MongoDB cluster, no framework is used, but you can adapt the code to |
| 37 | +your needs. |
| 38 | + |
| 39 | +.. code-block:: bash |
| 40 | + mkdir bref-mongodb-app && cd bref-mongodb-app |
| 41 | + composer init |
| 42 | + composer require bref/bref bref/extra-php-extensions |
| 43 | + vendor/bin/bref init |
| 44 | + |
| 45 | + |
| 46 | +A very simple web page have been created. To validate the deployment, you can start |
| 47 | +by deploying this default application. |
| 48 | + |
| 49 | +.. code-block:: bash |
| 50 | + serverless deploy |
| 51 | + |
| 52 | +BrefPHP provides a Lambda layer with PHP and some very common extensions. |
| 53 | +Additional extensions are provided by the package `bref/extra-php-extension <https://github.com/brefphp/extra-php-extensions>`. |
| 54 | + |
| 55 | +.. code-block:: yaml |
| 56 | + |
| 57 | + plugins: |
| 58 | + - ./vendor/bref/bref |
| 59 | + - ./vendor/bref/extra-php-extensions |
| 60 | + |
| 61 | + functions: |
| 62 | + api: |
| 63 | + handler: index.php |
| 64 | + runtime: php-83-fpm |
| 65 | + layers: |
| 66 | + - ${bref-extra:mongodb-php-83} |
| 67 | + |
| 68 | + |
| 69 | +Let's try to use the MongoDB driver with this simple web page that list planets |
| 70 | +from the `sample dataset <https://www.mongodb.com/docs/atlas/sample-data/>`. |
| 71 | +Replace the contents of ``index.php`` with the following: |
| 72 | + |
| 73 | +.. literalinclude:: /examples/lambda-aws/index.php |
| 74 | + :language: php |
| 75 | + |
| 76 | + |
| 77 | +Deploy the application |
| 78 | + |
| 79 | +.. code-block:: bash |
| 80 | + serverless deploy |
| 81 | + |
| 82 | + |
| 83 | +The application will not work unless you provide a working ``MONGODB_URI`` variable. |
| 84 | + |
| 85 | +AWS Credentials |
| 86 | +--------------- |
| 87 | + |
| 88 | +Atlas supports passwordless authentication with AWS credentials. In any Lambda function, |
| 89 | +AWS sets environment variables that contains the access token and secret token with |
| 90 | +the role assigned to deployed function. |
| 91 | + |
| 92 | +Set Up `unified AWS Access <https://www.mongodb.com/docs/atlas/security/set-up-unified-aws-access/>` |
| 93 | + |
| 94 | +1. Open the lambda function in the AWS console |
| 95 | +2. In "Configuration > Permission", copy the "Role name" |
| 96 | +3. Open the MongoDB Atlas project |
| 97 | +4. Go to "Security > Database Access" |
| 98 | +5. Click "Add a new Database User" |
| 99 | +6. Select Authentication Method: "AWS IAM", type "IAM Role" and paste the role name in "AWS Role ARN". |
| 100 | +7. Add "Built-in Role": "Read and write any database" |
| 101 | +8. Validate by clicking on "Add user". |
| 102 | + |
| 103 | +Now that the permissions have been configured, the lambda function is allowed to access |
| 104 | +your Atlas cluster. You can configure your application with the Atlas endpoint. |
| 105 | + |
| 106 | +Update the ``serverless.yml`` file to pass the environment variable ``MONGODB_URI`` |
| 107 | + |
| 108 | +.. code-block:: yaml |
| 109 | + |
| 110 | + provider: |
| 111 | + environment: |
| 112 | + MONGODB_URI: "mongodb+srv://cluster0.abcdefg.mongodb.net/" |
| 113 | + |
| 114 | + |
| 115 | +The value can be found in "Atlas > Deployment > Database > Connect". Select "3. AWS IAM". |
| 116 | +You can remove the ``<AWS access key>:<AWS secret key>`` part from the URI, the credentials |
| 117 | +will be read from environment variables. |
| 118 | + |
| 119 | +.. code-block:: bash |
| 120 | + |
| 121 | + serverless deploy |
0 commit comments