Local emulation of AWS Step Functions with serverless framework
This repository is the serverless framework example for local invocation of AWS Step Functions modifying the existing serverless-step-functions-local plugin.
-
create an AWS profile (for example,
test-profile
) in~/.aws/crendential
[test-profile] aws_access_key_id=<Your AWS Access Key ID> aws_secret_access_key=<Your AWS Secret Access Key>
-
install
unbuffer
command (Mac OS)$ brew install expect
type
unbuffer
command.$ unbuffer can't find package Expect while executing "package require Expect" (file "/opt/homebrew/bin/unbuffer" line 6)
If the above error occurs, execute
$ sudo vi /opt/homebrew/bin/unbuffer
and replace
# -*- tcl -*- # The next line is executed by /bin/sh, but not tcl \ exec tclsh "$0" ${1+"$@"} package require Expect
with
#\ exec expect "$0" ${1+"$@"}
-
install aws-sam-cli
$ brew tap aws/tap $ brew install aws-sam-cli
-
install serverless framework
$ npm install -g serverless
-
install related package
$ npm install stepfunctions-localhost serverless-pseudo-parameters serverless-dotenv-plugin serverless-step-functions serverless-step-functions-local serverless-sam $ pip install pyyaml
-
version
$ sam --version SAM CLI, version 1.56.1
$ npx serverless --version Framework Core: 2.72.3 (local) Plugin: 5.5.4 SDK: 4.3.2 Components: 3.18.2
First, deploy with serverless framework.
- execute the below command
$ npx serverless deploy --aws-profile test-profile
-
package with serverless framework
$ npx serverless package --aws-profile test-profile
-
export the SAM template and execute the converter (if there is no template.yml)
$ npx serverless sam export --output ./template.yml $ python ./scripts/sam_template_converter/main.py
cf. https://qiita.com/hayao_k/items/f8c7ad5e35e29d590957 (in Japanese)
-
build SAM layer
$ sam build --use-container LambdaLayer
if there is an error such as
Error: PythonPipBuilder:CopySource - [Errno 2] No such file or directory: '/tmp/samcli/source/.serverless/pythonRequirements.zip'
remove the
layer_requirements/.serverless
folder and build again:$ rm -rf ./layer_requirements/.serverless
-
execute two commands below on independent terminals to invoke local Step Functions
cf. https://kazuhira-r.hatenablog.com/entry/2019/04/23/000355 (in Japanese)$ npx serverless stepf offline --aws-profile test-profile
$ aws stepfunctions --endpoint https://localhost:8083 start-execution --state-machine arn:aws:states:ap-northeast-1:012345678901:stateMachine:StateMachine --name Lambda_local --input ""
-
if you want to invoke a single Lambda function,
$ aws lambda invoke /dev/null \ --endpoint-url https://localhost:4002 \ --function-name ServerlessStepFunctionsExampleentrypoint