Follow the step-by-step instructions below to create a State Machine. Click on each step number to expand the section.

  • Create a state machine by using the AWS Step Functions console

    To create a state machine, you use the AWS Step Functions console. Step Functions is a web service that lets you coordinate the components of distributed applications and microservices by using visual workflows. The Lambda functions are invoked when the state machine is run and reaches the Task state.

    For this project, the state machine performs the following tasks:

    • Creates an AppStream 2.0 image builder.
    • Gets the status of the AppStream 2.0 image builder and fleet.
    • Sends a streaming URL.
    • Gets the AppStream 2.0 image name.
    • Creates an AppStream fleet and stack, and associates the fleet with a stack.
    • Stops the AppStream 2.0 fleet and image builder.
     

    To create the state machine, perform the following steps:

    1. Open the AWS Step Functions console at https://console.aws.amazon.com/states/.
    2. Do one of the following:
      • If you haven’t created any State Machines functions, a Getting Started page displays. Choose Getting Started, and then choose State Machines.
      • If you have created a State Machines function, in the upper right corner of the State machines page, choose Create state machine.
    3. On the Define State Machine page, keep Author from code snippet selected.
    4. In the Type section, keep Standard selected.
    5. In the State machine definition section, delete the placeholder code and paste the following code in the edit window.
    {
      "StartAt": "Create Image Builder",
      "States": {
        "Create Image Builder": {
          "Type": "Task",
          "Resource":
          "arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_createimagebuilder",
          "ResultPath": "$.type",
          "Next": "Resource Created?"
        },
        "Resource Created?": {
          "Type": "Choice",
          "Choices": [
            {
              "Variable": "$.type",
              "NumericEquals": -1,
              "Next": "Creation Failed"
            },
            {
              "Variable": "$.type",
              "NumericEquals": 1,
              "Next": "Sleep"
            },
            {
              "Variable": "$.type",
              "NumericEquals": 2,
              "Next": "Sleep"
            }
          ],
          "Default": "Sleep"
        },      
        "Sleep": {
          "Type": "Wait",
          "Seconds": 300,
          "Next": "Get Status"
        }, 
        "Get Status": {
          "Type": "Task",
          "Resource":
          "arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getstatus",
          "Next": "Instance Status",
          "ResultPath": "$.status"
        },
        "Instance Status": {
          "Type": "Choice",
          "Choices": [
            {
              "Variable": "$.status",
              "NumericEquals": -1,
              "Next": "Creation Failed"
            },    
            {
              "Variable": "$.status",
              "NumericEquals": 1,
              "Next": "Send StreamingURL"
            }
          ],
          "Default": "Sleep"
        },   
        "Send StreamingURL": {
          "Type": "Task",
          "Resource":
          "arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_sendstreamingurl",
          "Next": "Split",
          "ResultPath": "$.status"      
        },
        "Split": {
            "Type": "Choice",
          "Choices": [
            {
              "Variable": "$.type",
              "NumericEquals": 1,
              "Next": "Wait For Snapshot"
            },
            {
              "Variable": "$.type",
              "NumericEquals": 2,
              "Next": "1 Hour Timer"
            }
          ],
          "Default": "1 Hour Timer"
        }, 
        "Wait For Snapshot": {
          "Type": "Wait",
          "Seconds": 1500,
          "Next": "IB Status"
        },  
        "IB Status": {
            "Type": "Task",
            "Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getstatus",
            "ResultPath": "$.status",
            "Next": "Snapshotting?"
        },  
        "Snapshotting?": {
          "Type": "Choice",
          "Choices": [
            {
              "Variable": "$.status",
              "NumericEquals": 2,
              "Next": "Get ImageName"
            },
            {
              "Variable": "$.status",
              "NumericEquals": 3,
              "Next": "Get ImageName"
            }
          ],
          "Default": "Wait For Snapshot"
        },   
        "Get ImageName": {
          "Type": "Task",
          "Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getimagename",
          "ResultPath": "$.imagename",
          "Next": "Wait For ImageReady"
        }, 
        "Wait For ImageReady": {
          "Type": "Wait",
          "Seconds": 300,
          "Next": "Image Status"
        },
        "Image Status": {
          "Type": "Task",
          "Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getstatus",
          "ResultPath": "$.status",
          "Next": "ImageReady?"
        },
        "ImageReady?": {
          "Type": "Choice",
          "Choices": [
            {
              "Variable": "$.status",
              "NumericEquals": 3,
              "Next": "Create Fleet"
            }
          ],
          "Default": "Wait For ImageReady"
        },
        "Create Fleet": {
          "Type": "Task",
          "Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_createfleet",
          "ResultPath": "$.type",
          "Next": "Resource Created?"
        },
        "1 Hour Timer": {
          "Type": "Wait",
          "Seconds": 3600,
          "Next": "StopResources"
        },  
        "Creation Failed": {
          "Type": "Fail",
          "Cause": "Process Failed",
          "Error": "Process Failed"
        },    
        "StopResources": {
          "Type": "Task",
          "Resource":
          "arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_stopresources",
          "End": true
        }
      }
    }  
    

     6. Replace the following with your own values:

          • Replace <AWS-ACCOUNT-ID> with your AWS account number.

          • Replace <REGION-CODE> with the AWS Region you chose. The value must be in all lowercase.

     7. Choose Next.
     
     8. For Name, type examplecorp_eao.
     
     9. On the Permissions section, select Choose an existing role.
     
     10. In the Existing roles dropdown choose examplecorp_eao_role, the IAM role that you created in Module 2.
     
     11. Choose Create state machine.
     
     12. At the top of the page, in the Details section, the ARN of the state machine displays. Make a note of this ARN. This ARN is required later in the project and is formatted as arn:aws:states:<REGION-CODE>:<AWS_ACCOUNT-ID>:stateMachine:<STATE-MACHINE-NAME>