|
| 1 | +.. _php-aws-lambda: |
| 2 | + |
| 3 | +==================== |
| 4 | +Deploy to AWS Lambda |
| 5 | +==================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: tutorial |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: serverless, deployment, code example, live |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the tool `Bref |
| 24 | +<https://bref.sh>`__ to deploy serverless PHP applications on AWS |
| 25 | +Lambda. This guide demonstrates how to deploy a PHP application built by |
| 26 | +using the {+library-short+} and connect to an Atlas cluster by using AWS |
| 27 | +IAM authentication. |
| 28 | + |
| 29 | +Before You Get Started |
| 30 | +---------------------- |
| 31 | + |
| 32 | +Before you can deploy to AWS Lambda by using Bref, you must set up the |
| 33 | +following components: |
| 34 | + |
| 35 | +- AWS account and access keys |
| 36 | +- `Serverless Framework <https://www.serverless.com/>`__ |
| 37 | + |
| 38 | +The `Setup <https://bref.sh/docs/setup>`__ tutorial in the Bref |
| 39 | +documentation describes how to prepare these components. |
| 40 | + |
| 41 | +Install Dependencies |
| 42 | +-------------------- |
| 43 | + |
| 44 | +Bref uses `Lambda layers |
| 45 | +<https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html>`__ to |
| 46 | +provide the PHP runtime. The ``bref`` layer is compiled with PHP and a |
| 47 | +few extensions. You can install extensions, such as ``mongodb``, |
| 48 | +in other layers. |
| 49 | + |
| 50 | +The following commands create a new project directory and install the |
| 51 | +MongoDB and Bref dependencies: |
| 52 | + |
| 53 | +.. code-block:: bash |
| 54 | + |
| 55 | + mkdir bref-mongodb-app && cd bref-mongodb-app |
| 56 | + composer init |
| 57 | + composer require bref/bref bref/extra-php-extensions mongodb/mongodb |
| 58 | + |
| 59 | +Then, initialize the serverless configuration by using the ``bref`` |
| 60 | +command: |
| 61 | + |
| 62 | +.. code-block:: bash |
| 63 | + |
| 64 | + vendor/bin/bref init |
| 65 | + |
| 66 | +After the commands complete, your project contains the following files: |
| 67 | + |
| 68 | +- ``composer.json``: Lists PHP dependencies installed in the ``vendor`` directory |
| 69 | +- ``index.php``: Defines a sample webpage |
| 70 | +- ``serverless.yml``: Configures the deployment |
| 71 | + |
| 72 | +Deploy the Sample Application |
| 73 | +----------------------------- |
| 74 | + |
| 75 | +To validate your setup, try to deploy the default application. The |
| 76 | +following command deploys the application and returns a URL that renders |
| 77 | +a webpage that shows the Bref logo: |
| 78 | + |
| 79 | +.. code-block:: bash |
| 80 | + |
| 81 | + serverless deploy |
| 82 | + |
| 83 | +Add the MongoDB Extension to Your Configuration |
| 84 | +----------------------------------------------- |
| 85 | + |
| 86 | +After you initialize the project, you can add the ``mongodb`` extension. |
| 87 | +Locate the ``Serverless config`` name in the list of extensions provided |
| 88 | +by the `bref/extra-php-extension |
| 89 | +<https://github.com/brefphp/extra-php-extensions>`__ package. |
| 90 | + |
| 91 | +Add it to the ``layers`` of the function in the ``serverless.yaml`` |
| 92 | +file, as shown in the following code: |
| 93 | + |
| 94 | +.. code-block:: yaml |
| 95 | + |
| 96 | + plugins: |
| 97 | + - ./vendor/bref/bref |
| 98 | + - ./vendor/bref/extra-php-extensions # Adds the extra Serverless plugin |
| 99 | + |
| 100 | + functions: |
| 101 | + api: |
| 102 | + handler: index.php |
| 103 | + runtime: php-83-fpm |
| 104 | + layers: |
| 105 | + - ${bref-extra:mongodb-php-81} # Adds the MongoDB layer |
| 106 | + |
| 107 | +Customize the Sample Application |
| 108 | +-------------------------------- |
| 109 | + |
| 110 | +This step explains how to create a web page that list planets from the |
| 111 | +Atlas :atlas:`sample data </sample-data>`. Replace the contents of |
| 112 | +``index.php`` with the following code: |
| 113 | + |
| 114 | +.. literalinclude:: /examples/aws-lambda/index.php |
| 115 | + :language: php |
| 116 | + |
| 117 | +Redeploy the application with the new ``index.php`` by running the |
| 118 | +following command: |
| 119 | + |
| 120 | +.. code-block:: bash |
| 121 | + |
| 122 | + serverless deploy |
| 123 | + |
| 124 | +The application page displays an error message because the ``MONGODB_URI`` |
| 125 | +environment variable is not set.The following section explains how to |
| 126 | +set your connection string as an environment variable. |
| 127 | + |
| 128 | +Set AWS Credentials |
| 129 | +------------------- |
| 130 | + |
| 131 | +Atlas supports passwordless authentication when using AWS credentials. |
| 132 | +In any Lambda function, AWS sets environment variables that contain the |
| 133 | +access token and secret token for the role assigned to deploy the function. |
| 134 | + |
| 135 | +The following steps demonstrate how to set the role in your Atlas |
| 136 | +cluster: |
| 137 | + |
| 138 | +1. Open the Lambda function in the AWS console. |
| 139 | +#. Enter :guilabel:`Configuration > Permission`, then copy the :guilabel:`Role name`. |
| 140 | +#. Add this role to your Atlas cluster in the :guilabel:`Database |
| 141 | + Access` section. Select the :guilabel:`AWS IAM` authentication method |
| 142 | + and set the built-in role ``Read and write any database``. |
| 143 | + |
| 144 | +To learn how to set up unified AWS access, see :atlas:`Set Up Unified |
| 145 | +AWS Access </security/set-up-unified-aws-access/>` in the Atlas documentation. |
| 146 | + |
| 147 | +After you configure the permissions, the Lambda function is allowed to access |
| 148 | +your Atlas cluster. Next, configure your application to use the Atlas endpoint. |
| 149 | + |
| 150 | +Access to Atlas clusters is also restricted by IP address. Since the |
| 151 | +range of IP addresses that comes from AWS is very wide, you can allow |
| 152 | +access from everywhere. To learn how to allow universal access, see |
| 153 | +:atlas:`Configure IP Access List Entries </security/ip-access-list/>` in |
| 154 | +the Atlas documentation. |
| 155 | + |
| 156 | +.. note:: |
| 157 | + |
| 158 | + Using VPC Peering is recommended in order to isolate your Atlas |
| 159 | + cluster from the internet. This requires the Lambda function to be |
| 160 | + deployed in this AWS VPC. |
| 161 | + |
| 162 | +Next, copy your connection string and remove the ``<AWS access key>:<AWS |
| 163 | +secret key>`` section, as your credentials are read from environment variables. |
| 164 | + |
| 165 | +Update the ``serverless.yml`` file in your project to set the |
| 166 | +environment variable ``MONGODB_URI`` to your connection string: |
| 167 | + |
| 168 | +.. code-block:: yaml |
| 169 | + |
| 170 | + provider: |
| 171 | + environment: |
| 172 | + MONGODB_URI: "<connection string without credentials>" |
| 173 | + |
| 174 | +Deploy Your Application |
| 175 | +----------------------- |
| 176 | + |
| 177 | +Finally, deploy with the new configuration: |
| 178 | + |
| 179 | +.. code-block:: bash |
| 180 | + |
| 181 | + serverless deploy |
| 182 | + |
| 183 | +After deployment completes, you can access the URL and see the |
| 184 | +list of planets from your collection. |
0 commit comments