【问题标题】:when locally running 'aws stepfunctions' how to use the statemachine ASL file for the definition在本地运行“aws stepfunctions”时,如何使用状态机 ASL 文件进行定义
【发布时间】:2021-07-16 04:33:04
【问题描述】:

在 AWS samaws 环境中是否可以使用在 SAM 项目中定义的相同 json 文件在本地调用 SAM 步骤函数?

我正在尝试在 AWS“Stock Trade”示例中本地调用示例步骤函数,但提供 .asl.json 文件的 filename 而不是手动重新创建 json(以-reference 变量名)并将其粘贴到命令行。

我有两个必要的 docker 容器正在运行,例如

sam local start-lambda

docker run -p 8083:8083 amazon/aws-stepfunctions-local

在单独的 Mac 终端窗口中运行。

步骤函数在文件statemachine/stock_trader.asl.json中定义

问题是我能找到的 100% 的指南只是简单地重复通过标准输入而不是文件名创建状态机的同一 AWS 示例。

虽然我找不到任何记录在案的示例,但预感我发现我可以使用 --definition file://PATH/FILE 提供一个 json 文件,例如

aws stepfunctions --endpoint http://localhost:8083 create-state-machine \
    --definition file://statemachine/stock_trader.asl.json \
    --name StockTradingStateMachine --role-arn "arn:aws:iam::012345678901:role/DummyRole"

但是,这(自然)只是插入文字 json 文件...例如,使用诸如 "Resource": "${StockCheckerFunctionArn}" 之类的变量名称,而不是资源的本地 ARN。

有没有办法取消引用 json 文件中的变量名?还是我们必须为每个引用的资源手动创建(并维护)具有手动替换的“本地”ARN 的重复 json 文件?

【问题讨论】:

    标签: aws-step-functions aws-sam


    【解决方案1】:

    我希望有更好的解决方案出现,但这是我目前所做的......

    我在 Bash 脚本中调用状态机,该脚本调用 sed 以手动将通用模板重新写入 json 文件,并将参数替换为 bash 脚本中指定的值

    这是一个小的单变量示例:

    $template_source=template.json 
    $sm_actual=ready_to_use.json # a "runable" tmp file with placeholders replaced with actual variable names
    $sm_function_tag=REPLACEMENT_TEXT
    $sm_name=MyFunctionName
    $sm_arn=arn:aws:states:us-east-1:MYACCOUNT:stateMachine:$sm_name
    
    # THIS creates the de-referenced json file
    # (can have 5 -e params if replacing 5 placeholders)
    sed -e "s/REPLACEME/$sm_function_tag/"  ./statemachine/$template_source > ./statemachine/$sm_actual  || exit 2
    
    # THIS copies the file into a bash variable 
    json_text=$(cat ./statemachine/$sm_actual)
    
    # This supplies the de-referenced json file contents to the `--definition` parameter:
    aws stepfunctions CMD-INVOKE-UPLOAD-WHATEVER \
      --no-cli-pager \
      --state-machine-arn $sm_arn \
      --role-arn  $sm_role_arn \
      --definition "$json_text" \
      --profile AWS-PROFILE-TO-USE || exit 2
    

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 2021-03-24
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2017-03-16
      • 1970-01-01
      • 2019-11-17
      相关资源
      最近更新 更多