GitHub Source
Prerequisites
- AWS Account
- Composer
- PHP
- VS Code IDE
Create a Directory
Create a directory with your project name.
$cd phpLambdaFunction
composer.json
Make sure to install composer software on your local machine. The following composer code contains bref/bref.
Install Plugins
Use the following command to download the PHP bref dependencies.
Loading composer repositories with package information
Updating dependencies
Lock file operations: 10 installs, 0 updates, 0 removals
- Locking bref/bref (2.0.8)
- Locking crwlr/query-string (v1.0.3)
- Locking hollodotme/fast-cgi-client (v3.1.7)
- Locking nyholm/psr7 (1.8.0)
- Locking psr/container (2.0.2)
- Locking psr/http-factory (1.0.2)
- Locking psr/http-message (1.1)
- Locking psr/http-server-handler (1.0.2)
- Locking riverline/multipart-parser (2.1.1)
- Locking symfony/process (v6.3.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
3 packages you are using are looking for funding.
validateEmail.php
Here the following function validates input email MX domain records.
Output
validateEmail('[email protected]'); // FALSE
validateEmail('srinivas.tamada'); // FALSE
index.php
Create a main index file and include the validateEmail and autoload.php will load the bref dependencies. Here the following return function reads the post-request data and responds with a valid status code.
Project File Structure
Here compose install the vendor dependency folder and created a lock file.
Step 1: Create a Lambda Function
You have the launch the AWS Lambda and you find an option to create a function. You have to give the function name and choose the following runtime.
Step 2: Advance Settings
Recommended setting for Lambda function. But the following options support AWS_IAM authentication.
For testing you can choose NONE to enable the function url and hit create function button. We can turn off the latter.
Step 3: Function Overview
Here you will the function url with ARN details for AWS_IAM. Now click on the layers.
Step 4: Layers
Layers will add additional support to the AWS lambda function.
Step 5: Add a Layer
Here you have to define the ARN(Amazon Resource Name) URL. Choose to specify an ARN option.
Step 6: Select Bref Runtimes
Open a new browser tab and launch runtimes.bref.sh. Here you will find the PHP layer versions. Select the better PHP version, based on your function needs.
Step 7: Update Layer ARN
Paste the layer url and hit add button.
Step 8: Layer Update
Now you will the layer got added.
Step 9: Code Source
Scroll down and find the default code source.
Step 10: Upload your PHP code
Select upload from and choose from .zip file.
Step 11: Create a PHP Project Zip file
Go to your PHP project directory and use the following command to zip the source code.
You will find the ZIP file here.
Step 12: Upload the index.zip
Upload index.zip file and click save button.
Step 13: PHP Code Source
Now you will find the PHP email validation source code.
Step 14: Runtime Setting
Scroll down and find the runtime settings. You have to update the handler default hello.handler to index.php
Step 15: Update Runtime Handler with index.php
Update with index.php and choose runtime Amazon Linux 2.
Step 16: Function URL
You will find the function url.
Step 17: Postman or Thunder Client
You need a restful api client software to execute endpoints. If you are using VS Code, install the Tunder Client extension.
Step 19: Test the PHP Lambda Function Positive
PHP restful endpoint is ready. Use the function url and post request the email in JSON format. Here you will notice the response status TRUE with 200
Step 20: Test the PHP Lambda Function Negative
Here you will notice the response status False with 500 for an invalid email domain.
Function ARN
AWS is recommending to use of function ARN with AWS_IAM. I will explain in my upcoming article.
Update and deploy
Use the code source editor and update the application code for any modification. Once ready just click on deploy for changes.
Disable Function URL
I would recommend disabling the function URL. It will charge per request once the free trial expires. Go to configuration and select Function Url edit the options.
Change it back to AWS_IAM.
Function URL Forbidden
Function URL should respond back with forbidden access.
Delete Lambda Function
If you are not using it, better to delete the function. It will not add any additional cost.
Monitor Function Logs
AWS Lambda is offering a dashboard to monitor function logs.
0 comments: