1. Home
  2. Training Library
  3. Amazon Web Services
  4. Courses
  5. Advanced Use of Amazon API Gateway

Creating a State Machine

Start course
Overview
DifficultyAdvanced
Duration22m
Students1204
Ratings
4.8/5

Description

In this course, we will create a multi-tier Serverless architecture on AWS using Amazon API Gateway, AWS Lambda, AWS Step Functions and Amazon Polly text to speech. 
This is a hands-on course where you will learn how to create serverless functions, data access, business logic and integration layers on AWS. Also, you will learn to create a presentation layer for your application using the client SDK generated by Amazon API Gateway.

We will then host the application on Amazon S3. At the conclusion of the course, you will be able to recognize and implement an end to end workflow built in the Amazon Cloud using Serverless components.

This course is intended for developers or DevOps engineers who want to create Serverless applications on AWS, or who may be considering migrating their existing web applications to AWS.

Pre-requisites for this course are having a basic familiarity with Amazon API Gateway, Lambda, DynamoDb, S3 and AWS Step functions is required and some knowledge of building web applications using HTML and JavaScript will also be helpful.

Course resources - https://github.com/cloudacademy/advanced-api-gateway-resources

Transcript

- [Instructor] Hello, and welcome back. In this lecture, we'll use Step Functions to create a state machine to orchestrate our Lambda Functions. State machine is just a visual workflow representation that lets you arrange and coordinate discrete components of your distributed application. Those components could be microservices consisting of compute resources or Lambda Functions, as in our case. I'll go to the Step Functions console now. State machine is built using JSON notation, and the Step Functions console gives us quite a few blueprints to start with. So I'll just choose the ChoiceState blueprint, which is best suited for our scenario. Here, as you can see, the FirstState is of type Task, which is a Lambda Function. And then the ChoiceState is of type Choice, and it depends on a variable foo. If its value equals a certain number, the workflow follows a certain execution path. And if it matches another number, the workflow follows another path. So, we'll follow the same pattern as in this blueprint, and we'll try to come up with our own food-order workflow. To edit the JSON, I would suggest you copy the blueprint from here and edit it in another editor. So I'll go ahead and copy the blueprint in another editor. I want my first state to be the PlaceOrder function, followed by the ProcessPayment function. Note that I'm leaving the resource as it is here. We'll fill it up with the Lambda Function ARN later. Then I have my ChoiceState as IsPaymentSuccesful, which depends on the Boolean paymentSuccess variable. If the choice is true, my next state will be process order. Otherwise, it will be CancelOrder. Both of which will be followed by NotifyUser and notify user with Polly states. Let me delete all the resource ARNs, and we're pretty much done here. So I'll paste this JSON in the Step Functions editor in the console. Click on the preview to see how our state machine looks like. And it looks good. As I told you earlier, we'll insert the Lambda Function ARNs here. It can be easily done through the in-line editor, as it gives you a list of all available Lambda Functions, from which you can easily choose the one you want. We're almost done here. I'll give a name to the state machine. And then I'll go ahead and click on Create state machine. Click OK on the IAM role selection for state machine execution window. And our state machine is created successfully. Let's quickly test our state machine here. Click on New execution, and its input should look like this. And let's give all 1's as credit card number, so that our payment will succeed. I will click on Start execution here. Now, as you can see, the workflow is taking the success path here. And our output message status is correct that the order will be ready in 15 minutes. Now let's do another execution. This time, I'll change the credit card number to something else, so that it can fail. Here you can see that the workflow is taking the CancelOrder route as expected. And our output message is also correct, showing that the order was canceled. Okay, so far, all our Lambda Functions are nicely organized in a workflow through Step Functions. And what we need now is a ReST interface to expose this back-end functionality to the front-end user. In the next video, we'll see how to do just that through API Gateway.

About the Author

Tehreem is a Sr. Software Engineer with passion in Cloud Technologies, Big Data analytics, Software Testing and Automation. She has over 10 years of work experience comprising of her tenure at ServiceNow, Microsoft and Harmonic Inc. Most recently she has been developing learning content in-line with the emergence of Public Clouds and XaaS platforms with focus on AWS, Microsoft Azure and GCP. Tehreem resides in BayArea, CA with her family and when not working she enjoys nature/outdoors, movies and fine dining.