Skip to content

Commit b2fab0c

Browse files
committed
PHPLIB-1163 Create tutorial for using MongoDB with Bref
1 parent d2f3766 commit b2fab0c

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ psalm.xml
1919
.phpbench/
2020
phpbench.json
2121

22+
# bref
23+
.serverless/
24+
2225
mongocryptd.pid
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "mongodb/bref-tutorial",
3+
"type": "project",
4+
"license": "MIT",
5+
"repositories": [
6+
{
7+
"type": "path",
8+
"url": "../../..",
9+
"options": {
10+
"symlink": false
11+
}
12+
}
13+
],
14+
"require": {
15+
"bref/bref": "^2.1",
16+
"bref/extra-php-extensions": "^1.4",
17+
"mongodb/mongodb": "@dev"
18+
}
19+
}

docs/examples/serverless/index.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
25+
<title>MongoDB Planets</title>
26+
<link href="https://fonts.googleapis.com/css?family=Dosis:300&display=swap" rel="stylesheet">
27+
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
28+
</head>
29+
<body class="flex h-screen">
30+
<div class="rounded-full mx-auto self-center relative" style="height: 400px; width: 400px; background: linear-gradient(123.19deg, #266488 3.98%, #258ECB 94.36%)">
31+
<h1 class="font-light absolute w-full text-center text-blue-200" style="font-family: Dosis; font-size: 45px; top: 35%">Hello there,</h1>
32+
<div class="w-full relative absolute" style="top: 60%; height: 50%">
33+
<div class="absolute inset-x-0 bg-white" style="bottom: 0; height: 55%"></div>
34+
<svg viewBox="0 0 1280 311" fill="none" xmlns="http://www.w3.org/2000/svg"><g clip-path="url(#clip0)"><path d="M1214 177L1110.5 215.5L943.295 108.5L807.5 168.5L666 66.5L581 116L517 49.5L288.5 184L163.5 148L-34.5 264.5V311H1317V258.5L1214 177Z" fill="white"/><path d="M1214 177L1110.5 215.5L943.295 108.5L807.5 168.5L666 66.5L581 116L517 49.5L288.5 184L163.5 148L-34.5 264.5L163.5 161L275 194L230.5 281.5L311 189L517 61L628 215.5L600 132.5L666 77L943.295 295L833 184L943.295 116L1172 275L1121 227L1214 189L1298 248L1317 258.5L1214 177Z" fill="#DCEFFA"/></g><defs><clipPath id="clip0"><rect width="1280" height="311" fill="white"/></clipPath></defs></svg>
35+
</div>
36+
</div>
37+
<div>
38+
<?php foreach ($planets as $planet) : ?>
39+
<div class="bg-white shadow-md rounded-lg p-6 m-4" style="width: 300px">
40+
<h2 class="text-xl font-semibold"><?= $planet['name'] ?></h2>
41+
</div>
42+
<?php endforeach;
43+
44+
?>
45+
</div>
46+
</body>
47+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
service: app
2+
3+
provider:
4+
name: aws
5+
region: us-east-1
6+
environment:
7+
MONGODB_URI: ${env:MONGODB_URI}
8+
9+
plugins:
10+
- ./vendor/bref/bref
11+
- ./vendor/bref/extra-php-extensions
12+
13+
functions:
14+
api:
15+
handler: index.php
16+
description: ''
17+
runtime: php-83-fpm
18+
timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
19+
events:
20+
- httpApi: '*'
21+
layers:
22+
- ${bref-extra:mongodb-php-83}
23+
24+
# Exclude files from deployment
25+
package:
26+
patterns:
27+
- '!node_modules/**'
28+
- '!tests/**'

docs/tutorial/serverless.txt

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
BrefPHP allows to deploy PHP applications on AWS Lambda.
19+
20+
Codecs are used to decode BSON documents into PHP objects, and encode PHP objects into BSON documents. In contrast to
21+
other methods (e.g. type maps), codecs allow for greater customization and handling of different data types. They
22+
separate logic for BSON encoding and decoding from the domain classes, which also enables BSON to be decoded into plain
23+
old PHP objects.
24+
25+
Setup Bref deployment with mongodb extension
26+
--------------------------------------------
27+
28+
Follow the Bref setup tutorial to install serverless and configure AWS credentials.
29+
https://bref.sh/docs/setup
30+
31+
Init a new application
32+
https://bref.sh/docs/default/getting-started
33+
34+
35+
mkdir bref-mongodb-app
36+
cd bref-mongodb-app
37+
composer init
38+
composer require bref/bref bref/extra-php-extensions
39+
vendor/bin/bref init
40+
41+
Replace the contents of ``index.php`` with the following:
42+
43+
.. literalinclude:: /examples/serverless-bref/index.php
44+
:language: php
45+
46+
47+
Bref provide a Lambda layer with PHP and some very common extensions. Additional extensions are provided
48+
by the package [bref/extra-php-extension](https://github.com/brefphp/extra-php-extensions).
49+
50+
```yaml
51+
plugins:
52+
- ./vendor/bref/bref
53+
- ./vendor/bref/extra-php-extensions
54+
55+
functions:
56+
api:
57+
handler: index.php
58+
runtime: php-83-fpm
59+
layers:
60+
- ${bref-extra:mongodb-php-83}
61+
```
62+
63+
Deploy the application
64+
65+
serverless deploy
66+
67+
The application will not work unless you provide a working MONGODB_URI variable.
68+
69+
AWS Credentials
70+
---------------
71+
72+
Atlas supports passwordless authentication with AWS credentials.
73+
74+
Get the role of the deployed AWS Lambda.
75+
76+
Set Up unified AWS Access
77+
https://www.mongodb.com/docs/atlas/security/set-up-unified-aws-access/
78+
79+
1. Open the lambda function in AWS console
80+
2. In "Configuration > Permission", copy the "Role name"
81+
3. Open the Atlas project
82+
4. Go to "Security > Database Access"
83+
5. Click "Add a new Database User"
84+
6. Select Authentication Method: "AWS IAM", type "IAM Role" and paste the role name in "AWS Role ARN".
85+
7. Add "Built-in Role": "Read and write any database"
86+
8 Validate by clicking on "Add user".
87+
88+
89+
Update the serverless.yml file to pass the environment variable ``MONGODB_URI``
90+
91+
```yaml
92+
provider:
93+
environment:
94+
MONGODB_URI: ${env:MONGODB_URI}
95+
```
96+
97+
The value can be found in "Atlas > Deployment > Database > Connect". Select "3. AWS IAM".
98+
You can remove the ``<AWS access key>:<AWS secret key>`` part from the URI, the
99+
AWS SDK embedded in the MongoDB PHP Extension will request the metadata endpoint
100+
to retrieve the credentials.
101+
102+
```
103+
MONGODB_URI=mongodb+srv://cluster0.abcdefg.mongodb.net/ serverless deploy
104+
```
105+

0 commit comments

Comments
 (0)