Giter VIP home page Giter VIP logo

serverless-step-functions-local's Introduction

serverless-step-functions-local

Run AWS step functions offline with Serverless!

This is a plugin for the Serverless Framework. It uses stepfunctions-localhost to emulate step functions with AWS' provided tool for local development.

Requirements

Install

npm install serverless-step-functions-local -D

Getting Started

You'll need to add this plugin to your serverless.yml. The plugins section should look something like this when you're done:

plugins:
  ...
  - serverless-step-functions
  - serverless-step-functions-local
  - serverless-offline-lambda
  - serverless-offline
  ...

Then, add a new section to config with accountId and region parameters:

custom:
  stepFunctionsLocal:
    accountId: 101010101010
    region: us-east-1

Although not neccessary, it's strongly recomended to add the folder with the downloaded step function executables to .gitignore. By default, this path is ./.step-functions-local.

The plugin binds to port 8083, this cannot be changed. The plugin binds to port 8083, since stepfunctions-localhost doesn't support dynamic port, it can't be changed.

It also adds an environment variable for each created state machine that contains the ARN for it. These variables are prefixed by OFFLINE_STEP_FUNCTIONS_ARN_, so the ARN of a state machine named 'WaitMachine', for example could be fetched by reading OFFLINE_STEP_FUNCTIONS_ARN_WaitMachine.

Options

(These go under custom.stepFunctionsLocal.)

  • accountId (required) your AWS account ID
  • region (required) your AWS region
  • lambdaEndpoint (defaults to http://localhost:3000 from serverless offline) the endpoint for the lambda service. This plugin tries to use the configured value from serverless-offline
  • path (defaults to ./.step-functions-local) the path to store the downloaded step function executables
  • TaskResourceMapping allows for Resource ARNs to be configured differently for local development. (Serverless 3.x prefixes the lambda function names with the stack name and the stage)
  • eventBridgeEvents allows sending EventBridge events on execution status changes
    • enabled (bool) enabled or disable this feature. Disabled by default.
    • endpoint Endpoint for sending events to eg. for serverless-offline-aws-eventbridge would be http://localhost:4010
  • externalInstance allows to set a flag to use external instance of local step functions. Disabled by default.
  • stepFunctionsEndpoint (defaults to http://localhost:8083) the endpoint for the step functions service

Full Config Example

service: local-step-function

plugins:
  - serverless-step-functions
  - serverless-step-functions-local
  - serverless-offline-lambda
  - serverless-offline

provider:
  name: aws
  runtime: nodejs14.x # supported nodejs versions: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html


custom:
  stepFunctionsLocal:
    accountId: 101010101010
    region: us-east-1
    TaskResourceMapping:
      FirstState: arn:aws:lambda:us-east-1:101010101010:function:${self:service}-${opt:stage, self:provider.stage}-hello
      FinalState: arn:aws:lambda:us-east-1:101010101010:function:${self:service}-${opt:stage, self:provider.stage}-hello
    eventBridgeEvents:
      enabled: true
      endpoint: http://localhost:4010
  sqsUrl: http://localhost:4566/101010101010/example-queue

functions:
  hello:
    handler: handler.hello

stepFunctions:
  stateMachines:
    WaitMachine:
      definition:
        Comment: "An example of the Amazon States Language using wait states"
        StartAt: FirstState
        States:
          FirstState:
            Type: Task
            Resource: Fn::GetAtt: [hello, Arn]
            Next: send_message
          send_message:
            Type: Task
            Resource: arn:aws:states:::sqs:sendMessage
            Parameters:
              QueueUrl: ${self:custom.sqsUrl}
              "MessageBody.$": "$"
            Next: wait_using_seconds
          wait_using_seconds:
            Type: Wait
            Seconds: 10
            Next: FinalState
          FinalState:
            Type: Task
            Resource: Fn::GetAtt: [hello, Arn]
            End: true

Start your local step functions

To run your local step function, use this command:

aws stepfunctions --endpoint-url http://localhost:8083 start-execution --state-machine-arn arn:aws:states:us-east-1:101010101010:stateMachine:servicename-stagename-hello --input '{"hello": ["world"]}'

Running external instance

To run external step functions local using docker, use this command:

docker run \
    -p 8083:8083 \
    -e "AWS_ACCOUNT_ID=101010101010" \
    -e "AWS_DEFAULT_REGION=us-east-1" \
    -e "LAMBDA_ENDPOINT=http://localhost:4000" \
    amazon/aws-stepfunctions-local

serverless-step-functions-local's People

Contributors

codetheweb avatar danielpoonwj avatar a-marcel avatar dicrescenzoa avatar kuba9392 avatar adambartholomew avatar czlowiek488 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.