-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1163 Create tutorial for using MongoDB with Bref #1273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
3646ef7
af95c04
2acc8fa
576ac73
46da485
5689613
c97aef9
16a7b31
c765d0c
2366c95
824fe18
883e951
125a427
4131fa3
6706469
99cebe4
1cace2d
f5adae8
6f8e963
9b089f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,7 @@ psalm.xml | |
.phpbench/ | ||
phpbench.json | ||
|
||
# bref | ||
.serverless/ | ||
|
||
mongocryptd.pid |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "mongodb/bref-tutorial", | ||
"type": "project", | ||
"license": "MIT", | ||
"repositories": [ | ||
{ | ||
"type": "path", | ||
"url": "../../..", | ||
"options": { | ||
"symlink": false | ||
} | ||
} | ||
], | ||
"require": { | ||
"bref/bref": "^2.1", | ||
"bref/extra-php-extensions": "^1.4", | ||
"mongodb/mongodb": "@dev" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use MongoDB\Client; | ||
|
||
require_once __DIR__ . '/vendor/autoload.php'; | ||
|
||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('The MONGODB_URI environment variable is not set'); | ||
$client = new Client($uri); | ||
$planets = $client->sample_guides->planets | ||
->find( | ||
[], | ||
['sort' => ['orderFromSun' => 1]], | ||
); | ||
|
||
?> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>MongoDB Planets</title> | ||
</head> | ||
<body> | ||
<ul> | ||
<?php foreach ($planets as $planet) : ?> | ||
<li><?= $planet->name ?></li> | ||
<?php endforeach ?> | ||
</ul> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
service: app | ||
|
||
provider: | ||
name: aws | ||
region: us-east-1 | ||
environment: | ||
MONGODB_URI: ${env:MONGODB_URI} | ||
|
||
plugins: | ||
- ./vendor/bref/bref | ||
- ./vendor/bref/extra-php-extensions | ||
|
||
functions: | ||
api: | ||
handler: index.php | ||
description: '' | ||
runtime: php-83-fpm | ||
timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds) | ||
events: | ||
- httpApi: '*' | ||
layers: | ||
- ${bref-extra:mongodb-php-83} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,124 @@ | ||||||||
============================== | ||||||||
Deploy to AWS Lambda with Bref | ||||||||
============================== | ||||||||
|
||||||||
.. default-domain:: mongodb | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
.. contents:: On this page | ||||||||
:local: | ||||||||
:backlinks: none | ||||||||
:depth: 2 | ||||||||
:class: singlecol | ||||||||
|
||||||||
.. versionadded:: 1.17 | ||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
Overview | ||||||||
-------- | ||||||||
|
||||||||
`Bref <https://bref.sh>`_ allows to deploy serverless PHP applications on AWS Lambda. | ||||||||
In this tutorial, you will deploy a simple PHP application with the MongoDB PHP extension, | ||||||||
and connect to an Atlas cluster using AWS IAM authentication. | ||||||||
|
||||||||
Prerequisites | ||||||||
-------------- | ||||||||
|
||||||||
Before you begin, you must install Bref on your machine. You can follow the | ||||||||
`official documentation to setup Bref <https://bref.sh/docs/setup>`_. | ||||||||
|
||||||||
Install the MongoDB extension | ||||||||
----------------------------- | ||||||||
|
||||||||
By default, the bref layer is compiled with PHP and a few extensions. Additional extensions | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
are provided in additional layers. | ||||||||
|
||||||||
Start by creating a new directory for your project and install the required MongoDB | ||||||||
and Bref dependencies. This project will be a bare minimum PHP web application that | ||||||||
connects to a MongoDB cluster. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: I think this task was already described in more detail in the first section, so this sentence should be omitted to avoid repetition and clutter.
Suggested change
|
||||||||
|
||||||||
.. code-block:: bash | ||||||||
|
||||||||
mkdir bref-mongodb-app && cd bref-mongodb-app | ||||||||
composer init | ||||||||
composer require bref/bref bref/extra-php-extensions mongodb/mongodb | ||||||||
vendor/bin/bref init | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
|
||||||||
The file ``index.php`` has been created. To validate the deployment, you can start | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
by deploying this default application. | ||||||||
|
||||||||
.. code-block:: bash | ||||||||
|
||||||||
serverless deploy | ||||||||
|
||||||||
|
||||||||
Bref provides a Lambda layer with PHP and some very common extensions. | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
Additional extensions are provided by the package `bref/extra-php-extension <https://github.com/brefphp/extra-php-extensions>`_. | ||||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
.. code-block:: yaml | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
plugins: | ||||||||
- ./vendor/bref/bref | ||||||||
- ./vendor/bref/extra-php-extensions | ||||||||
|
||||||||
functions: | ||||||||
api: | ||||||||
handler: index.php | ||||||||
runtime: php-83-fpm | ||||||||
layers: | ||||||||
- ${bref-extra:mongodb-php-83} | ||||||||
|
||||||||
|
||||||||
Let's try to use the MongoDB driver with this simple web page that list planets | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
from the :manual:`sample dataset </atlas/sample-data/>`. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ccho-mongodb: Is this the correct prefix? The intended URL is https://www.mongodb.com/docs/atlas/sample-data/, but conf.py in mongodb/docs-php-library suggests this will be "http://docs.mongodb.org/manual%s". Now that I'm thinking about it, that pattern looks incorrect since all docs should be pointing to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, I got it wrong. The prefix should be added. Maybe added to this standing PR: mongodb/docs-php-library#57 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reverted to full url links for the time being. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the extlinks entry may override the Snooty role. The Docs Platform team can probably confirm/deny this. For example, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I asked about this in Slack and will revise mongodb/docs-php-library#57 based on the response. |
||||||||
Replace the contents of ``index.php`` with the following: | ||||||||
|
||||||||
.. literalinclude:: /examples/aws-lambda/index.php | ||||||||
:language: php | ||||||||
|
||||||||
|
||||||||
Deploy the application | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mongodb/dbx-php What's your opinion on the formatting of this tutorial? It's actually steps. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No objections if you feel it's worth the effort. Please make sure that formatting doesn't clash with "AWS Credentials", which has a numbered list within. I'm not expecting any issue nesting a list within the RST step syntax -- just a request to confirm the rendered output looks presentable. Edit: looking at the Kafka tutorial, I consider it less useful that the steps aren't incorporated into the "On this page" navigation. Given that, I'd lean toward no changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think skipping steps formatting is fine and more efficient with time. The issue I was focusing on was missing punctuation. Perhaps it could be something to experiment with in a future PR! |
||||||||
|
||||||||
.. code-block:: bash | ||||||||
|
||||||||
serverless deploy | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
|
||||||||
The application will not work unless you define the ``MONGODB_URI`` environment variable. | ||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
AWS Credentials | ||||||||
--------------- | ||||||||
|
||||||||
Atlas supports passwordless authentication with AWS credentials. In any Lambda function, | ||||||||
AWS sets environment variables that contains the access token and secret token with | ||||||||
the role assigned to deployed function. | ||||||||
|
||||||||
Set up :manual:`unified AWS Access </atlas/security/set-up-unified-aws-access/>`: | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ccho-mongodb: Intended link here is https://www.mongodb.com/docs/atlas/security/set-up-unified-aws-access/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use the https://github.com/mongodb/snooty-parser/blob/main/snooty/rstspec.toml#L1290-L1292 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Does the very existence of mongodb/docs-php-library#57 was going to introduce As I mentioned in Slack, we'd need a custom role for
...might mean we lose access to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disregard my last comment. @schmalliso confirmed that @GromNaN: I'm not sure why |
||||||||
|
||||||||
1. Open the Lambda function in the AWS console | ||||||||
2. In "Configuration > Permission", copy the "Role name" | ||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
3. Open the MongoDB Atlas project | ||||||||
4. Go to "Security > Database Access" | ||||||||
5. Click "Add a new Database User" | ||||||||
6. Select Authentication Method: "AWS IAM", type "IAM Role" and paste the role name in "AWS Role ARN". | ||||||||
7. Add "Built-in Role": "Read and write any database" | ||||||||
8. Validate by clicking on "Add user". | ||||||||
|
||||||||
Now that the permissions have been configured, the lambda function is allowed to access | ||||||||
your Atlas cluster. You can configure your application with the Atlas endpoint. | ||||||||
|
||||||||
Update the ``serverless.yml`` file to pass the environment variable ``MONGODB_URI`` | ||||||||
|
||||||||
.. code-block:: yaml | ||||||||
|
||||||||
provider: | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
environment: | ||||||||
MONGODB_URI: "mongodb+srv://cluster0.example.mongodb.net/" | ||||||||
|
||||||||
|
||||||||
The value can be found in "Atlas > Deployment > Database > Connect". Select "3. AWS IAM". | ||||||||
Remove the ``<AWS access key>:<AWS secret key>`` part from the URI, the credentials | ||||||||
will be read from environment variables. | ||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
.. code-block:: bash | ||||||||
|
||||||||
serverless deploy |
Uh oh!
There was an error while loading. Please reload this page.