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

feat: New Command Simplify to Modify Templates to Use Fn::ForEach #185

Closed
wants to merge 15 commits into from

Conversation

elishacatherasoo
Copy link

Description of changes:
This new command can modify templates to use the new Fn::ForEach function. Right now it can modify basic templates with only one property that is different in Resources/Outputs/Conditions.

How to use: rain simplify --foreach ../path/to/template.yaml

An example of a template that can be modified is shown below.

Unprocessed template:

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::LanguageExtensions'
Resources:
  DynamoDBTable1:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table1
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
  DynamoDBTable2:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table2
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
  DynamoDBTable3:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table3
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
  DynamoDBTable4:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table4
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
  DynamoDBTable5:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table5
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
  DynamoDBTable6:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table6
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
  DynamoDBTable7:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table7
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
  DynamoDBTable8:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table8
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
  DynamoDBTable9:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table9
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
  DynamoDBTable10:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: Table10
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'

Processed template:

AWSTemplateFormatVersion: "2010-09-09"

Transform: AWS::LanguageExtensions

Resources:
  Fn::ForEach::Loop0:
    - Variable0
    - - Table1
      - Table10
      - Table2
      - Table3
      - Table4
      - Table5
      - Table6
      - Table7
      - Table8
      - Table9
    - Resource${Variable0}:
        Properties:
          AttributeDefinitions:
            - AttributeName: id
              AttributeType: S
          KeySchema:
            - AttributeName: id
              KeyType: HASH
          ProvisionedThroughput:
            ReadCapacityUnits: "5"
            WriteCapacityUnits: "5"
          TableName: !Ref Variable0
        Type: AWS::DynamoDB::Table

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@elishacatherasoo elishacatherasoo marked this pull request as ready for review August 18, 2023 06:57
Copy link
Contributor

@ericzbeard ericzbeard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this would be better as an addition to the fmt command, instead of adding a new top level command. rain fmt --foreach is more specific than rain simplify. Unless we had some more ideas for ways that we could simplify templates (fmt already converts intrinsics to short form, which is a minor simplification).

Can you point me to the code that figures out what resources have been duplicated? I was finding it a little hard to follow the flow of the code from the top down, maybe you could add a comment section somewhere that describes the overall design of the feature.

I noticed a few code blocks that look like they were copied from other areas in the project, it would be good if we could refactor/reuse rather than copy, for example simplify.String.

@ericzbeard ericzbeard closed this Sep 21, 2023
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

Successfully merging this pull request may close these issues.

3 participants