Skip to content

Commit 883e951

Browse files
committed
Update index.php to display errors
Review tutorial
1 parent 824fe18 commit 883e951

File tree

2 files changed

+83
-58
lines changed

2 files changed

+83
-58
lines changed

docs/examples/aws-lambda/index.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
require_once __DIR__ . '/vendor/autoload.php';
66

7-
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('The MONGODB_URI environment variable is not set');
8-
$client = new Client($uri);
9-
$planets = $client
10-
->selectCollection('sample_guides', 'planets')
11-
->find([], ['sort' => ['orderFromSun' => 1]]);
7+
$uri = getenv('MONGODB_URI');
8+
9+
try {
10+
$client = new Client($uri);
11+
$planets = $client
12+
->selectCollection('sample_guides', 'planets')
13+
->find([], ['sort' => ['orderFromSun' => 1]]);
14+
} catch (Throwable $exception) {
15+
exit($exception->getMessage());
16+
}
1217

1318
?>
1419
<!DOCTYPE html>

docs/tutorial/aws-lambda.txt

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Deploy to AWS Lambda with Bref
33
==============================
44

5-
.. default-domain:: mongodb
6-
75
.. contents:: On this page
86
:local:
97
:backlinks: none
@@ -13,72 +11,86 @@ Deploy to AWS Lambda with Bref
1311
Overview
1412
--------
1513

16-
`Bref <https://bref.sh>`__ allows to deploy serverless PHP applications on AWS Lambda.
14+
`Bref <https://bref.sh>`__ lets you deploy serverless PHP applications on AWS Lambda.
1715
In this tutorial, you will deploy a simple PHP application with the MongoDB PHP extension,
1816
and connect to an Atlas cluster using AWS IAM authentication.
1917

2018
Prerequisites
2119
--------------
2220

23-
Before you begin, you must install Bref on your machine. You can follow the
24-
`official documentation to setup Bref <https://bref.sh/docs/setup>`__.
21+
To deploy to AWS Lambda by using Bref, you must have the following components set up:
22+
23+
- AWS account with access keys
24+
- Serverless Framework
25+
26+
To learn how to set these up, follow the `Setup tutorial<https://bref.sh/docs/setup>`__
27+
in the Bref official documentation.
2528

2629
Install the MongoDB extension
2730
-----------------------------
2831

29-
By default, the bref layer is compiled with PHP and a few extensions. Additional extensions
30-
are provided in additional layers.
32+
Bref uses Lambda layers to provide the PHP runtime. The ``bref`` layer is compiled
33+
with PHP and a few extensions. Other extensions, like ``mongodb``, are available
34+
in additional layers.
3135

3236
Start by creating a new directory for your project and install the required MongoDB
33-
and Bref dependencies. This project will be a bare minimum PHP web application that
34-
connects to a MongoDB cluster.
37+
and Bref dependencies.
38+
39+
.. code-block:: none
3540

36-
.. code-block:: bash
41+
$ mkdir bref-mongodb-app && cd bref-mongodb-app
42+
$ composer init
43+
$ composer require bref/bref bref/extra-php-extensions mongodb/mongodb
3744

38-
mkdir bref-mongodb-app && cd bref-mongodb-app
39-
composer init
40-
composer require bref/bref bref/extra-php-extensions mongodb/mongodb
41-
vendor/bin/bref init
45+
Then initialize the serverless configuration using the ``bref`` command.
4246

47+
.. code-block:: none
4348

44-
The file ``index.php`` has been created. To validate the deployment, you can start
45-
by deploying this default application.
49+
$ vendor/bin/bref init
4650

47-
.. code-block:: bash
4851

49-
serverless deploy
52+
This series of commands creates a file named ``index.php``. To validate your
53+
setup, try deploying this default application. This will output an
54+
URL that renders a webpage with the Bref logo.
5055

56+
.. code-block:: none
5157

52-
Bref 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>`__.
58+
$ serverless deploy
59+
60+
61+
Now that you have initialized the project, you will add the ``mongodb`` extension.
62+
Locate the "Serverless config" name in the list of extensions provided by
63+
`bref/extra-php-extension <https://github.com/brefphp/extra-php-extensions>`__.
64+
Add it to the ``layers`` of the function in ``serverless.yaml``, this file
65+
will look like this:
5466

5567
.. code-block:: yaml
5668

