Skip to content

Sample code for SES/S3 and HTTP API Gateway integrations on Amazon Lambda, both with and without the help of https://bref.sh; companion code to PHP on Lambda with Custom Runtimes presentation

Notifications You must be signed in to change notification settings

iansltx/spongebot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SpongeBot

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.

Basic Setup

  1. composer install
  2. 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).
  3. 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.
  4. Zip the contents outside runtime, omitting the rie directory, and upload to the function.

**NOTE: The contents of runtime were generated by pulling the master branch of brefphp/aws-lambda-layers, running make, then copying output/php-83, with custom bootstrap and bootstrap.php files.

Email handler

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.

  1. 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.
  2. 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.
  3. Add S3 "all object create events" to the above bucket as a trigger for the Lambda function.
  4. Send your bot an email!

API

  1. 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.
  2. 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.
  3. Make note of, and change if you like, the Resource path on the routes screen, then click Next.
  4. Click Next, then Create; the default deployment stage is good enough for what we want to do.
  5. 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.

About

Sample code for SES/S3 and HTTP API Gateway integrations on Amazon Lambda, both with and without the help of https://bref.sh; companion code to PHP on Lambda with Custom Runtimes presentation

Resources

Stars

Watchers

Forks