CloudFormation 中的数学运算 [英] Mathematical operations in CloudFormation

查看:36
本文介绍了CloudFormation 中的数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我遇到过两个有用的领域:1. 设置IOPS,需要是磁盘大小的比率.2. 为RDS Free存储空间设置云看闹钟.将其设置为磁盘大小的百分比会很有用.

解决方案

有两种通用解决方案可以在

资源:LambdaExecutionRole:类型:AWS::IAM::角色特性:AssumeRolePolicyDocument:版本:'2012-10-17'陈述:- 效果:允许委托人:{服务:[lambda.amazonaws.com]}行动:['sts:AssumeRole']小路: "/"ManagedPolicyArns:- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole添加功能:类型:AWS::Lambda::Function特性:处理程序:index.handler角色:!GetAtt LambdaExecutionRole.Arn代码:压缩文件:!Sub |var response = require('cfn-response');export.handler = 函数(事件,上下文){var 结果 = parseInt(event.ResourceProperties.Op1) + parseInt(event.ResourceProperties.Op2);response.send(event, context, response.SUCCESS, {Value: result});};运行时:nodejs添加测试:类型:自定义::添加特性:ServiceToken: !GetAtt AddFunction.Arn操作 1:8操作 2:5输出:结果:描述:结果值: !GetAtt AddTest.Value

2.模板预处理器

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

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

资源:# CloudFormation 堆栈至少需要一种资源假的:类型:AWS::SNS::主题输出:结果:描述:结果值:<%=8+5%>

要处理模板,请运行 cat template.yml.erb |ruby -rerb -e "把 ERB.new(ARGF.read, nil, '-').result" >template.yml,它将以下 CloudFormation-ready 模板写入 template.yml:

资源:# CloudFormation 堆栈至少需要一种资源假的:类型:AWS::SNS::主题输出:结果:描述:结果价值:13

Is it possible to perform some sort of mathematical operation in Cloudformation json template?

There are two areas that I've encountered where this would be useful: 1. Setting IOPS which needs to be a ratio of the disk size. 2. Setting Cloud Watch Alarms for RDS Free Storage Space. It would be useful to set this as a % of the disk size.

解决方案

There are two general solutions to performing custom logic in CloudFormation templates not supported by Intrinsic Functions, such as mathematical operations:

1. Custom Resource

Write a Custom Resource to execute your mathematical operation, passing inputs as Properties and outputs as Values. Here's a self-contained working example that returns Result: 13 as a Stack Output:

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. Template Preprocessor

Use a full-featured templating language/platform of your choice to write a 'source' template that produces a valid CloudFormation-template as output. You can use a full-featured CloudFormation-specific library like troposphere, but it's also easy enough to write a simple preprocessor layer to suit your use-case and programming-language/library preferences.

My current choice is embedded Ruby (ERB), mostly because I'm already familiar with it. Here's an example template.yml.erb file using embedded Ruby syntax to perform a mathematical operation that returns Result: 13 as a Stack Output:

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

To process the template, run cat template.yml.erb | ruby -rerb -e "puts ERB.new(ARGF.read, nil, '-').result" > template.yml, which will write the following CloudFormation-ready template to template.yml:

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

这篇关于CloudFormation 中的数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