├── images ├── 1.png ├── 2.png └── 3.png ├── .gitignore └── README.md /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vgaltes/LearnStepFunctions/HEAD/images/1.png -------------------------------------------------------------------------------- /images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vgaltes/LearnStepFunctions/HEAD/images/2.png -------------------------------------------------------------------------------- /images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vgaltes/LearnStepFunctions/HEAD/images/3.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | .serverless/ 61 | .DS_Store 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn Step Functions 2 | 3 | This is a workshop to learn how to develop and deploy [AWS Step Functions](https://aws.amazon.com/documentation/step-functions/). You can find some tutorials I've written [here](http://vgaltes.com/tags/#serverless). 4 | 5 | ## Pre-requisites 6 | 7 | ### Serverless framework 8 | 9 | We're going to use the [serverless framework](http://serverless.com) to deploy and test the step functions. The serverless framework is a NodeJS library, so first we'll need to install NodeJs. Go to the [NodeJS] website and install it in your favourite OS. 10 | 11 | Once we have NodeJS install, we can install the serverless framework. To do that, open a terminal window and type `npm install serverless -g`. That will install the framework. To check that the installation finished successfully, type `sls -v` to see the installed version of the framework. At the time of writting, you should see `1.16.1` 12 | 13 | You'll need to install the [plugin](https://github.com/horike37/serverless-step-functions) for step functions. To do that, create your working folder and, inside it type `npm install --save serverless-step-functions` 14 | 15 | ### .Net Core 16 | 17 | You can download the last version of the SDK, but we'll need to target netcoreapp1.0. So, go to the [official website](https://www.microsoft.com/net/core) and follow the instructions for your favourite OS. 18 | 19 | ### Javascript 20 | 21 | If you've followed the steps to install the serverless framework, you should be in a good position to develop using Javascript. 22 | 23 | ### Python 24 | 25 | Download and install the latest version of Python from the [official website](https://www.python.org/downloads/release/python-361). Take a look [here](https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-macos) for extra help. 26 | 27 | ### VSCode 28 | 29 | I'm going to use VSCode in this workshop (you can use whatever editor you'd like). If you don't have VSCode installed, go to the [official website](https://code.visualstudio.com/) and follow the instructions for you favourite OS. 30 | 31 | There are a couple of extensions that will make our live easier. For F# development, download the [ionide](http://ionide.io/) extension. For C# development, download the [C# extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp). 32 | 33 | ### AWS 34 | 35 | You'll need an AWS account to follow the workshop. You can get one for free [here](https://aws.amazon.com/free/). Once you have the account, you'll need to setup an account so that the serverless framework can interact with AWS. Please, follow the steps explained [here](https://serverless.com/framework/docs/providers/aws/guide/credentials/). My preferred option is to set up an AWS profile. It's a good idea that you give access from the console to that user, so you can connect to it and see the step function running. 36 | 37 | ## Creating the first function 38 | 39 | Create a simple Step Function called *rpg* with an state called *Attack* of type Pass. 40 | 41 | Deploy the Step Function and test it. 42 | 43 | Check the Step Function in the AWS console. 44 | 45 | ![step 1](/images/1.png) 46 | 47 | Check the [Step 1 branch](https://github.com/vgaltes/learnstepfunctions/tree/Step1) to see the final solution. 48 | 49 | ## A parallel execution 50 | 51 | Create a parallel state called *CalculateMultipliers* with two branches. The first branch should have a state of type Pass called *AttackMultiplier* and the second branch should have a state of type Pass called *DefenseMultiplier*. 52 | 53 | Create the step that collects the result of the paralles execution and name it *Result*. 54 | 55 | Link the state created in the previous step to the parallel one. 56 | 57 | Deploy and test the Step Function. 58 | 59 | Check the Step Function in the AWS console. 60 | 61 | ![step 2](/images/2.png) 62 | 63 | Check the [Step 2 branch](https://github.com/vgaltes/learnstepfunctions/tree/Step2) to see the solution. 64 | 65 | ## Choices 66 | 67 | Rename the *Result* state to *CalculateAttackResult*. Change its type to Choice and create two choices, one for a number greater than 0 and the other for a number less or equal to 0 (use any variable you want depending on what you want to pass to the function). 68 | 69 | Link the first choice to an state of type Fail named *Alive* and link the second choice to an state of type Succeed named *Dead*. 70 | 71 | Deploy and test the function. 72 | 73 | Check the Step Function in the AWS console. 74 | 75 | ![step 3](/images/3.png) 76 | 77 | Check the [Step 3 branch](https://github.com/vgaltes/learnstepfunctions/tree/Step3) for the final solution. 78 | 79 | ## Our first lambda 80 | 81 | Create a Lambda using C#, call it *AttackMultiplierLambda*. The lambda will receive a JSON like this: 82 | 83 | { 84 | "Attack":{ 85 | "Player":{ 86 | "Level":10, 87 | "Live":50 88 | }, 89 | "Strength":10 90 | }, 91 | "Defense":{ 92 | "Player":{ 93 | "Level":8, 94 | "Live":20 95 | }, 96 | "Strength": 30 97 | } 98 | } 99 | 100 | Create the classes that will allow us to receive this input (and return it). 101 | 102 | Change the code to generate a number between 0 and 2 (0, 1 or 2) and multiply the strength of the attack to it. Return the whole object. 103 | 104 | Deploy the function and test it. 105 | 106 | Check the [Step 4 branch](https://github.com/vgaltes/learnstepfunctions/tree/Step4) for the final solution. 107 | 108 | ## Defense multiplier in Python 109 | 110 | Create a Lambda using Python. 111 | 112 | Change the code to generate a number between 0 and 2 (0, 1 or 2) and multiply the strength of the defense to it. Return the whole object. 113 | 114 | Deploy the function and test it. 115 | 116 | Check the [Step 5 branch](https://github.com/vgaltes/learnstepfunctions/tree/Step5) for the final solution. 117 | 118 | ## Calculate result in JS 119 | 120 | Create a Lambda using NodeJS. 121 | 122 | Change the code to calculate the result of the attack, i.e. substract the result of the substraction of the strength of the defense to the strength of the attack, to the player's live. 123 | 124 | Return the result (the whole object). 125 | 126 | Deploy and test the Lambda. 127 | 128 | Check the [Step 6 branch](https://github.com/vgaltes/learnstepfunctions/tree/Step6) for the final solution. 129 | 130 | ## Joining all together 131 | 132 | Modify the serverles.yml file in the root folder to replace the actual states of *CalculateAttackMultiplier*, *CalculateDefenseMultiplier* and *CalculateResult* with the recently created Lambdas. 133 | 134 | Deploy the Step Function and test it. 135 | 136 | Check the [Step 7 branch](https://github.com/vgaltes/learnstepfunctions/tree/Step7) for the final solution. 137 | 138 | ## Calling the Step Function. 139 | 140 | Create an HTTP endpoint in your Step Function to be able to call it (POST). 141 | 142 | Deploy the Step Function and test it via curl or postman. 143 | 144 | Install the aws-sdk package via npm or yarn. Create a Lambda that calls the Step Function. Get the arn of the step function via environment variable. 145 | 146 | Edit the serverles.yml file to export the arn of the Step Function. Pass it to the Lambda Function via environment variable. Add an HTTP handler to the Lambda. 147 | 148 | Grant permissions to the service to be able to execute a Step Function. 149 | 150 | Check the [Step 8 branch](https://github.com/vgaltes/learnstepfunctions/tree/Step8) for the final solution. --------------------------------------------------------------------------------