【问题标题】:Serverless cloudformation: cast environment variable to number无服务器 cloudformation:将环境变量转换为数字
【发布时间】:2022-10-13 12:19:58
【问题描述】:

我正在使用serverless step functions plugin,我需要从环境变量中指定超时。问题是我不知道如何将其转换为数字(Cloudformation 在运行时预期),我没有找到任何辅助函数来执行此操作:

serverless.yml

stepFunctions:
  stateMachines:
    MyStateMachine:
      name: 'MyStateMachine'
      definition:
        Comment: ''
        StartAt: Worker
        States:
          Worker:
            Type: Task
            Resource: arn:aws:states:::ecs:runTask.waitForTaskToken
            InputPath: $
            ResultPath: $
            OutputPath: $
            TimeoutSeconds: ${env:TIMEOUT_SECONDS} # Need this to be casted to a number
            Parameters:
              # ...              
            Catch:
              - ErrorEquals: ["States.ALL"]
                Next: Failure
            Next: Success

          Failure:
            Type: Fail

          Success:
            Type: Succeed

【问题讨论】:

    标签: amazon-web-services yaml amazon-cloudformation serverless serverless-framework


    【解决方案1】:

    使用Cloudformation,您可以在Cloudformation 文件的顶部声明一个参数

    使参数值默认为环境变量

         Parameters:
              TimeoutSecs:
                Description: The specified timeout from an environment
                Type: Number
                Timeout: "60"
                Default: ${{ env:TIMEOUT_SECONDS} }
    

    根据文档将Type 定义为数字 来自AWS Cloudformation Docs

    Number 整数或浮点数。 AWS CloudFormation 将参数值验证为数字;但是,当您在模板中的其他位置使用参数时(例如,通过使用 Ref 内部函数),参数值将变为字符串。

    例如,用户可以指定“8888”。

    列表 由逗号分隔的整数或浮点数数组。 AWS CloudFormation 将参数值验证为数字;但是,当您在模板中的其他地方使用参数时(例如,通过使用 Ref 内部函数),参数值将变为字符串列表。

    例如,用户可以指定 "80,20",而 Ref 将导致 ["80","20"]

    【讨论】:

      猜你喜欢
      • 2021-11-17
      • 2016-08-08
      • 2011-06-21
      • 1970-01-01
      • 2021-09-13
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      相关资源
      最近更新 更多