2
2
Deploy to AWS Lambda with Bref
3
3
==============================
4
4
5
- .. default-domain:: mongodb
6
-
7
5
.. contents:: On this page
8
6
:local:
9
7
:backlinks: none
@@ -13,61 +11,75 @@ Deploy to AWS Lambda with Bref
13
11
Overview
14
12
--------
15
13
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.
17
15
In this tutorial, you will deploy a simple PHP application with the MongoDB PHP extension,
18
16
and connect to an Atlas cluster using AWS IAM authentication.
19
17
20
18
Prerequisites
21
19
--------------
22
20
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.
25
28
26
29
Install the MongoDB extension
27
30
-----------------------------
28
31
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.
31
35
32
36
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
35
40
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
37
44
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.
42
46
47
+ .. code-block:: none
43
48
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
46
50
47
- .. code-block:: bash
48
51
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.
50
55
56
+ .. code-block:: none
51
57
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:
54
66
55
67
.. code-block:: yaml
56
68
57
- plugins:
58
- - ./vendor/bref/bref
59
- - ./vendor/bref/extra-php-extensions
69
+ plugins:
70
+ - ./vendor/bref/bref
71
+ - ./vendor/bref/extra-php-extensions
60
72
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}
67
79
68
80
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/>`__.
71
83
Replace the contents of ``index.php`` with the following:
72
84
73
85
.. literalinclude:: /examples/aws-lambda/index.php
@@ -76,9 +88,9 @@ Replace the contents of ``index.php`` with the following:
76
88
77
89
Deploy the application
78
90
79
- .. code-block:: bash
91
+ .. code-block:: none
80
92
81
- serverless deploy
93
+ $ serverless deploy
82
94
83
95
84
96
The application will not work unless you define the ``MONGODB_URI`` environment variable.
@@ -90,33 +102,40 @@ Atlas supports passwordless authentication with AWS credentials. In any Lambda f
90
102
AWS sets environment variables that contains the access token and secret token with
91
103
the role assigned to deployed function.
92
104
93
- Set up `unified AWS Access <https://www.mongodb.com/docs/atlas/security/set-up-unified-aws-access/>`__:
94
-
95
105
1. Open the Lambda function in the AWS console
96
106
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"
106
108
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.
108
112
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.
110
115
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/>`__.
114
118
119
+ .. note::
115
120
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 of you in "Atlas > Deployment > Database > Connect". Select "3. AWS IAM".
117
125
Remove the ``<AWS access key>:<AWS secret key>`` part from the URI, the credentials
118
126
will be read from environment variables.
119
127
120
- .. code-block:: bash
128
+ Update the ``serverless.yml`` file to pass the environment variable ``MONGODB_URI``.
129
+
130
+ .. code-block:: yaml
131
+
132
+ provider:
133
+ environment:
134
+ MONGODB_URI: "mongodb+srv://cluster0.example.mongodb.net/?authSource=%24external&authMechanism=MONGODB-AWS&retryWrites=true&w=majority"
135
+
136
+ Finally, deploy with the new configuration. Once done you can access the function URL
137
+ and see the list of planets from your Atlas cluster.
138
+
139
+ .. code-block:: none
121
140
122
- serverless deploy
141
+ $ serverless deploy
0 commit comments