One drawback of having a static website is the lack of server-side functionality that would permit a contact form. Lucky for us we can easily mediate this by building a microservice using AWS API Gateway, Lambda and SES. The diagram below represents the architecture flow. The user submits information through the “contact form” which will post through an AWS API and be passed to an AWS lambda function. The lambda function will then generate an email and forward it using SES.
The first step is to verify an email address that will be used to send and receive emails.
- Navigate to Simple Email Service in AWS console and click email addresses
- Click Verify a New Email Address and enter the email address you would like to use
- Click Verify This Email Address
- You should then navigate to your inbox and click the link in the verification email to confirm your email address
- Navigate to Lambda on AWS console and click Create function
- Choose author from scratch
- Give your function a name, choose Node.js 6.10
- For Role choose Create a new role from one or more templates and give the new role a name
- Finally click Create function
Before we can use our Lambda function, we need to configure the newly created Lambda role and attach a policy that will allow it to send emails through SES. This can be done via IAM.
- Navigate to IAM from the AWS Console
- Click Policies and click Create Policy
- Select Create Your Own Policy and name it contact-form-send-policy
- Configure the policy document as follows and click create policy
Once this policy is created, navigate to roles in IAM and attach this policy to your newly created Lambda role.
Navigate back to Lambda from the AWS Console and click on the lambda function. In the inline editor clear out the code that’s there and paste in the code below. Make sure to replace your-verified-ses-email with your verified email in the code below
After copying and pasting the code your window should look similar to the screenshot above. The last step is to create the API Gateway trigger that will activate lambda function.
- Click on API Gateway from the triggers list to add it to the lambda function.
- You should now see a Configure triggers section to fill out
- Choose Create a new API, Open for security, name the API “Contact“ and then click add
- Finally click save to save changes to our lambda function
To make sure our lambda function is accessible and our API Gateway works properly we can test it.
- Navigate to API Gateway from the AWS Console and click on “contact“ the newly created API
- Click “ANY“ under the resources column and then click TEST with the lightening bolt symbol
- Choose POST for Method and for the Request Body copy and paste in the following:
"subject": "test from apigw",
"message": "this is a test message"
- Click Test
On the right hand side, you’ll see the request flow of API Gateway calling our Lambda function. If you get a Method completed with status: 200 awesome it worked and you should check your email. But if you get a Method completed with status: 500 oh no something is wrong, time to go back and double check everything.