Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Meshing MongoDB Atlas, Twilio, and AWS Simple Email With AWS Step Functions

DZone's Guide to

Meshing MongoDB Atlas, Twilio, and AWS Simple Email With AWS Step Functions

Serverless apps using AWS rarely rely on one Lambda function. Fortunately, Step Functions were recently introduced to help orchestrate various serverless services.

· Cloud Zone
Free Resource

What if you could learn how to use MongoDB directly from the experts, on your schedule, for free? We've put together the ultimate guide for learning MongoDB. Sign up and you'll receive instructions for how to get started!

A few weeks ago, I took a close look at AWS Lambda functions, and I hope you've enjoyed that tutorial.

I highly suggest that you read it first if you have no previous experience with AWS Lambda, as I will build on that knowledge for the second leg of our road trip into developer-centric services available in Amazon Web Services.

Indeed, I'd like to continue our discovery journey by investigating AWS Step Functions. If that name doesn't ring a bell, don't worry: Step Functions were recently introduced at AWS re:Invent 2016 and are still relatively unknown, despite their incredible potential, which I'll try to make apparent in this post.

Lambda Functions Are Great, But...

If you're building a real cloud-native app, you probably already know that your app won't be able to rely on one single Lambda function. As a matter of fact, it will rely on a multitude of functions that interact with multiple systems, such as databases, message queues, and even physical or virtual servers (yes, even in a serverless world, there are still servers!). And as the screenshot below tries to depict, your app will need to call them at certain times, in a specific order and under specific conditions.

@Copyright Amazon Web Services (from the AWS:reInvent 2016 Step Functions SVR201 session])

Orchestrating all these calls is a tedious endeavor that falls into the developer's lap (you!). Wouldn't it be nice if there was some sort of cloud technology that would help us reliably deal with all that coordination work?

Introducing AWS Step Functions

In a nutshell, Step Functions are a cloud workflow engine: They aim at solving the issue of orchestrating multiple serverless functions (such as AWS Lambdas) without having to rely on the application itself to perform that orchestration. Essentially, Step Functions allow us to design visual workflows (or "state machines" as AWS refers to) that coordinate the order and conditions in which serverless functions should be called.

Surely enough, the concepts of orchestration and workflows have been around for quite some time so there's nothing groundbreaking about them. As a matter of fact, AWS even released its own Simple Workflow Service back in 2012, before serverless had become the cool kid on the block.
What's interesting about Step Functions though is that they provide an integrated environment primarily designed to ease the orchestration of AWS Lambda functions. And as Lambda functions become more and more popular, AWS Step Functions turn out to be exactly what we need!

So What's a Good Use Case to Employ Step Functions?

For those of you who have no idea what the screenshot above means, don't worry! In my next post, I'll dive into the technical details of the sample app it's taken from. For now, let's just say that it's the visual representation of the state machine I built. But you may ask yourself: what does this state machine do exactly and why is it relevant to the topic today?

Here's the fictitious (but probably not too hypothetical) use case I tried to solve with it: You went to a great Italian restaurant in New York but you don't quite remember its exact name. But the food was so delicious that you'd really like to go back there! (you might think only Dory — or me — does not to remember an amazing restaurant, but I'm sure that happens even to the best of us).

Wouldn't it be useful if you could get notified instantly about the possible restaurant matches with their exact names and addresses in your area? Ideally, if there happens to be one match only, you'd like to get the notification via text message (or "SMS" in non-US parlance). But if there are a lot of matches, a text message might be difficult to read, so you'd rather get an email instead with the list of restaurants matching your search.

Now, I'm quite sure that service already exists (Yelp, anyone?) but I thought it was a good use case to demonstrate how Step Functions can help you solve a business process requirement, as well as Step Functions’ ability to easily mash up different APIs and services together into one single workflow.

How Did I Go About Building Such a Step Function?

As I was envisioning this sample app built with AWS Step Functions, I thought about the required components I'd have to build, and then boiled them down to 3 AWS Lambda functions:

  • GetRestaurants function that queries a collection of restaurants stored in a MongoDB Atlas database.
  • SendBySMS function that sends a text message using SMS by Twilio if the result of the GetRestaurants query only returns one restaurant.
  • SendByEmail function that sends an email using AWS Simple Email Service if the GetRestaurants function returns more than one restaurant.

If you look closely at the screenshot above, you will probably notice I seemingly forgot a step: there's indeed a fourth Lambda helper function named CountItems whose purpose is simply to count the items returned by the GetRestaurants function and pass that count value on to the NotificationMethodChoice branching logic. Granted, I could have easily merged that helper function into the GetRestaurants function but I chose to leave it because I figured it was a good way to experiment with Step Functions' inputs and outputs flexibility and showcase their power to you (more about this topic in my next post). It's a Step Functions technique I've used extensively to pass my initial input fields down to the latest SendBy* Lambda functions.

I hope you liked this short introduction to AWS Step Functions and the use case of the sample app I built to demonstrate its usefulness. Stay tuned for the next post where we'll explore the nitty-gritty implementation details of this app!

Enjoyed this post? Replay our webinar where we have an interactive tutorial on serverless architectures with AWS Lambda.

What if you could learn how to use MongoDB directly from the experts, on your schedule, for free? We've put together the ultimate guide for learning MongoDBSign up and you'll receive instructions for how to get started!

Topics:
cloud ,step functions ,aws lambda ,serverless ,tutorial

Published at DZone with permission of Raphael Londner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}