Math operations in CloudFormation

Is it possible to perform some kind of mathematical operation in a java Cloudformation template?

There are two areas that I came across when it would be useful: 1. Installing IOPS, which should be a disk size ratio. 2. Configure Cloud Watch signals for free RDS storage space. It would be helpful to set this as% of disk size.

+4
source share
2 answers

There are two general solutions for executing custom logic in CloudFormation templates that are not supported by internal functions , such as mathematical operations:

1. User resource

, . , Result: 13 :

Launch stack

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.

/ , "source", 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" > template.yml, , CloudFormation, template.yml:

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

. , .

:

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

VPC . , , , , , ( !) , . , , , (, 0.0.0.0:22 no IOPS > x threshold dev) .

0

Source: https://habr.com/ru/post/1623547/


All Articles