Skip to content

A sample application to deploy a Node Email API using AWS Lambda and API Gateway

Notifications You must be signed in to change notification settings

mattpodolak/email-api-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Email API using Node with API Gateway and AWS Lambda

This sample application is a Lambda function that processes events from an API Gateway REST API. The API provides a public endpoint that you can access with a web browser or other HTTP client. When you send a request to the endpoint, the API serializes the request and sends it to the function. The function uses the nodemailer package to send out an email to a set Gmail address. This is useful for websites which incorporate a contact form.

⚠️ The application creates a public API endpoint that is accessible over the internet. When you're done testing, run the cleanup script to delete it.

Architecture

The project source includes function code and supporting resources:

  • function - A Node.js function.
  • template.yml - An AWS CloudFormation template that creates an application.
  • 0-run-tests.sh, 1-create-bucket.sh, 2-deploy.sh, etc. - Shell scripts that use the AWS CLI to deploy and manage the application.

Use the following instructions to deploy the email application.

Requirements

If you use the AWS CLI v2, add the following to your configuration file (~/.aws/config):

cli_binary_format=raw-in-base64-out

This setting enables the AWS CLI v2 to load JSON events from a file, matching the v1 behavior.

Local Testing

Create a file named setEnvVars.js in the jest folder with the required environment variables USER and PASS, these values are from the Gmail account you will be sending emails from:

process.env.USER = 'sample@email.com'
process.env.PASS = 'password'

To test the email function locally using Jest, run 0-run-tests.sh.

email-api-lambda$ ./scripts/0-run-tests.sh

Setup

Download or clone this repository.

$ git clone https://github.com/mattpodolak/email-api-lambda.git
$ cd email-api-lambda

To create a new bucket for deployment artifacts, run 1-create-bucket.sh.

email-api-lambda$ ./scripts/1-create-bucket.sh

Deploy

To deploy the application, run 2-deploy.sh.

email-api-lambda$ ./scripts/2-deploy.sh

This script uses AWS CloudFormation to deploy the Lambda functions and an IAM role. If the AWS CloudFormation stack that contains the resources already exists, the script updates it with any changes to the template or function code.

Manual Lambda Configuration

Set ENV Variables

In the AWS Lambda Console, select your function that was deployed and select "Manage environment variables", add both the USER and PASS variables for the Gmail account that will send the emails.

Create Node Modules Layer

To create a layer which incorporates the required node_modules for the email application, run 3-create-layer.sh.

email-api-lambda$ ./scripts/3-create-layer.sh

Send the nodejs folder to a zip file named nodejs.zip.

In the AWS Lambda Console, navigate to Layers and select "Create Layer".

Name your layer and upload the nodejs.zip file you created. Select the Node.js 12.x Runtime, and click "Create".

Create Layer

Add Layer to Function

Navigate back to your Lambda function in the AWS Console. Select "Layers" in the Designer area.

Layers

In the Layers section which appears, select "Add a layer".

Add a layer

Select "Custom layers" and find the layer you have just created, select the "Version" and click Add. Your Lambda function can now use the Node modules that are installed in the layer you have just added.

Test

To invoke the function directly with a test event (event.json), run 4-invoke.sh.

email-api-lambda$ ./scripts/4-invoke.sh

Let the script invoke the function a few times and then press CRTL+C to exit.

To invoke the function with the REST API, run the 5-post.sh script. This script uses cURL to send a GET request to the API endpoint.

email-api-lambda$ ./scripts/5-post.sh

The application uses AWS X-Ray to trace requests. Open the X-Ray console to view the service map.

Cleanup

To delete the application, run 6-cleanup.sh.

email-api-lambda$ ./scripts/6-cleanup.sh

If you are using Git Bash, the command to delete the log group will fail because / is a special mount. Add MSYS_NO_PATHCONV=1 to the front of the aws logs command in 6-cleanup.sh: MSYS_NO_PATHCONV=1 aws logs delete-log-group --log-group-name /aws/lambda/$FUNCTION

Future Consideration

  • Automated layer creation for Lambda Function
  • Automated environment variable creation
  • Use Terraform instead of CloudFormation for IaC

About

A sample application to deploy a Node Email API using AWS Lambda and API Gateway

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published