AWSLambdaAPI GatewayDynamoDBSES | 10 Min Read
Handling Contact Forms Submissions With a Custom REST API using AWS SES, API Gateway, and Lambda
Learn how to build a REST API to handle contact form submissions using AWS SES, Lambda, and API Gateway via the AWS CDK. And, how to test it using Postman.
When it comes to building contact forms for a website, you could use a paid service to handle the sending of form submissions to you but in a world where everything is going subscription based do you really want to add one more to your list?
So, in this tutorial, we’ll be exploring how to build a REST API that can process requests and send the contents of those requests as an email to a designated email address. You would then be able to take the URL of this API and make requests to it from a frontend application and allow users to submit contact forms to then be delivered to your inbox. I’ll demonstrate this functionality by using Postman to send requests to our API.
For this tutorial, I’ll be using the AWS CDK but if you would rather complete this project using the AWS dashboard, then make sure to check out my past tutorial on this, it also includes a frontend project example for building a contact form.
The AWS services we’ll be using in this tutorial are:
- API Gateway: For creating our REST API and API key for authentication
- Lambda: For creating a function to handle the processing of requests and request the sending of emails via SES
- SES: Sending the emails and configuring our email address to send the emails
- IAM: To give permission to our Lambda function to allow sending emails using SES.
Now, we know what we’ll be building and what we’ll be building it with we’re almost ready to get started. But, before we get into the tutorial, I just want to mention that you’ll need to have an AWS account created as well as have the AWS CDK and CLI configured on your machine, along with having an existing CDK project. You can learn how to do all of this in under 60 seconds with my tutorial on TikTok here.
Configuring SES to Send Emails
The first thing we need to do in our CDK project is define the email address we want to send our form submissions from, this is called a verified email identity. To create this verified identity, add the below code to our stack definition file in the `lib`
directory; make sure to update `YOUR_EMAIL_ADDRESS`
to the email address you want to send emails from.
1// 1. Define our SES Verified Email Address2const verifiedEmail = "YOUR_EMAIL_ADDRESS";3const identity = Identity.email(verifiedEmail);4new EmailIdentity(this, "SESIdentity", {5 identity,6});
tsAdding Our Lambda Processor Function
With SES now configured and ready to go, let’s shift our focus to the Lambda function we’ll be using to process requests from our API and then request the sending of emails with SES. The first thing we want to do is define our Lambda function in our stack definition file, so just below the SES code, add the code below for our Lambda function.
1// 2. Create our Lambda functions to handle requests2const sendEmailLambda = new NodejsFunction(this, "SendEmailLambda", {3 entry: "resources/send-email.ts",4 handler: "handler",5 environment: {6 VERIFIED_EMAIL: verifiedEmail,7 },8 initialPolicy: [9 new PolicyStatement({10 actions: ["ses:SendEmail"],11 resources: [12 `arn:aws:ses:${this.region}:${this.account}:identity/${identity.value}`,13 ],14 }),15 ],16});
tsThe two important things to note with this definition are that we pass the email address we configured with SES to our Lambda function as an environment variable so we can access it inside the function. And, secondly, we add a new IAM policy to our Lambda to allow it to send emails using SES with the email address we configured.
With both of those things covered, let’s move on to creating the actual function that our Lambda will execute. To do this, create a new file at `./resources/send-email.ts`
and then inside it add the below code.
1import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";2import { APIGatewayProxyEvent } from "aws-lambda";3
4interface IEventBody {5 firstName: string;6 lastName: string;7 email: string;8 message: string;9}10
11const sesClient = new SESClient({});12
13export const handler = async (event: APIGatewayProxyEvent) => {14 // If no body, return an error15 if (!event.body) {16 return {17 statusCode: 400,18 body: JSON.stringify({ message: "Missing body" }),19 };20 }21
22 const { VERIFIED_EMAIL = "" } = process.env;23
24 // Get data from the request sent from the frontend that triggered the lambda25 const body = JSON.parse(event.body) as IEventBody;26 const { firstName, lastName, email, message } = body;27 const requiredFields = ["firstName", "lastName", "email", "message"];28
29 // Check all of the required fields are present in the body30 for (const key of requiredFields) {31 if (!body[key as keyof IEventBody]) {32 return {33 statusCode: 400,34 body: JSON.stringify({ message: `Missing field: ${key}` }),35 };36 }37 }38
39 // Config for SES to send the email40 const params = {41 // Email address the email is sent to42 Destination: {43 ToAddresses: [VERIFIED_EMAIL],44 },45 Message: {46 // Body of the email47 Body: {48 Text: {49 Data: `50New message:51---52Name:${firstName} ${lastName}53Email: ${email}54Message: ${message}55`,56 },57 },58 // Subject line of the email59 Subject: { Data: `Contact Form Message` },60 },61 // Email address the email is sent from62 Source: VERIFIED_EMAIL,63 };64
65 // Send the email66 try {67 const response = await sesClient.send(new SendEmailCommand(params));68
69 if (response.$metadata.httpStatusCode !== 200) {70 return {71 statusCode: 500,72 body: JSON.stringify({ message: "Error sending email" }),73 };74 }75
76 return {77 statusCode: 200,78 body: JSON.stringify({ message: "Email sent" }),79 };80 } catch (e) {81 return {82 statusCode: 500,83 body: JSON.stringify({ message: e }),84 };85 }86};
tsIn this function, we take the body from the API request and check it contains all of the defined required fields before configuring the parameters required for SES to send our email and finally, sending the actual email.
Inside the lambda, we also do several checks to handle potential errors; for example, the request body not being provided, required fields being missed, or if an error happens with SES sending the email. Each of these will return an error code with a relevant message to our client sending the request.
Finally, it’s worth noting that the email we send from and to is the same email address (the one we verified in SES). However, if you wanted, you could configure the receiving email address to be a different email address by changing the array passed to the `ToAddresses`
property in the SES config. But it’s important we keep the `Source`
property as the email address we verified in SES otherwise, the emails won’t send.
Creating an API to trigger our Lambda function
With our Lambda function now defined and written, we’re ready to move onto the final piece of our CDK stack and that’s defining our new REST API. To define our new REST API, add the below code under the Lambda definition we created earlier in the stack definition file in the `lib`
directory.
1// 3. Define our REST API2const api = new RestApi(this, "EmailApi", {3 restApiName: "EmailApi",4 defaultCorsPreflightOptions: {5 allowOrigins: Cors.ALL_ORIGINS,6 allowMethods: Cors.ALL_METHODS,7 },8 apiKeySourceType: ApiKeySourceType.HEADER,9});10
11// 4. Create our API Key12const apiKey = new ApiKey(this, "EmailApiKey");13
14// 5. Create a usage plan and add the API key to it15const usagePlan = new UsagePlan(this, "EmailUsagePlan", {16 name: "Email Usage Plan",17 apiStages: [18 {19 api,20 stage: api.deploymentStage,21 },22 ],23});24
25usagePlan.addApiKey(apiKey);26
27// 6. Connect our Lambda functions to our API Gateway endpoints28const sendEmailIntegration = new LambdaIntegration(sendEmailLambda);29
30// 7. Define a POST handler on the root of our API31api.root.addMethod("POST", sendEmailIntegration, {32 apiKeyRequired: true,33});
tsIn this code, we define several things, we start by defining our new REST API before then adding a new API key and a usage plan to connect our API and API key together. We then configure a new `LambdaIntegration`
which is how we connect our API and Lambda function. Before, finally, defining the endpoint to which we want users to send their requests along with the Lambda function we want that endpoint to trigger.
In our case, we’ll be using the root of the API with a `POST`
request which will trigger the Lambda function we created earlier for processing the results.
It’s worth mentioning that creating REST APIs with API Gateway and the AWS CDK can be a complicated subject so to help with this I’ve created a standalone tutorial that goes into a lot more depth than this tutorial, you can check it out here.
Deploying Our New API
We’ve now defined all of the services we need for our new REST API to process requests from a contact form and send the results to our target email address. But, before we can deploy our CDK stack to our AWS account, we need to add one more output to our CDK stack for our API key ID. So, at the bottom of your stack definition file add the below code.
1// Misc: Outputs2new cdk.CfnOutput(this, "API Key ID", {3 value: apiKey.keyId,4});
tsWith that code added we’re ready to deploy our CDK stack, so in the terminal run the command `cdk deploy`
and then accept any prompts you’re given. Once your deployment has finished, you should have two outputs in your terminal that look like the below.
1SesSendEmailApiStack.APIKeyID = YOUR_API_KEY_ID;2SesSendEmailApiStack.EmailApiEndpoint2C21ACE3 = YOUR_API_URL;
Now, we have our API key ID we can get our actual API key by using the AWS CLI and running the command `aws apigateway get-api-key --api-key <YOUR_API_KEY_ID> --include-value`
. Then once we have our API key value from the result shown to us in the terminal, we can perform requests to our API using a tool like Postman which is what we’ll be doing in the next section!
Testing Our New API With Postman
Before we can test our new API, we first need to complete the verification process for the email address we configured with SES at the start of the tutorial. To complete this process, check the inbox of the email address you configured for an email from AWS, inside that email should be a link, if you click that link it will complete the verification process and take you to a page confirming its completion.
With our email address now verified, we’re ready to configure Postman to test our new API. To do this, add our API URL to the URL input field in Postman and select the request method of `POST`
as that’s what we configured our API to use.
We’ll then want to configure our API key to be included in the request by adding a header to the request. To do this, under the “Headers” tab in Postman, add a new entry with the key of `x-api-key`
and the value as your API key.
Finally, we need to add a body to our request, to do this, go to the “Body” tab and select the “raw” option before then adding the below JSON (customize the values as you want).
1{2 "firstName": "FIRST_NAME",3 "lastName": "LAST_NAME",4 "email": "EMAIL_ADDRESS",5 "message": "MESSAGE"6}
jsonYou can then hit the send button in Postman and within a few moments, you should receive an email in the inbox of the email address you specified at the start of this tutorial (or, if you changed it, the email address you added to the `ToAddresses`
property instead). You should also receive a response in Postman with a status code of `200`
and a body of `{"message": "Email sent"}`
.
At this point we know our API works but if you would like to test your API further than a successful request, here are some more tests you could run to test some of the failure conditions we handled inside the function.
Test | Result |
---|---|
No API key provided | 403 Forbidden |
API key and no request body provided | 400 - Missing body |
API key and missing required fields | 400 - Missing field: MISSING_FIELD_NAME |
Closing Thoughts
And, if all of the tests passed successfully then congrats you have a working API that you can use to send contact form messages to a target email address. All you need to do now is send requests to your API URL from your frontend with your API key and a body containing the required fields, the rest is handled for you!
So, I hope you found this tutorial on building a REST API to handle contact form submissions using the AWS CDK helpful and if you’d like to see the full example code for this project, you can see it over on my AWS CDK examples repository here.
And, until next time, thank you for reading.
Coner