{"id":1881,"date":"2019-04-18T14:46:21","date_gmt":"2019-04-18T04:46:21","guid":{"rendered":"https:\/\/developers.messagemedia.com\/?p=1881"},"modified":"2019-04-18T14:46:21","modified_gmt":"2019-04-18T04:46:21","slug":"an-introduction-to-aws-step-functions","status":"publish","type":"blog","link":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/","title":{"rendered":"An Introduction to AWS Step Functions"},"content":{"rendered":"<p>This is a guest blog from <a href=\"https:\/\/www.linkedin.com\/in\/lini-abraham-64685a58\/\">Lini Abraham<\/a>, an Automation Test Analyst at MessageMedia.<\/p>\n<blockquote><p><em>Lini is working as a QA Engineer in MessageMedia.She is curious to learn and experiment new technologies.\u00a0<\/em><em>Working in a highly ambitious and flexible team has enabled her to work on development tasks along with testing.\u00a0<\/em><em>She also loves travelling and exploring new places with her family.<\/em><\/p><\/blockquote>\n<p>AWS Step functions are one among the current 90 and ever-growing services offerings by Amazon Web Services. It&#8217;s much the same as a state machine where a system has multiple states and the output of the previous state serves as the input for the next state. This article is will give you a basic idea to build a simple step function on the AWS Console.<\/p>\n<p>In your applications, there might be scenarios where there are &#8216;n&#8217; number of states or steps and for a process or job to be completed, it has to traverse through these states. Step functions lets you build visual workflows to coordinate different activities in your applications. The implementation of each step can be accomplished using AWS Lambda which is a server-less computing platform provided by AWS. Building server-less applications means that you can focus on your code instead of worrying about managing and operating servers or runtimes as it&#8217;s taken care of by the cloud provider. It&#8217;s a pay-as-you-go model where you only pay for the duration and memory allocated to run your code; without any associated fees for idle time.<\/p>\n<p>Step functions are defined in JSON format using AWS States Language (ASL) (AWS Documentation: https:\/\/states-language.net\/spec.html). The state machine can be validated and visualised as a workflow (similar to a flowchart) on AWS Console by providing the JSON definition.<\/p>\n<p>The best way to learn something is through practice. So, let\u2019s create a simple step function with only one state.<\/p>\n<p>AWS Documentation to create a state machine can be found <a href=\"https:\/\/docs.aws.amazon.com\/step-functions\/latest\/dg\/getting-started.html\">here<\/a>.<\/p>\n<h2>Pre-requisites for creating the state machine<\/h2>\n<p>You&#8217;ll need an AWS account and access to your AWS console.<\/p>\n<h3>Step 1: Create the IAM Role which lets Lambda function write logs to Cloud Watch<\/h3>\n<p>Navigate to AWS IAM &gt; Create Role &gt; AWS Service &gt; Choose Lambda &gt; \u201dNext: Permissions\u201d &gt; filter and select the policy &#8220;CloudWatchLogsFullAccess&#8221; &gt; \u201d Next: Tags\u201d &gt; \u201dNext: Review\u201d \uf0e0&gt; Provide the role name as \u201clambda_cloudwatch_access\u201c &gt; Create Role.<\/p>\n<h3>Step 2: Create a Lambda Function from AWS Console using Python 3.6 runtime<\/h3>\n<p>(For more information regarding creating a Lambda on AWS console, refer to the AWS<a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/getting-started-create-function.html\"> documentation here<\/a>.)<\/p>\n<p>On AWS Management Console, navigate to \u201cLambda\u201d service &gt; Create Function &gt; Choose \u201cAuthor from scratch\u201d &gt; Provide the function name as SayHello with runtime as Python 3.6 &gt; Click on Choose or create an execution role under \u201cPermissions\u201d and select \u201cChoose an existing role\u201d from the drop-down and provide the role name lambda_cloudwatch_access &gt; Click on Create function button.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1889\" src=\"https:\/\/messagemedia.com\/wp-content\/uploads\/2020\/08\/Picture2.png\" alt=\"\" width=\"500\" height=\"268\" \/><\/p>\n<p>Copy the below code and paste it in Lambda code section. The below function will get the name field from its input event and returns &#8216;Hello &#8216; + as the output.<\/p>\n<pre>def lambda_handler (event, context):\nname = event['name']\nreturn 'Hello ' + name<\/pre>\n<p>Click on \u201cSave\u201d button at the top of the console and note the ARN of the lambda which will be used inside the Step Function definition. The ARN looks like this:<\/p>\n<pre>arn:aws:lambda:&lt;aws_region&gt;:&lt;aws_account_id&gt;:function:SayHello<\/pre>\n<h3>Step 3: Create the IAM role to grant access to Step Function to execute Lambda Function<\/h3>\n<p>The step function that we create later in this tutorial requires permission to execute the \u201cSayHello\u201d lambda function.<\/p>\n<p>On AWS Console select IAM &gt; Create Role &gt; AWS Service &gt; Choose Step Function &gt; Click on \u201cNext: Permissions\u201d button &gt;<br \/>\n\u201cAWSLambdaFullAccess\u201d permission is displayed by default &gt; Next: Tags &gt; Next: Review &gt; Provide the role name as \u201cstepfunction_lambda_access &gt; Click on Create Role<\/p>\n<p>Once you have created the role, note the role ARN to use it while creating the step function.<\/p>\n<h3>Step 4: Create an AWS Step Function (State Machine)<\/h3>\n<p>In the <a href=\"https:\/\/docs.aws.amazon.com\/step-functions\/latest\/dg\/getting-started.html\">AWS <\/a>documentation, the state machine is created by selecting <strong>Templates<\/strong>\u00a0which lets you build it from existing templates. In order to create a step function from scratch, choose <strong>Author with code snippets\u00a0<\/strong>instead (Refer to the below screenshot), name it as &#8216;SayHello&#8217; and paste the state machine definition.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1888\" src=\"https:\/\/messagemedia.com\/wp-content\/uploads\/2020\/08\/1_Picture1.png\" alt=\"\" width=\"500\" height=\"246\" \/><\/p>\n<p>In this example, we will use the below definition written using Amazon States Language.<br \/>\nNote: Replace the value of \u201cResource\u201d key with the ARN of SayHello lambda function created earlier<\/p>\n<pre>{\n\"StartAt\":\"SayHello\",\n\"Comment\":\"An example of a state machine with Task state\",\n\"States\":{\n\"SayHello\":{\n\"Type\":\"Task\",\n\"Resource\":\"\",\n\"End\":true\n}\n}\n}<\/pre>\n<p>After pasting the definition, click \u201cNext\u201d and \u201cChoose an existing IAM role\u201d and provide the ARN of the role we created to grant lambda execution access to Step Function. Then click on Create state machine button.<\/p>\n<h4>Step Function mandatory and optional fields<\/h4>\n<p>Mandatory fields: startAt, States<br \/>\nMandatory fields of a state with Type =\u201d Task\u201d: Resource, End\/Next<br \/>\nOptional: Comment<\/p>\n<p>Let&#8217;s take a deep-dive into the JSON definition:<\/p>\n<p><em>StartAt:<\/em> The name of the state from which you expect the workflow to begin with<br \/>\n<em>States Object:<\/em> All the different states in the step function and their attributes go in here<br \/>\n<em>SayHello:<\/em> Name of the state<\/p>\n<p><em>Type:<\/em> AWS supports 7 types of states<br \/>\n<u><\/u><\/p>\n<ul>\n<li>Task &#8211; Performs a specific action<\/li>\n<li>Choice &#8211; Lets you add and evaluate conditions<\/li>\n<li>Parallel &#8211; Carries out actions in parallel<\/li>\n<li>Wait &#8211; Adds delay for a certain amount of time specified<\/li>\n<li>Fail &#8211; Terminates a step function execution resulting in a failure<\/li>\n<li>Succeed- Ends an execution successfully<\/li>\n<li>Pass &#8211; Passes the input of the state and extra parameters to its output<\/li>\n<\/ul>\n<p><em>Resource<\/em>: The Amazon Resource Name (ARN) of your Lambda function<br \/>\n<em>End:<\/em> Any state except for Choice, Succeed, and Fail must have a field named \u201cEnd\u201d whose value is a Boolean if that is the final state of the step function<\/p>\n<p>If there is more than one state, then the states must have either &#8220;next&#8221;: &#8220;name of the next state&#8221; if it&#8217;s followed by another state or &#8220;end&#8221;: true if it&#8217;s the last state (except for Choice, Succeed, and Fail states).<\/p>\n<h3>Step 5: Running your Step Function<\/h3>\n<p>To execute your step function, navigate to Step functions on AWS console and choose the step function you created.<br \/>\nIn the \u201cExecutions\u201d tab, click on Start execution button, specify an execution name and provide the input as below<\/p>\n<pre>{\n\"name\": \"Input your name here\"\n}<\/pre>\n<p>Once you hit start execution button, you will be able to see your state machine getting executed and the colour of the state changes to green if it succeeds (blue while it\u2019s being executed).<br \/>\nClick on the state to see it\u2019s input and output and exception if the state execution fails in which case, the state is represented in red colour. The input and output for the sayHello state is as follows:<\/p>\n<pre>input: {\n\"name\": \"the name you entered\"\n}\noutput: \"Hello \"the name you entered\"<\/pre>\n<h2>Conclusion<\/h2>\n<p>So now you have learned to create a Step function. You understand the lambda function which runs the implementation code behind the \u201ctask\u201d state of the step function, IAM roles for the Step function to execute the lambda function and lambda to output logs to Cloud watch.<\/p>\n<p>In real-time scenarios where you have complex interrelated states which perform single or parallel actions and evaluates decisions, you can orchestrate them programmatically using step functions and serverless technologies in an efficient manner. There will be many advancements and expansions in step functions and the serverless world in the forthcoming years and it will be exciting to see the evolution.<\/p>\n","protected":false},"author":0,"featured_media":8917,"menu_order":186,"template":"page-blog-v2.php","meta":{"_acf_changed":false,"popular":false,"coming_soon":false,"link":"","footnotes":""},"blog_category":[37],"class_list":["post-1881","blog","type-blog","status-publish","has-post-thumbnail","hentry","blog_category-developers"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>An Introduction to AWS Step Functions - Sinch MessageMedia Australia<\/title>\n<meta name=\"description\" content=\"Discover how Step Functions revolutionize your workflow orchestration, offering seamless integration, scalability, and reliability. Dive into the fundamentals, explore use cases, and unlock the potential of serverless architecture. Australia\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An Introduction to AWS Step Functions - Sinch MessageMedia\" \/>\n<meta property=\"og:description\" content=\"Discover how Step Functions revolutionize your workflow orchestration, offering seamless integration, scalability, and reliability. Dive into the fundamentals, explore use cases, and unlock the potential of serverless architecture.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Sinch MessageMedia\" \/>\n<meta property=\"og:image\" content=\"https:\/\/messagemedia.com\/wp-content\/uploads\/2019\/04\/An-Introduction-to-AWS-Step-Functions.png\" \/>\n\t<meta property=\"og:image:width\" content=\"919\" \/>\n\t<meta property=\"og:image:height\" content=\"288\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/blog\\\/an-introduction-to-aws-step-functions\\\/\",\"url\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/blog\\\/an-introduction-to-aws-step-functions\\\/\",\"name\":\"An Introduction to AWS Step Functions - Sinch MessageMedia\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/blog\\\/an-introduction-to-aws-step-functions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/blog\\\/an-introduction-to-aws-step-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/messagemedia.com\\\/wp-content\\\/uploads\\\/2019\\\/04\\\/An-Introduction-to-AWS-Step-Functions.png\",\"datePublished\":\"2019-04-18T04:46:21+00:00\",\"description\":\"Discover how Step Functions revolutionize your workflow orchestration, offering seamless integration, scalability, and reliability. Dive into the fundamentals, explore use cases, and unlock the potential of serverless architecture.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/blog\\\/an-introduction-to-aws-step-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-AU\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/messagemedia.com\\\/au\\\/blog\\\/an-introduction-to-aws-step-functions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-AU\",\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/blog\\\/an-introduction-to-aws-step-functions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/messagemedia.com\\\/wp-content\\\/uploads\\\/2019\\\/04\\\/An-Introduction-to-AWS-Step-Functions.png\",\"contentUrl\":\"https:\\\/\\\/messagemedia.com\\\/wp-content\\\/uploads\\\/2019\\\/04\\\/An-Introduction-to-AWS-Step-Functions.png\",\"width\":919,\"height\":288,\"caption\":\"An Introduction to AWS Step Functions\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/blog\\\/an-introduction-to-aws-step-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/messagemedia.com\\\/us\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"An Introduction to AWS Step Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/#website\",\"url\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/\",\"name\":\"Sinch MessageMedia\",\"description\":\"Business SMS &amp; Messaging Platform\",\"publisher\":{\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-AU\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/#organization\",\"name\":\"Sinch MessageMedia\",\"url\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-AU\",\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/messagemedia.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/logo-mm-sinch.svg\",\"contentUrl\":\"https:\\\/\\\/messagemedia.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/logo-mm-sinch.svg\",\"width\":1,\"height\":1,\"caption\":\"Sinch MessageMedia\"},\"image\":{\"@id\":\"https:\\\/\\\/messagemedia.com\\\/au\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"An Introduction to AWS Step Functions - Sinch MessageMedia Australia","description":"Discover how Step Functions revolutionize your workflow orchestration, offering seamless integration, scalability, and reliability. Dive into the fundamentals, explore use cases, and unlock the potential of serverless architecture. Australia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/","og_locale":"en_US","og_type":"article","og_title":"An Introduction to AWS Step Functions - Sinch MessageMedia","og_description":"Discover how Step Functions revolutionize your workflow orchestration, offering seamless integration, scalability, and reliability. Dive into the fundamentals, explore use cases, and unlock the potential of serverless architecture.","og_url":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/","og_site_name":"Sinch MessageMedia","og_image":[{"width":919,"height":288,"url":"https:\/\/messagemedia.com\/wp-content\/uploads\/2019\/04\/An-Introduction-to-AWS-Step-Functions.png","type":"image\/png"}],"twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/","url":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/","name":"An Introduction to AWS Step Functions - Sinch MessageMedia","isPartOf":{"@id":"https:\/\/messagemedia.com\/au\/#website"},"primaryImageOfPage":{"@id":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/#primaryimage"},"image":{"@id":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/messagemedia.com\/wp-content\/uploads\/2019\/04\/An-Introduction-to-AWS-Step-Functions.png","datePublished":"2019-04-18T04:46:21+00:00","description":"Discover how Step Functions revolutionize your workflow orchestration, offering seamless integration, scalability, and reliability. Dive into the fundamentals, explore use cases, and unlock the potential of serverless architecture.","breadcrumb":{"@id":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/#breadcrumb"},"inLanguage":"en-AU","potentialAction":[{"@type":"ReadAction","target":["https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-AU","@id":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/#primaryimage","url":"https:\/\/messagemedia.com\/wp-content\/uploads\/2019\/04\/An-Introduction-to-AWS-Step-Functions.png","contentUrl":"https:\/\/messagemedia.com\/wp-content\/uploads\/2019\/04\/An-Introduction-to-AWS-Step-Functions.png","width":919,"height":288,"caption":"An Introduction to AWS Step Functions"},{"@type":"BreadcrumbList","@id":"https:\/\/messagemedia.com\/au\/blog\/an-introduction-to-aws-step-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/messagemedia.com\/au\/"},{"@type":"ListItem","position":2,"name":"Blog","item":"https:\/\/messagemedia.com\/us\/blog\/"},{"@type":"ListItem","position":3,"name":"An Introduction to AWS Step Functions"}]},{"@type":"WebSite","@id":"https:\/\/messagemedia.com\/au\/#website","url":"https:\/\/messagemedia.com\/au\/","name":"Sinch MessageMedia","description":"Business SMS &amp; Messaging Platform","publisher":{"@id":"https:\/\/messagemedia.com\/au\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/messagemedia.com\/au\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-AU"},{"@type":"Organization","@id":"https:\/\/messagemedia.com\/au\/#organization","name":"Sinch MessageMedia","url":"https:\/\/messagemedia.com\/au\/","logo":{"@type":"ImageObject","inLanguage":"en-AU","@id":"https:\/\/messagemedia.com\/au\/#\/schema\/logo\/image\/","url":"https:\/\/messagemedia.com\/wp-content\/uploads\/2024\/03\/logo-mm-sinch.svg","contentUrl":"https:\/\/messagemedia.com\/wp-content\/uploads\/2024\/03\/logo-mm-sinch.svg","width":1,"height":1,"caption":"Sinch MessageMedia"},"image":{"@id":"https:\/\/messagemedia.com\/au\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/messagemedia.com\/au\/wp-json\/wp\/v2\/blog\/1881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/messagemedia.com\/au\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/messagemedia.com\/au\/wp-json\/wp\/v2\/types\/blog"}],"version-history":[{"count":0,"href":"https:\/\/messagemedia.com\/au\/wp-json\/wp\/v2\/blog\/1881\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/messagemedia.com\/au\/wp-json\/wp\/v2\/media\/8917"}],"wp:attachment":[{"href":"https:\/\/messagemedia.com\/au\/wp-json\/wp\/v2\/media?parent=1881"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/messagemedia.com\/au\/wp-json\/wp\/v2\/blog_category?post=1881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}