An example of a PHP-based Lambda function, from scratch...ish (runtime is based on Bref).
If you're looking for an example using Bref and Serverless Framework, switch to the bref2
branch.
composer install
- Zip the contents of
runtime
and upload as a Lambda Layer; specify that it's compatible with Amazon Linux 2 custom runtimes (AL2023 won't work at this time). - Create a Lambda function on Amazon Linux 2 with enough permissions to send SES emails, read from S3, and do normal Lambda activities. An IAM role including policies of AmazonS3ReadOnlyAccess, AmazonSESFullAccess, and AWSLambdaBasicExecutionRole is overkill but will get the job done.
- Zip the contents outside
runtime
, omitting therie
directory, and upload to the function.
**NOTE: The contents of
runtime
were generated by pulling the master branch ofbrefphp/aws-lambda-layers
, runningmake
, then copyingoutput/php-83
, with custombootstrap
andbootstrap.php
files.
NOTE: The direct SES -> Lambda integration doesn't provide message content, so you can't use that integration for this example. If all you need is e-mail headers, the Lambda integration will work fine, and provides headers as part of the JSON-encoded event body, so there's no need to use a MIME parser.
- Set up SES, including domain verification + MX records. This may take a bit to verify, and you'll need to open a support ticket to actually be able to send email to anything other than hand-verified email addresses.
- Add an SES rule to push inbound messages from your desired email address to an S3 bucket. It's easier to get this set up if you create the bucket within the rule creation process.
- Add S3 "all object create events" to the above bucket as a trigger for the Lambda function.
- Send your bot an email!
- Under AWS's API Gateway UI, click Build on the HTTP API card; you'll have to click Create API to see that card if you already have API gateways set up.
- Click Add Integration, select an Integration type of Lambda, then select your function as the integration target. Specify an arbitrary name for the API, then click Next.
- Make note of, and change if you like, the Resource path on the routes screen, then click Next.
- Click Next, then Create; the default deployment stage is good enough for what we want to do.
- Hit the Invoke URL, plus the route you set in step 3, in a web browser. Optionally, provide the "message" value in the query string.