Skip to content

DOCSP-47690: aws lambda tutorial #207

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

Merged
merged 7 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions source/aws-lambda.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
.. _php-aws-lambda:

====================
Deploy to AWS Lambda
====================

.. facet::
:name: genre
:values: tutorial

.. meta::
:keywords: serverless, deployment, code example, live

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

Overview
--------

In this guide, you can learn how to use `Bref
<https://bref.sh>`__ to deploy serverless PHP applications to AWS
Lambda. This guide demonstrates how to deploy a PHP application built by
using the {+library-short+} and connect to an Atlas cluster by using AWS
IAM authentication.

Before You Get Started
----------------------

Before you can deploy to AWS Lambda by using Bref, you must set up the
following components:

- AWS account and access keys
- `Serverless Framework <https://www.serverless.com/>`__

The `Setup <https://bref.sh/docs/setup>`__ tutorial in the Bref
documentation describes how to prepare these components.

Install Dependencies
--------------------

Bref uses `Lambda layers
<https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html>`__ to
provide the PHP runtime. The ``bref`` layer integrates Bref into your
application and is compiled with PHP and a few other extensions. You can
install the other necessary extensions, such as ``mongodb``, in other layers.

The following commands create a new project directory and install the
MongoDB and Bref dependencies:

.. code-block:: bash

mkdir bref-mongodb-app && cd bref-mongodb-app
composer init
composer require bref/bref bref/extra-php-extensions mongodb/mongodb

Then, initialize the serverless configuration by using the ``bref``
command:

.. code-block:: bash

vendor/bin/bref init

After the commands complete, your project contains the following files:

- ``composer.json``: Lists PHP dependencies installed in the ``vendor`` directory
- ``index.php``: Defines a sample webpage
- ``serverless.yml``: Configures the deployment

Add the MongoDB Extension to Your Configuration
-----------------------------------------------

After you initialize the project, you can add the ``mongodb`` extension.
Locate the ``Serverless config`` name in the list of extensions provided
by the :github:`bref/extra-php-extension <brefphp/extra-php-extensions>`
package. Add it to the ``layers`` of the function in the ``serverless.yaml``
file, as shown in the following code:

.. code-block:: yaml

plugins:
- ./vendor/bref/bref
- ./vendor/bref/extra-php-extensions # Adds the extra Serverless plugin

functions:
api:
handler: index.php
runtime: php-83-fpm
layers:
- ${bref-extra:mongodb-php-81} # Adds the MongoDB layer

Customize the Sample Application
--------------------------------

This step explains how to create a web page that list planets from the
Atlas :atlas:`sample data </sample-data>`. Replace the contents of
``index.php`` with the following code:

.. literalinclude:: /examples/aws-lambda/index.php
:language: php

Redeploy the application with the new ``index.php`` by running the
following command:

.. code-block:: bash

serverless deploy

The application page displays an error message because the ``MONGODB_URI``
environment variable is not set. The following section explains how to
set your connection string in an environment variable.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conflicted on this – I think it's helpful to tell readers to redeploy using the serverless deploy command once they update their index file, but I'm also unsure of telling readers to redeploy into a situation where they'll receive an error. You do have a section at the end that instructs readers to redeploy, so I think I lean towards cutting this part out. Let me know what you think!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed- can remove

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part got left in, just checking you didn't mean to delete it instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird , yes meant to delete


Set AWS Credentials
-------------------

Atlas supports passwordless authentication when using AWS credentials.
In any Lambda function, AWS sets environment variables that contain the
access token and secret token for the role assigned to deploy the function.

The following steps demonstrate how to set the role in your Atlas
cluster:

1. Open the Lambda function in the AWS console.
#. Navigate to :guilabel:`Configuration > Permission`, then copy the
:guilabel:`Role name`.
#. Add this role to your Atlas cluster in the :guilabel:`Database
Access` section. Select the :guilabel:`AWS IAM` authentication method
and set the built-in role ``Read and write any database``.

To learn how to set up unified AWS access, see :atlas:`Set Up Unified
AWS Access </security/set-up-unified-aws-access/>` in the Atlas documentation.

After you configure the permissions, the Lambda function is allowed to access
your Atlas cluster. Next, configure your application to use the Atlas endpoint.

Access to Atlas clusters is also restricted by IP address. Since the
range of IP addresses that comes from AWS is very wide, you can allow
access from everywhere. To learn how to allow universal access, see
:atlas:`Configure IP Access List Entries </security/ip-access-list/>` in
the Atlas documentation.

.. note::

Using Virtual Private Cloud (VPC) Peering is recommended to isolate
your Atlas cluster from the internet. This requires the Lambda
function to be deployed in the AWS VPC. To learn more, see
:atlas:`Set Up a Network Peering Connection </security-vpc-peering>`
in the Atlas documentation.

Next, copy your connection string and remove the ``<AWS access key>:<AWS
secret key>`` section, as your credentials are read from environment variables.

In your project's ``serverless.yml`` file, set the
``MONGODB_URI`` environment variable to your connection string:

.. code-block:: yaml

provider:
environment:
MONGODB_URI: "<connection string without credentials>"

To learn more about using the ``MONGODB-AWS`` authentication mechanism,
see the :ref:`MONGODB-AWS <php-mongodb-aws>` section of the
Authentication Mechanisms guide.

Deploy Your Application
-----------------------

Finally, deploy with the new configuration:

.. code-block:: bash

serverless deploy

After deployment completes, you can access the URL and see the
list of planets from your collection.
9 changes: 8 additions & 1 deletion source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ MongoDB PHP Library
Monitor Your Application </monitoring>
Security </security>
Specialized Data Formats </data-formats>
Deploy to AWS Lambda </aws-lambda>
Compatibility </compatibility>
What's New </whats-new>
Upgrade </upgrade>
Expand Down Expand Up @@ -111,6 +112,12 @@ Specialized Data Formats
Learn how to work with specialized data formats and custom types in the
:ref:`php-data-formats` section.

Deploy to AWS Lambda
--------------------

Learn how to deploy a PHP application on AWS Lambda by using Bref in the
:ref:`php-aws-lambda` section.

Compatibility
-------------

Expand All @@ -134,4 +141,4 @@ FAQ
---

See answers to commonly asked questions about the {+library-short+} in the
the :ref:`php-faq` section.
the :ref:`php-faq` section.
153 changes: 0 additions & 153 deletions source/tutorial/aws-lambda.txt

This file was deleted.

Loading