$30 off During Our Annual Pro Sale. View Details »

Local testing of CDK-defined Step Functions state machine

Local testing of CDK-defined Step Functions state machine

This presentation describes the different pieces of the puzzle needed to unit test a Step Functions state machine defined with CDK code.

Tweet

More Decks by Jérôme Van Der Linden

Other Decks in Technology

Transcript

  1. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Jerome Van Der Linden (@jeromevdl)
    Automate Local Testing of CDK-defined
    Step Functions
    Solutions Architect Builder @ AWS

    View Slide

  2. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Who am I ?
    Jérôme Van Der Linden
    Solutions Architect Builder @ AWS (Geneva, Switzerland)
    Former consultant, developer, tech lead, agile & devops
    coach, architect @ OCTO Technology
    Husband and Dad of 3
    jeromevdl

    View Slide

  3. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Objec8ves
    • Unit testing AWS Step Functions State Machines
    • Defined with CDK code
    • Locally without reaching the cloud
    • Automate as much as possible the process

    View Slide

  4. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Big Picture

    View Slide

  5. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Big Picture

    View Slide

  6. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    CDK-defined Step Functions State Machine

    View Slide

  7. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    CDK-defined Step Functions State Machine

    View Slide

  8. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    CDK-defined Step Functions State Machine

    View Slide

  9. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    CDK Deploy

    View Slide

  10. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    CDK Deploy
    Since I recorded this video, I
    discovered cdk-asl-extractor
    It permits to retrieve the ASL without
    deploying the stack. 😀🤩
    Use cdk synth to get the
    CloudFormation template
    and use cdk-asl-extractor
    to get the ASL from the template.

    View Slide

  11. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Outputs
    🌀🌩☠
    ❗💥🤯
    output.json
    Stack name Output name Randomly generated string
    Construct
    name

    View Slide

  12. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Extracting the State Machine definition
    Rocks!

    View Slide

  13. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    State Machine definition
    state_machine.asl.json

    View Slide

  14. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Wrap-up: CDK code to JSON ASL

    View Slide

  15. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Mocked service integration
    MockConfigFile.json
    State Machine name
    Mocked states
    Mocked responses names
    Mocked responses definitions

    View Slide

  16. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Step Func8ons Local
    Start stepfunctions-local:
    Mocked Configuration

    View Slide

  17. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Step Functions Local
    Create the state machine locally:
    State machine definition
    (ASL / JSON)
    State Machine name
    (same as in the MockConfigFile.json)

    View Slide

  18. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Wrap-up: Step-Func8ons local bootstrap

    View Slide

  19. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Unit Tests
    State Machine name
    (same as in the MockConfigFile.json)
    Test case name
    (same as in the MockConfigFile.json)
    Local endpoint

    View Slide

  20. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Unit Tests
    Test case name
    (same as in the MockConfigFile.json)

    View Slide

  21. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Wrap-up

    View Slide

  22. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Wrap-up
    1. Retrieve the state machine ASL
    from the CDK code

    View Slide

  23. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Wrap-up
    1. Retrieve the state machine ASL
    from the CDK code
    2. Create Mocked Service
    integration config

    View Slide

  24. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Wrap-up
    1. Retrieve the state machine ASL
    from the CDK code
    2. Create Mocked Service
    integration config
    3. Start Step Functions local with
    this mocked configuration
    4. Create a State Machine locally

    View Slide

  25. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Wrap-up
    1. Retrieve the state machine ASL
    from the CDK code
    2. Create Mocked Service
    integration config
    3. Start Step Functions local with
    this mocked configuration
    4. Create a State Machine locally
    5. Run your tests (using the SDK)

    View Slide

  26. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Wrap-up
    1. Retrieve the state machine ASL
    from the CDK code
    2. Create Mocked Service
    integration config
    3. Start Step Functions local with
    this mocked configuration
    4. Create a State Machine locally
    5. Run your tests (using the SDK)

    View Slide

  27. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Resources

    View Slide

  28. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    • Testing Step Functions State Machines Locally
    • https://docs.aws.amazon.com/step-functions/latest/dg/sfn-local.html
    • Using Mocked Service Integrations
    • https://docs.aws.amazon.com/step-functions/latest/dg/sfn-local-test-sm-exec.html
    • Mocking service integrations with AWS Step Functions Local
    • https://aws.amazon.com/blogs/compute/mocking-service-integrations-with-aws-step-functions-local/
    • Code Sample GitHub
    • https://github.com/aws-samples/step-functions-direct-integrations/tree/main/direct-integration/infra

    View Slide

  29. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark
    Thank you
    jeromevdl
    Jérôme Van Der Linden

    View Slide