Home United States USA — software Meshing MongoDB Atlas, Twilio, and AWS Simple Email With AWS Step Functions...

Meshing MongoDB Atlas, Twilio, and AWS Simple Email With AWS Step Functions (Part 2) Meshing MongoDB Atlas, Twilio, and AWS Simple Email With AWS Step Functions (Part 2)

216
0
SHARE

Integrating MongoDB Atlas, Twilio, and AWS Simple Email is easily possible via Step Functions, which allow you to orchestrate your serverless services.
This is Part 2 of the AWS Step Functions overview post published a few weeks ago. If you want to get more context on the sample application business scenario, head back to read Part 1. In this post, you’ ll get a deep dive into the application’s technical details. As a reference, the source code of this sample app is available on GitHub.
The screenshot above is the graphical representation of the state machine we will eventually be able to test and run. But before we get there, we need to set up and publish the 4 Lambda functions this Step Functions state machine relies on. To do so, clone the AWS Step Functions with MongoDB GitHub repository and follow the instructions in the Readme file to create and configure these Lambda functions.
If you have some time to dig into their respective codebases, you’ll realize they’re all made up of just a few lines, making it simple to embed Twilio, AWS and MongoDB APIs in your Lambda function code. In particular, I would like to point out the concise code the Get-Restaurants lambda function uses to query the MongoDB Atlas database:
The code snippet above is a simple yet powerful example of aggregation framework queries using the $match and $project stages along with the $avg and $max accumulator operators. In a nutshell, this aggregation filters the restaurants dataset by 3 properties (zip code, cuisine, and name) in the $match stage, returns a subset of each restaurant’s properties (to minimize the bandwidth usage and query latency) , and computes the maximum and average values of health scores obtained by each restaurant (over the course of 4 years) in the $project stage. This example shows how you can very easily replace SQL clauses (such as WHERE () , MAX () and AVG () ) using MongoDB’s expressive query language.
Once you are done with setting up and configuring these Lambda functions, it’s time to finally create our Step Functions state machine.
We’ re not quite done yet. But before we complete the last steps to get a fully functional Step Functions state machine, let me take a few minutes to walk you through some of the technical details of my state machine JSON file.
Enter the following JSON test document (with a valid emailTo field) and press Start Execution:
If everything was properly configured, you should get a successful result, similar to the following one:
If you see any red boxes (in lieu of a green one) , check CloudWatch where the Lambda functions log their errors. For instance, here is one you might get if you forgot to update the emailTo field I mentioned above:
And that’s it (I guess you can truly say we’ re ” done ” now) ! You have successfully built and deployed a fully functional cloud workflow that mashes up various API services thanks to serverless functions.
For those of you who are still curious, read on to learn how that sample state machine was designed and architected.
Let’s start with the state machine design:
At this point, you might ask yourself: how is the data passed from one lambda function to another?
This input contains all the information my state machine might need, from the search criteria (startsWith, cuisine, and zipcode) , to the user’s cell phone number (if the state machine ends up using the SMS notification method) , first name, email address and email subject (if the state machine ends up using the email notification method) .
In this post, I showed you how to create a state machine that orchestrates calls to various cloud services and APIs using a fictitious restaurant search and notification scenario. I hope you enjoyed this tutorial explaining how to deploy and test that state machine using the AWS console. Last, I went through various design and architecture considerations, with a focus on data flow abilities available in Step Functions.
If you haven’ t done so already, sign up for MongoDB Atlas and create your free M0 MongoDB cluster in minutes. Next, you can get more familiar with AWS Lambda development and deployment by following our 101 Lambda tutorial. As a last step, you might be interested in Step Functions integration with API Gateway to learn how to call a state machine from an external application.

Continue reading...