{"id":62022,"date":"2024-06-11T18:19:10","date_gmt":"2024-06-11T12:49:10","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=62022"},"modified":"2024-06-11T18:55:17","modified_gmt":"2024-06-11T13:25:17","slug":"unleashing-the-power-of-api-orchestration-with-aws-step-functions-and-api-gateway","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/unleashing-the-power-of-api-orchestration-with-aws-step-functions-and-api-gateway\/","title":{"rendered":"Unleashing the Power of API Orchestration with AWS Step Functions and API Gateway"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">In today&#8217;s complex software landscape, applications often rely on interactions with multiple APIs to complete tasks. Managing these interactions can quickly become cumbersome, leading to code duplication, increased complexity, and potential errors. This is where <\/span><b>API orchestration<\/b><span style=\"font-weight: 400;\"> comes to the rescue.<\/span><\/p>\n<p><b>API orchestration<\/b><span style=\"font-weight: 400;\"> allows you to choreograph the calls to various APIs in a pre-defined sequence, treating them as composable building blocks for a larger functionality. This approach offers several benefits:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>Improved code maintainability:<\/b><span style=\"font-weight: 400;\"> You reduce code duplication and simplify future maintenance by centralizing the orchestration logic in a single location.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Increased error handling:<\/b> Define centralized error handling mechanisms within the orchestration layer, improving the overall resilience of your application.<\/li>\n<li style=\"font-weight: 400;\"><b>Enhanced scalability:<\/b> Orchestration tools like AWS Step Functions enable building scalable workflows that can adapt to varying workloads.\n<p><div id=\"attachment_62023\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62023\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-62023 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-16-31-1024x614.png\" alt=\"API Orchestartion diagram\" width=\"625\" height=\"375\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-16-31-1024x614.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-16-31-300x180.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-16-31-768x461.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-16-31-624x374.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-16-31.png 1157w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-62023\" class=\"wp-caption-text\">API Orchestration diagram<\/p><\/div><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>The image illustrates an architecture where an API Gateway routes client requests to various microservices, with an Orchestrator Service handling requests, aggregating responses from multiple services (Service A, B, and C), and composing the final response back to the client. The Orchestrator Service coordinates interactions among the microservices, managing the data flow and ensuring a cohesive response is returned to the client.<\/p>\n<p><span style=\"font-weight: 400;\">AWS Step Functions is a serverless orchestration service by which we can combine AWS Lambda functions and other AWS services to build complex business applications. <\/span><span style=\"font-weight: 400;\">We can author the orchestration logic in a declarative style using a JSON-based format called the Amazon States Language(ASL). AWS Step functions also provide a Work ow Studio where we can define and run our work ows.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this article, we will introduce the concepts of AWS Step Functions and understand its working with the help of an example.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let us first understand some basic concepts of Step Functions.<\/span><\/p>\n<h2><b>State Machine, State, and Transitions<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">A state machine is a mathematical model of computation consisting of different states connected with transitions. AWS Step functions also implement a state machine to represent the orchestration logic. Each step of the orchestration is represented by a state in the state machine and connected to one or more states through transitions.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">They are represented by a diagram to visualize the current state of a system as shown here:<\/span><\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_62024\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62024\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-62024\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-21-18.png\" alt=\"State and Transition Diagram\" width=\"600\" height=\"443\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-21-18.png 780w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-21-18-300x222.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-21-18-768x567.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-16-21-18-624x461.png 624w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><p id=\"caption-attachment-62024\" class=\"wp-caption-text\">State and Transition Diagram<\/p><\/div>\n<p><span style=\"font-weight: 400;\">State machines contain at least one state. Transitions represent different events that allow the system to transition from one state to another state.\u00a0<\/span><\/p>\n<h2><b>Types of State<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">States receive input, perform actions to produce some output and pass the output to other states. States are of different types which determine the nature of the functions a state can perform. Some of the commonly used types are:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><b>Task<\/b><span style=\"font-weight: 400;\">: A state-of-type <\/span><span style=\"font-weight: 400;\">task <\/span><span style=\"font-weight: 400;\">represents a single unit of work performed by a state machine. Tasks perform all the work in a state machine. The work is performed by using an activity or an AWS Lambda function, or by passing parameters to the API actions of other services.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Parallel<\/b><span style=\"font-weight: 400;\">: State of type <\/span><span style=\"font-weight: 400;\">parallel <\/span><span style=\"font-weight: 400;\">is used to trigger multiple execution branches.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Map<\/b><span style=\"font-weight: 400;\">: We can dynamically iterate steps with a state-of-type <\/span><span style=\"font-weight: 400;\">map<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Choice<\/b><span style=\"font-weight: 400;\">: We use this type of state as decision points within a state machine to choose among multiple execution branches.<\/span><\/li>\n<li><strong>Wait:\u00a0<\/strong>A Wait state represents a pause or delay in executing your workflow.<\/li>\n<li><strong>Pass:\u00a0<\/strong>A Pass state represents a no-op or pass-through state that passes its input to its output without performing any processing.<\/li>\n<li><strong>Fail:\u00a0<\/strong>A Fail state represents a terminal state in your workflow indicating that the execution has failed.<\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">The state machine executes one state after another till it has no more states to execute. We will understand these concepts further by implementing a sample Generation <\/span><span style=\"font-weight: 400;\">process of an application with a state machine.<\/span><\/p>\n<h2><b>Types of State Machine: Standard vs Express<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">We can create two types of state machines. State machine executions differ based on the type. The type of state machine cannot be changed after the state machine is created.<\/span><\/p>\n<ol>\n<li><b>Standard<\/b><span style=\"font-weight: 400;\">: State machine of type: <\/span><span style=\"font-weight: 400;\">Standard <\/span><span style=\"font-weight: 400;\">should be used for long-running, durable, and auditable processes.<\/span><\/li>\n<li><b>Express:<\/b>\u00a0State machine of type: Express is used for high-volume, event-processing workloads such as IoT data ingestion, streaming data processing and transformation, and mobile application backends. They can run for up to five minutes.<\/li>\n<\/ol>\n<h2><b>Exploring the state machine<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">The state machine orchestrates the ticket generation processing and distribution:<\/span><\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_62028\" style=\"width: 630px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62028\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-62028 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/ticket-Generation-AOL-1.png\" alt=\"Sample State Machine Diagram\" width=\"620\" height=\"674\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/ticket-Generation-AOL-1.png 620w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/ticket-Generation-AOL-1-276x300.png 276w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><p id=\"caption-attachment-62028\" class=\"wp-caption-text\">Sample State Machine Diagram<\/p><\/div>\n<p><span style=\"font-weight: 400;\">The workflow is structured as follows:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><b>Choice State for User Selection<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">The workflow starts with a choice state that conditionally executes based on the user&#8217;s choice between &#8220;Fetch&#8221; or &#8220;Generate&#8221; actions.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Parallel State for Data Filtering: A parallel state is used with two pass states following the choice state<\/b><span style=\"font-weight: 400;\">. Each pass state filters out specific details for calling the Ticket generation API.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Lambda Function for Ticket Generation: <\/b><span style=\"font-weight: 400;\">After data filtering, a Lambda function is invoked to generate a ticket using an API call based on the filtered details.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Handling Success and Failure: <\/b><span style=\"font-weight: 400;\">The workflow includes success and fail states that respond accordingly and terminate the workflow if ticket generation fails.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Lambda Function for Ticket Encryption: <\/b><span style=\"font-weight: 400;\">Upon successful ticket generation, another Lambda function is triggered to perform encryption on the generated ticket.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Parallel State for Distribution: <\/b><span style=\"font-weight: 400;\">Next, a parallel state is utilized to trigger: <\/span><span style=\"font-weight: 400;\">A success state indicates the completion of the workflow. and an Amazon SNS (Simple Notification Service) action that sends the encrypted ticket to a subscriber.<\/span><\/li>\n<\/ol>\n<h2><b>Connect API Gateway to Step Functions<\/b><\/h2>\n<div id=\"attachment_62029\" style=\"width: 604px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62029\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-62029\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/API-Gateway-StepFN-Intgration.drawio.png\" alt=\"API Gateway Integration With Step Function \" width=\"594\" height=\"200\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/API-Gateway-StepFN-Intgration.drawio.png 594w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/API-Gateway-StepFN-Intgration.drawio-300x101.png 300w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><p id=\"caption-attachment-62029\" class=\"wp-caption-text\">API Gateway Integration With Step Function<\/p><\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Connecting API Gateway to AWS Step Functions allows you to trigger Step Functions workflows directly from incoming HTTP requests.<\/span><\/p>\n<ol>\n<li>\n<h3><strong>\u00a0Creating a Rest API<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Navigate to the API Gateway console, and click on \u201cCreate API\u201d in the right top corner. Click on \u201cBuild\u201d under \u201cREST API\u201d.<\/span><\/p>\n<div id=\"attachment_62031\" style=\"width: 949px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62031\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-62031 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-17-59-10.png\" alt=\"Creating Rest API on API Gateway\" width=\"939\" height=\"756\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-17-59-10.png 939w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-17-59-10-300x242.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-17-59-10-768x618.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-17-59-10-624x502.png 624w\" sizes=\"(max-width: 939px) 100vw, 939px\" \/><p id=\"caption-attachment-62031\" class=\"wp-caption-text\">Creating Rest API<\/p><\/div>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Choose the API Type &#8211; select \u201cREST API \u201d<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Create a new API &#8211; select \u201cNew API\u201d<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Settings &#8211; provide a suitable \u201cAPI name\u201d, \u201cDescription\u201d<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Click \u201cCreate API\u201d to create the base API.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3><strong>2.\u00a0 <\/strong><strong>Configure Integration Request\u00a0<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 Open the newly created API. Click on \u201cResources\u201d and create a \u201cPOST\u201d method and set the \u201cIntegration Request\u201d following as shown in the figure.<\/span><\/p>\n<div id=\"attachment_62032\" style=\"width: 946px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62032\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-62032\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Untitled-design-1.png\" alt=\"Integration Request Page\" width=\"936\" height=\"881\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Untitled-design-1.png 936w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Untitled-design-1-300x282.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Untitled-design-1-768x723.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Untitled-design-1-624x587.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Untitled-design-1-24x24.png 24w\" sizes=\"(max-width: 936px) 100vw, 936px\" \/><p id=\"caption-attachment-62032\" class=\"wp-caption-text\">Integration Request Page<\/p><\/div>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Integration type &#8211; AWS Service<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">AWS Region &#8211; Set the appropriate region<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">AWS Service &#8211; Step Functions<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Action &#8211; StartSyncExecution<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Execution role &#8211; Role ARN to allow API Gateway to invoke Step Function API (IAM Action &#8211; \u201cstates: StartSyncExecution\u201d)<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3.<strong> Deploy the API<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">\u00a0 \u00a0 Finally, deploy the API into an appropriate stage.<\/span><\/p>\n<div id=\"attachment_62033\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62033\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-62033\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-18-09-42.png\" alt=\"Deploy the API\" width=\"590\" height=\"543\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-18-09-42.png 590w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-18-09-42-300x276.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><p id=\"caption-attachment-62033\" class=\"wp-caption-text\">API Deploy<\/p><\/div>\n<p>&nbsp;<\/p>\n<h2><b>Last but not least, the pricing<\/b><\/h2>\n<div id=\"attachment_62034\" style=\"width: 886px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62034\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-62034\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-18-12-37.png\" alt=\"Pricing\" width=\"876\" height=\"429\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-18-12-37.png 876w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-18-12-37-300x147.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-18-12-37-768x376.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-from-2024-05-29-18-12-37-624x306.png 624w\" sizes=\"(max-width: 876px) 100vw, 876px\" \/><p id=\"caption-attachment-62034\" class=\"wp-caption-text\">Pricing<\/p><\/div>\n<p><span style=\"font-weight: 400;\">Step Functions charges based on the number of state transitions required to execute your application. So, every time a step in your workflow is executed, it is counted as a state transition. AWS charges you for the total number of your state transitions across all your state machines, including retries.<\/span><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Free Tier: 4,000 state transitions per month<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">State transitions: $0.000025 PER STATE TRANSITION after that $0.025 per 1,000 state transitions<\/span><\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>API orchestration simplifies managing modern applications by reducing code duplication and enhancing reliability. AWS Step Functions offer a powerful framework for scalable, maintainable, and resilient workflows. With robust error handling and seamless AWS integration, Step Functions streamline complex processes, enabling efficient and reliable applications that enhance the user experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s complex software landscape, applications often rely on interactions with multiple APIs to complete tasks. Managing these interactions can quickly become cumbersome, leading to code duplication, increased complexity, and potential errors. This is where API orchestration comes to the rescue. API orchestration allows you to choreograph the calls to various APIs in a pre-defined [&hellip;]<\/p>\n","protected":false},"author":1833,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":133},"categories":[2348],"tags":[248,5939,5940],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/62022"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1833"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=62022"}],"version-history":[{"count":13,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/62022\/revisions"}],"predecessor-version":[{"id":62287,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/62022\/revisions\/62287"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=62022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=62022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=62022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}