Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (language extension) functions that handles basic math operations #158

Open
yvele opened this issue Jun 10, 2024 · 0 comments
Open

Add (language extension) functions that handles basic math operations #158

yvele opened this issue Jun 10, 2024 · 0 comments

Comments

@yvele
Copy link

yvele commented Jun 10, 2024

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Tell us about your request

It would be useful to add a way to perform basic arithmetic mathematical operations such as addition, subtraction, multiplication and division.

Tell us about the problem you are trying to solve. What are you trying to do, and why is it hard?

Mostly to keep template DRY.

See also:

From the reddit post:

I have a pretty basic need to do some math within a template; I'd like to make the IOPS values for EBS volumes follow the max formulas that AWS supports based on Volume size parameters

Seems really silly to write a lambda that writes a stack output that I then consume as an input for an EBS volume property; by my understanding this forces me into nested templates if I want to only stack once to perform this math. I may be misunderstanding that too, not sure.

Are you currently working around this issue?

My workaround it to repeat myself, this is a real use-case:

Parameters:
  Environment:
    Description: Environment
    Type: String
    AllowedValues: [production, development]

Mappings:
  Environment:
    production:
      RateEvaluationWindowMin: 5
      RateEvaluationWindowSec: 300 # 5 x 60
      RateLimit: 1000
    development:
      RateEvaluationWindowMin: 1
      RateEvaluationWindowSec: 60 # 1 x 60
      RateLimit: 1000

Resources:
  Waf:
    Type: AWS::WAFv2::WebACL
    Properties:
      CustomResponseBodies:
        IpRequestRateExceededException:
          ContentType: APPLICATION_TEXT
          Content: !Sub
            - Exceeded limit of ${RateLimit} requests / ${Window} minutes for a single IP.
            - Limit: [Environment, !Ref Environment, RateLimit]
              Window: [Environment, !Ref Environment, RateEvaluationWindowMin] # Minutes
      Rules:
        - Priority: 0
          Name: BlockIpRequestRate
          Statement:
            RateBasedStatement:
              AggregateKeyType: IP
              EvaluationWindowSec: !FindInMap [Environment, !Ref Environment, RateEvaluationWindowSec] # Seconds
              Limit: !FindInMap [Environment, !Ref Environment, RateLimit]
          Action:
            Block:
              CustomResponse:
                ResponseCode: 429
                CustomResponseBodyKey: IpRequestRateExceededException
                ResponseHeaders:
                  - Name: retry-after
                    Value: !FindInMap [Environment, !Ref Environment, RateEvaluationWindowSec] # Seconds

Others on the internet are using a custom function (overkill!) or a template preprocessor (CDK, etc.): https://stackoverflow.com/a/41422057/1480391

What is the expected behavior with this new feature

This could be added as intrinsic functions (added to AWS::LanguageExtensions?) and be named Fn::Add, Fn:Subtract, Fn:Multiply, Fn:Divide. Each basic function could take multiple arguments:

AWS::LanguageExtensions

# Gives 2
Value: !Add
  - 1
  - 1
AWS::LanguageExtensions

# Gives 3
Value: !Add
  - 1
  - 1
  - 1

Additional context

If rounding is too complicated to implement, we could stick to integer operations only and skip Fn:Divide. This will still cover many use cases and simplify maintainability.

Overflow could throw 🤷

Attachments

N/A

@yvele yvele changed the title Add (language extension) functions that handles basic mathematical operations Add (language extension) functions that handles basic math operations Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant