Serverless, Reactphp, and Expanding Frontiers
Serverless, Reactphp, and Expanding Frontiers
Serverless, ReactPHP,
and Expanding Frontiers
ALSO INSIDE
Education Station: Internal Apparatus: finally{}:
Data Structures, Part One Memory Abstractions The State of PHP
Community Corner: Security Corner:
Philosophy and Burnout, Access Control and
Part One Authorization
DaTA
M ay 3 1 s t • ONLINE
Join us online for the next Day Camp 4 Developers and learn to tame your data. Turn it from
a wandering stream to a powerful tool that can inform your enterprise.
daycamp4developers.com
Tickets include attendance to the live event as well as access to download the videos within seven days after the event.
FEATURE
Use your Access key ID and the Secret access key at the Output 1
prompts, set your region to us-east-1, and use JSON for your
output format. 1. $ vendor/bin/bref inito
2.
Now that we have AWS set up, we can write our first 3. What kind of lambda do you want to create? (You will be
Lambda function. 4. able to add more functions later by editing
5. `template.yaml`) [PHP function]:
Hello World With Bref 6. [0] PHP function
7. [1] HTTP application
To create a Lambda function using Bref, we start with a new
8. [2] Console application
directory and install Bref into it: 9. > 0
10.
$ mkdir bref-hello-world 11. Creating template.yaml
$ cd bref-hello-world
12. Creating index.php
$ composer require mnapoli/bref
13.
14.
These commands installed the Bref code into the vendor/ 15. [OK] Project initialized and ready to test or deploy.
folder, so we can go ahead a create the project now. You’ll see
something like Output 1
Listing 2
Bref supports three different kinds of serverless projects
depending on your needs. For our case, we want a standard 1. AWSTemplateFormatVersion: '2010-09-09'
Lambda function, so we select the [0] PHP function option, 2. Transform: AWS::Serverless-2016-10-31
and then Bref creates our files. To learn more about the 3. Description: ''
options, see the Bref documentation on runtimes7 4.
5. Resources:
What Files Do We Have? 6. MyFunction:
7. Type: AWS::Serverless::Function
bref init has created two files of interest: template.yaml 8. Properties:
and index.php. Starting with template.yaml, we have the SAM 9. FunctionName: 'my-function'
template which is the configuration file which defines our 10. Description: ''
application and in our case contains the definition of our first 11. CodeUri: .
12. Handler: index.php
Lambda function (see Listing 2). 13. Timeout: 10 # Timeout in seconds
14. MemorySize: 1024 # Relates to pricing and CPU power
15. Runtime: provided
16. Layers:
17. - 'arn:aws:lambda:us-east-1:209497400698:layer:php-73:1'
7 runtimes: https://bref.sh/docs/runtimes/
The template file consists of a small header and the Resources Deployment is done using the sam tool; however, first, we
section which holds our function. Its resource name isMyFunc- need to create an S3 bucket to store the CloudFormation
tion and it has a type of AWS::Serverless::Function. To define stack in.
our function’s properties, we provide a set of configuration
information under the Properties key. The table below shows $ aws s3 mb s3://helloworld-rka-brefapp
the key properties for a Lambda function.
You can name your bucket anything you like, but it must
be globally unique. I like to postfix with my initials and the
Property name Value Notes reason for the bucket to ensure it’s unique and that I can
The name of the Lambda
remember what it’s for.
function. Note that this is There are two steps to deploying our application. First, we
FunctionName my-function not the same same name upload the code and generate a CloudFormation stack into
as the CloudFormation our S3 bucket and then we deploy the stack:
resource name (MyFunction).
CodeUri . The path to the source code. $ sam package --output-template-file .stack.yaml \
--s3-bucket helloworld-rka-brefapp
The PHP file containing the $ sam deploy --template-file .stack.yaml \
Handler index.php lambda function the Bref --capabilities CAPABILITY_IAM \
runtime layer will invoke. --stack-name helloworld-rka-brefapp
$ rm .stack.yaml
The language runtime that
will invoke the function. For The output template file (.stack.yaml) is an intermediate
Runtime provided Bref, this isprovidedas we
CloudFormation file and isn’t needed after deploy. The stack
provide the runtime layer to
Lambda.
name must be unique within the AWS region, so I name it the
same as my S3 bucket.
By default this is Bref ’s PHP
7.3 runtime layer from the Running Our Function
us-east-1 region. See this list
Layers A List of layers To invoke our function, we can use bref.
https://runtimes.bref.sh for
the correct ARNs for other
$ vendor/bin/bref invoke my-function
regions and PHP versions. START RequestId: 4fa0f083-c02f-4b35-a23f-1e5e35d91af5
Version: $LATEST
As you can see, our template defines a single resource: a END RequestId: 4fa0f083-c02f-4b35-a23f-1e5e35d91af5
Lambda function called my-function that lives in index.php REPORT RequestId: 4fa0f083-c02f-4b35-a23f-1e5e35d91af5
within the current directory. Let’s take a look at index.php: Duration: 24.15 ms Billed Duration: 100 ms
Memory Size: 1024 MB Max Memory Used: 68 MB
<?php declare(strict_types=1);
"Hello world"
require __DIR__ . '/vendor/autoload.php';
To send data to our function we can pass in a JSON object
lambda(function (array $event) {
return 'Hello ' . ($event['name'] ?? 'world'); to the--eventparameter like this:
});
$ vendor/bin/bref invoke my-function \
A Bref Lambda function is defined as a closure that’s an --event '{"name": "Rob"}'
argument to Bref ’s lambda() function. It takes an array $event
Which results in the output of Hello Rob.
which contains the input data to the function, and you can
return whatever you want as long as it’s JSON serializable. Tidying Up
This $event contains information about the request which We can delete the application using:
triggered your lambda function8. In this case, we return the
string Hello followed by the name if it exists, otherwise world. aws cloudformation delete-stack \
--stack-name helloworld-rka-brefapp
Deploying Our Function
If you used the us-east-1 region for your configuration as We also need to delete the S3 bucket with:
recommended, then we can go ahead and deploy our func-
aws s3 rb s3://helloworld-rka-brefapp
tion immediately.
Related Reading
9 SAM documentation:
https://phpa.me/sam-event-types • Moving a Monolith to AWS by Keanan Koppenhaver. May 2018.
10 Schedule Expressions for Rules: https://phparch.com/article/moving-a-monolith-to-aws/
https://phpa.me/cloudwatch-events-rules • Community Corner: What’s the Fuss About Serverless?
11 Nineteen Feet Limited: http://19ft.com by James Titcumb. April 2018.
12 akrabat.com: https://akrabat.com https://phpa.me/april-2018-serverless