57-
plugins:
58-
- ./vendor/bref/bref
59-
- ./vendor/bref/extra-php-extensions
69+
plugins:
70+
- ./vendor/bref/bref
71+
- ./vendor/bref/extra-php-extensions
6072

61-
functions:
62-
api:
63-
handler: index.php
64-
runtime: php-83-fpm
65-
layers:
66-
- ${bref-extra:mongodb-php-83}
73+
functions:
74+
api:
75+
handler: index.php
76+
runtime: php-83-fpm
77+
layers:
78+
- ${bref-extra:mongodb-php-83}
6779

6880

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/>`__.
81+
Let's use the MongoDB driver with a web page that list planets from Atlas'
82+
`sample dataset <https://www.mongodb.com/docs/atlas/sample-data/>`__.
7183
Replace the contents of ``index.php`` with the following:
7284

7385
.. literalinclude:: /examples/aws-lambda/index.php
7486
:language: php
7587

7688

77-
Deploy the application
89+
Redeploy the application with the new ``index.php``:
7890

79-
.. code-block:: bash
91+
.. code-block:: none
8092

81-
serverless deploy
93+
$ serverless deploy
8294

8395

8496
The application will not work unless you define the ``MONGODB_URI`` environment variable.
@@ -90,33 +102,41 @@ Atlas supports passwordless authentication with AWS credentials. In any Lambda f
90102
AWS sets environment variables that contains the access token and secret token with
91103
the role assigned to deployed function.
92104

93-
Set up `unified AWS Access <https://www.mongodb.com/docs/atlas/security/set-up-unified-aws-access/>`__:
94-
95105
1. Open the Lambda function in the AWS console
96106
2. In "Configuration > Permission", copy the "Role name"
97-
3. Open the MongoDB Atlas project
98-
4. Go to "Security > Database Access"
99-
5. Click "Add a new Database User"
100-
6. Select Authentication Method: "AWS IAM", type "IAM Role" and paste the role name in "AWS Role ARN".
101-
7. Add "Built-in Role": "Read and write any database"
102-
8. Validate by clicking on "Add user".
103-
104-
Now that the permissions have been configured, the lambda function is allowed to access
105-
your Atlas cluster. You can configure your application with the Atlas endpoint.
107+
3. Add this role to your Atlas cluster with the built-in Role: "Read and write any database"
106108

107-
Update the ``serverless.yml`` file to pass the environment variable ``MONGODB_URI``
109+
To learn how to set up unified AWS access, see `Set Up Unified AWS Access
110+
<https://www.mongodb.com/docs/atlas/security/set-up-unified-aws-access/>`__ in the
111+
MongoDB Atlas documentation.
108112

109-
.. code-block:: yaml
113+
Now that the permissions have been configured, the Lambda function is allowed to access
114+
your Atlas cluster. You can configure your application with the Atlas endpoint.
110115

111-
provider:
112-
environment:
113-
MONGODB_URI: "mongodb+srv://cluster0.example.mongodb.net/"
116+
Access to Atlas clusters is also restricted by IP address. Since the range of IP that comes
117+
from AWS is very wide, you can `allow access from everywhere <https://www.mongodb.com/docs/atlas/security/ip-access-list/>`__.
114118

119+
.. note::
115120

116-
The value can be found in "Atlas > Deployment > Database > Connect". Select "3. AWS IAM".
121+
Using VPC Peering is recommended in order to isolate your Atlas cluster from Internet.
122+
This requires the Lambda function to be deployed in this AWS VPC.
123+
124+
Find the connection URI in the Atlas UI :guilabel:`Atlas > Deployment > Database > Connect`.
125+
Select :guilabel:`3. AWS IAM`.
117126
Remove the ``<AWS access key>:<AWS secret key>`` part from the URI, the credentials
118127
will be read from environment variables.
119128

120-
.. code-block:: bash
129+
Update the ``serverless.yml`` file to pass the environment variable ``MONGODB_URI``.
130+
131+
.. code-block:: yaml
132+
133+
provider:
134+
environment:
135+
MONGODB_URI: "mongodb+srv://cluster0.example.mongodb.net/?authSource=%24external&authMechanism=MONGODB-AWS&retryWrites=true&w=majority"
136+
137+
Finally, deploy with the new configuration. Once done you can access the function URL
138+
and see the list of planets from your Atlas cluster.
139+
140+
.. code-block:: none
121141

122-
serverless deploy
142+
$ serverless deploy

0 commit comments

Comments
 (0)