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 :

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:
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:
Dummy:
Type: AWS::SNS::Topic
Outputs:
Result:
Description: Result
Value: 13