【问题标题】:How to perform mathematical operations in CloudFormation?CloudFormation 中的数学运算
【发布时间】:2016-04-14 02:36:30
【问题描述】:

是否可以在 Cloudformation json 模板中执行某种数学运算?

我遇到了两个有用的方面: 1.设置IOPS,需要是磁盘大小的比例。 2. 设置RDS可用存储空间的Cloud Watch警报。将其设置为磁盘大小的百分比会很有用。

【问题讨论】:

    标签: math amazon-cloudformation


    【解决方案1】:

    Intrinsic Functions 不支持的 CloudFormation 模板中执行自定义逻辑有两种通用解决方案,例如数学运算:

    1。自定义资源

    写一个Custom Resource 来执行你的数学运算,将输入作为属性传递,将输出作为值传递。这是一个独立的工作示例,它返回 Result: 13 作为堆栈输出:

    Resources:
      LambdaExecutionRole:
        Type: AWS::IAM::Role
        Properties:
          AssumeRolePolicyDocument:
            Version: '2012-10-17'
            Statement:
            - Effect: Allow
              Principal: {Service: [lambda.amazonaws.com]}
              Action: ['sts:AssumeRole']
          Path: "/"
          ManagedPolicyArns:
          - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      AddFunction:
        Type: AWS::Lambda::Function
        Properties:
          Handler: index.handler
          Role: !GetAtt LambdaExecutionRole.Arn
          Code:
            ZipFile: !Sub |
              var response = require('cfn-response');
              exports.handler = function(event, context) {
                var result = parseInt(event.ResourceProperties.Op1) + parseInt(event.ResourceProperties.Op2);
                response.send(event, context, response.SUCCESS, {Value: result});
              };
          Runtime: nodejs
      AddTest:
        Type: Custom::Add
        Properties:
          ServiceToken: !GetAtt AddFunction.Arn
          Op1: 8
          Op2: 5
    Outputs:
      Result:
        Description: Result
        Value: !GetAtt AddTest.Value
    

    2。模板预处理器

    使用您选择的全功能模板语言/平台编写“源”模板,该模板会生成有效的 CloudFormation 模板作为输出。您可以使用功能齐全的特定于 CloudFormation 的库,例如 troposphere,但编写一个简单的预处理器层来满足您的用例和编程语言/库偏好也很容易。

    我目前的选择是嵌入式 Ruby (ERB),主要是因为我已经熟悉它。下面是一个示例 template.yml.erb 文件,它使用嵌入式 Ruby 语法执行数学运算,将 Result: 13 作为堆栈输出返回:

    Resources:
      # CloudFormation stacks require at least one resource
      Dummy:
        Type: AWS::SNS::Topic
    Outputs:
      Result:
        Description: Result
        Value: <%= 8 + 5 %>
    

    要处理模板,请运行cat template.yml.erb | ruby -rerb -e "puts ERB.new(ARGF.read, nil, '-').result" &gt; template.yml,这会将以下 CloudFormation-ready 模板写入template.yml

    Resources:
      # CloudFormation stacks require at least one resource
      Dummy:
        Type: AWS::SNS::Topic
    Outputs:
      Result:
        Description: Result
        Value: 13
    

    【讨论】:

    • 在第一个示例中,是否会启动整个 lambda 函数来执行 5+8?
    • @PaulaT 你明白了
    • 如果服务频繁部署,似乎有点过分,而且成本高昂。
    • 一个简单函数的 Lambda 成本约为每百万次调用 22 美分,因此如果该成本对于您的用例而言过高,请选择选项 2。
    【解决方案2】:

    我在使用预处理系统方面取得了巨大成功。一般来说,我会查看模板和可变数据,并根据这些数据做出决策。

    这里有一篇关于这个问题的博文:

    http://krogebry.blogspot.com/2014/12/cloudformation-discovery-and.html

    在撰写本文时,我们遇到了许多 VPC 和非常复杂的帐户管理系统的问题。您所说的似乎与您的想法相似,因为您有一些可能需要类似方法的复杂(且整洁!)要求。此外,像这样编译堆栈还有一个额外的好处,那就是能够根据您的用例强制执行某些规则(例如 dev 中没有 0.0.0.0:22 或没有 IOPS>x 阈值)。

    【讨论】:

    • 很遗憾,此链接已失效。
    猜你喜欢
    • 2015-02-04
    • 1970-01-01
    • 2016-07-29
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2013-07-12
    相关资源
    最近更新 更多