These are the Following Steps for creating Create Dynamic Contact Forms for S3 Static Websites Using AWS Lambda, Amazon API Gateway, and Amazon SES.
We are a preferred AWS consultant and offers the best cloud AWS consulting service. Our AWS-certified expert consultants conduct a thorough review and evaluation of your existing IT infrastructure and service interaction model to provide top-notch solutions.
Following is the architecture flow with detailed guidance.
In the above diagram, the customer is submitting their inquiry through a “contact us” form, which is hosted in an Amazon S3 bucket as a static website. Information will flow in three simple steps:
- Your “contact us” form will collect all user information and post to Amazon API Gateway restful service.
- Amazon API Gateway will pass collected user information to an AWS lambda function.
- AWS Lambda function will auto generate an e-mail and forward it to your mail server using Amazon SES.
Having Contact Me like the one below is an integral part of most of the personal and small business websites that are built.
I was building one such website, as the entire website is static, I honestly do not want to set up a server just to expose a single endpoint.
I know that cloud functions are something that solves my problem of having an endpoint without actually setting up the server. I chose AWS Lambda as it was much popular. But, the resources and blogs were not enough to give me a step by step guideline.
We are going to build a node emailer service that accepts a message in our POST request body and triggers an email to the predefined set of recipients from your Gmail account. Setup Amazon SES
- AWS Account Setup.
- Setting up Lambda.
- Uploading the emailer code to your lambda.
- Setup Amazon SES.
- Creating AWS API Gateway
- Intigrate API Gateway with lambda.
Create an AWS account here. Your account setup will be complete with you entering your Credit card details and verifying your email. Charges are usage-based.
- Naviagte to AWS Console.
- Choose Lambda under the Find Services input field.
- You should now be on the lambda functions dashboard which shows the list of your available lambda functions.
- You should now be on the lambda functions dashboard which shows the list of your available lambda functions.
- In the next screen, fill in your Function name - emailer and choose Nodejs runtime as we are implementing this using node.
- Now you can execute and test your AWS lambda function as directed in the AWS developer guide. Make sure to update the Lambda execution role and follow the steps provided in the Lambda developer guide to create a basic execution role.
- Add following code under policy to allow Amazon SES access to AWS lambda function:
- On Clicking the Create function button you should see Successfully created the function emailer message on the next screen.
- On scrolling down the page, you will see a sample nodeJS code with index.js.
- Create a new test with any name of your choice and click on the Test button, you should be getting the response in the Execution Result tab.
The Aws lambda IDE for nodeJS does not allow us to install our npm packages on the go. Due to this, we have to get this set up locally in our machine and then upload the code to lambda by zipping it.
- Download the Zip. It contains the code to be uploaded to your lambda function.
- If you want to create the zip, the content is present inside this repo where there are a nodemailer dependency and some code to send an email. Make sure to npm install and create a zip from the root directory including your node_modules folder.
- Once you got the Zip, upload it to the AWS lambda using Actions -> Upload a .zip file option.
- If you open index.js you should be able to see the code where we have given our Email credentials and sending an email.
- Headers are set to handle CORS errors if you try hitting your lambda from another origin.
Amazon SES requires that you verify your identities (the domains or email addresses that you send email from) to confirm that you own them, and to prevent unauthorized use. Follow the steps outlined in the Amazon SES user guide to verify your sender e-mail.
- Create an API to expose this lambda function as a service.
- Click on Services -> API gateway service from the search bar -> Create API -> REST API -> Build -> API name -> Create.
- You should be on this screen now.
- We need two methods to be created. 1.POST and 2.OPTIONS to handle CORS.
- Actions -> Create Method -> POST -> TICK -> Integration type -> Lambda -> Lambda Function -> emailer -> Save -> OK.
- We need to allow few Headers so that they could be read by the client.
- Method Response -> Expand the Accordion next to 200.
- Add the following headers
- Go to Integration Response -> Expand Accordion -> Header Mappings -> Make the following.
- If you have multiple headers being passed from your API, in order to consume them you have to enable it here.
- You can now do a test from the TEST option -> pass the following in the body.
- Click on Test -> you should get an Email with "HELLO" in the Message.
- Actions -> Deploy API -> Deployment Stage (New Stage) -> Dev as Stage Name -> Deploy.
- Your POST API is Now deployed.
- Copy the INVOKE URL.
- POST call this INVOKE URL with message param in body to send the email.
Since we created our AWS Lambda function and provided the API-endpoint access using API gateway, it’s time to connect all the pieces together and test them.
now API_Gateway has been added as a triiger in lambda function. So now finally go to postman and select post method and enter the api url into postman and in body section add the code which is shown in the following attachements.
finally i Got the mail in my mail box as shown in the attachement.
Now you should be able to submit your contact form and start receiving email notifications when a form is completed and submitted.