Skip to content

Commit 679b311

Browse files
committed
Rename tutorial and add to menu
1 parent f5f0194 commit 679b311

File tree

8 files changed

+163
-161
lines changed

8 files changed

+163
-161
lines changed

docs/examples/lambda-aws/index.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use MongoDB\Client;
4+
5+
require_once __DIR__ . '/vendor/autoload.php';
6+
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+
->selectDatabase('sample_guides')
11+
->selectCollection('planets')
12+
->find(
13+
[],
14+
[
15+
'sort' => ['orderFromSun' => 1],
16+
'typeMap' => ['root' => 'array'],
17+
],
18+
);
19+
20+
?>
21+
<!DOCTYPE html>
22+
<html lang="en">
23+
<head>
24+
<title>MongoDB Planets</title>
25+
</head>
26+
<body>
27+
<ul>
28+
<?php foreach ($planets as $planet) : ?>
29+
<li><?= $planet['name'] ?></li>
30+
<?php endforeach ?>
31+
</ul>
32+
</body>
33+
</html>

docs/examples/serverless/index.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

docs/tutorial.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Tutorials
1616
/tutorial/encryption
1717
/tutorial/gridfs
1818
/tutorial/indexes
19+
/tutorial/lambda-aws
1920
/tutorial/tailable-cursor
2021
/tutorial/example-data
2122
/tutorial/modeling-bson-data

docs/tutorial/lambda-aws.txt

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

docs/tutorial/serverless.txt

Lines changed: 0 additions & 114 deletions
This file was deleted.

phpcs.xml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@
7575

7676
<!-- Disable forbidden annotation sniff as excluding @api from the list doesn't work -->
7777
<exclude name="SlevomatCodingStandard.Commenting.ForbiddenAnnotations.AnnotationForbidden" />
78+
79+
<!-- Example file using HTML templating -->
80+
<exclude name="Generic.Files.InlineHTML.Found">
81+
<exclude-pattern>docs/examples/*/index.php</exclude-pattern>
82+
</exclude>
83+
<exclude name="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountAfterControlStructure">
84+
<exclude-pattern>docs/examples/*/index.php</exclude-pattern>
85+
</exclude>
7886
</rule>
7987

8088

0 commit comments

Comments
 (0)