Building serverless app sending email reminder
Second Adrian Cantril demo that utilises S3, SES, API gateway, Lambda, and Step functions to build serverless app sending out email reminder.
Before

After

Firstly I set up the SES email that you want to send out emails from, you need to create identity on SES and verify the email.
stage 1 & 2 is pretty straight-forward and only involves steps to create identities and verifying.
then, I built lambda function that uses SDK to interact with SES, with the correct lambda role, it successfully get invoked. It was good to know that I am now able to build test events which means that i am getting a bit of an understanding on how the lambda works
During stage 3, a state machine was set up with a role that allows for invoking lambda functions and executing all actions related to SNS.
Essentially, this state machine initiates when the client application sends data via api gateway, triggering a lambda function that then runs the state machine with the input data received from the client app. The state machine extracts necessary information, such as the recipient's email address, the duration to wait, and the message to be sent, from this input and forwards this data to invoke a second lambda function that communicates with SES to send an email.
Initially, I found the syntax of the state machine a bit confusing, but thanks to the Amazon documentation, I was able to grasp the concept of passing parameters to the next lambda function to be invoked using $. Essentially, when selecting a value using a path, the key-value pair must end in .$.

{ "InvokeLambda":
{ "Type": "Task",
"Resource": "arn:aws:lambda:us-east1:123456789012:function:MyLambdaFunction",
"Parameters": {
"Payload":
{ "Input.$": "$" } },
"End": true } }Stage 4
Creating an API gateway helps establish an entry point for the application, which the endpoint client app can use to communicate with the application.
It took me a long time to understand CORS. I encountered a CORS error when trying to post data from the client app to the API gateway. Even though I confirmed that CORS was enabled on the API gateway, I still faced the same error.

In this scenario, the website hosted on S3 has JavaScript that fetches data from the API gateway, which then triggers a Lambda function. I realized that during this process, I overlooked including the following header: "headers": {"Access-Control-Allow-Origin":"*"}
Since CORS is a browser security feature that allows the sharing of resources between different origins, it verifies the origin. If the origin in the response header does not match, a CORS error is thrown.
In this case, the preflight check, designed to perform the initial security verification and return allowed methods and origins, returned successfully with Access Control Allow Origin set to *, but the actual request did not contain this header. I reviewed the Lambda function to ensure that the response included "headers": {"Access-Control-Allow-Origin":"*"}

I reployed the lambda function and it started working as expected




Comments