Let’s build a serverless contact form!
What am i going to be using?
- KOR Framework
- AWS SES
- IAM
This is a continuation of one of my previous posts “Building a Serverless Vue.js Front End in just a few minutes”. I thought it would be useful to make the contact form at the bottom of the page actually work, and why not do it in a serverless way?
Step 1
First I need to log into my KOR account. I will start by uploading a repository where I have my Lambda Function code. It looks like this:
index.js
const sendEmail = require('./sendEmail');
exports.handler = async (event, context) => {
return sendEmail(event.name, event.subject, event.message, event.email)
.then((response) => {
context.done(null, 'Email Sent');
})
};
sendEmail.js
const AWS = require('aws-sdk');
const ses = new AWS.SES();
function sendEmail(name, subject, message, senderEmail) {
const params = {
Destination: {
ToAddresses: [
'rodrigo@kor-test.com'
]
},
Message: {
Body: {
Text: {
Data: name + message,
Charset: 'UTF-8'
}
},
Subject: {
Data: subject,
Charset: 'UTF-8'
}
},
Source: 'rodrigo@kor-test.com,
ReplyToAddresses: [senderEmail]
}
return ses.sendEmail(params).promise();
}
module.exports = sendEmail;
The original code I modified can be found here: https://wanago.io/2020/08/31/aws-lambda-contact-form-simple-email-service/ ❤️
After cloning my function, I can confirm that everything is fine by clicking any file inside and inspecting it. All good here.
Step 2: Permissions
Now I need to enable the SES permitions into the role that KOR controls. This is very easy to do. Inside my AwesomeNuts habitat, I click on settings and then on Habitat Roles.
I see there that KOR-TEMPLATE-1606783402106 handles the resources of my habitat.
Next, I need to go to AWS IAM roles section and search for the habitat role name.
Good, now inside the role I will find the KOR policy attached.
Now I need to edit the policy and add the following action:
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
},
For the sake of the test, I will leave the resource with *.
Step 3: AWS SES
Ok, before I proceed I will add a quick step to verify my email on AWS SES. This is the same email that you can see inside the lambda function code. On AWS SES, I will start by verifying the domain:
Since my domain is registered on AWS, I recieved the prompt to add the Route53 records.
After a couple of minutes, the domain was verified.
Now I just needed to go to Email Addresses, add my email and confirm it via the email they sent to me. All right, we are ready to move on to the fun stuff.
Step 4: Creating the Function
I’ll go back to my AwesomeNuts Habitat on KOR - Settings - Functions - New Function.
Here I will select my LambdaContact repository, the role, put the handler and select Nodejs 12.
Deploying the Function
The next step is going to Releases and click the “Deploy All” button.
If I want to test the function I can click on the yellow “Test Function” button and do the following:
After selecting RequestResponse, I put this payload and click invoke:
{
"email": "some@email.com",
"subject": "testing2",
"message": "Hello my friend"
}
Final Step, Wrapping it all up
Ok, I have the function, the permissions, and SES working, but there is still one piece missing. How am I going to send information from my website?
For this, I need to link my lambda with an API Gateway and create a POST method.
Thankfully, with KOR this is extremely easy to do. In my habitat settings, I will create a new API Gateway which will take care of all the hard work for me.
I named my API Gateway “ContactForm”. Then after creating it, I can add an endpoint to it. I will create a POST method. The nice thing here is that I can select the function that will be triggered by the method. In my case it is the LambdaContact.
Awesome. Let’s take a look at the result:
Ok so after this, I have my Endpoint ready to be used on my code. Let’s give it a try on Postman:
Now I can use that endpoint inside my Vuejs website. But that is for another post.
Thanks for reading!