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

Support for Removing Hyphens in Resource Names When Using ForEach in Serverless YAML #155

Open
eyalya opened this issue Oct 8, 2024 · 0 comments

Comments

@eyalya
Copy link

eyalya commented Oct 8, 2024

Issue Description:

Hi,

I've been using the foreach loop in my Serverless Framework YAML to iterate over resources and generate multiple CloudFormation resources dynamically. However, I encountered an issue with the generated resource names, where hyphens (-) appear in the resource names, which is not allowed for some AWS resource types.

I need to ensure that the generated resource names are unique but also need to strip out or replace the hyphens. This is currently problematic as I rely on the foreach loop to generate these names dynamically.

Here’s my YAML code:

functions:
  test-1:
    handler: test1.handler
  test-2:
    handler: test2.handler

resources:
  Resources:
    $forEach:
      iterator: ${self:functions}
      template:
        DatadogForwarderLambdaLogFilter$forEach.key:
          Type: AWS::Logs::SubscriptionFilter
          DependsOn : DatadogForwarderLambdaPermission$forEach.key
          Properties:
            DestinationArn: ${file(../../common-config/config.yml):${self:provider.stage}.test}
            FilterPattern: '?ERROR ?WARN ?INFO ?DEBUG'
            FilterName: DatadogForwarder
            LogGroupName: /aws/lambda/${self:service}-${self:provider.stage}-$forEach.key

        DatadogForwarderLambdaPermission$forEach.key:
          Type: AWS::Lambda::Permission
          Properties:
            FunctionName: ${file(../../common-config/config.yml):${self:provider.stage}.test}
            Action: lambda:InvokeFunction
            Principal: logs.${self:provider.region}.amazonaws.com
            SourceArn: !Sub arn:aws:logs:${self:provider.region}:${AWS::AccountId}:log-group:/aws/lambda/${self:service}-${self:provider.stage}-$forEach.key:*
            SourceAccount: !Sub ${AWS::AccountId}

JSON generated:

"DatadogForwarderLambdaLogFiltertest-1": {
  "Type": "AWS::Logs::SubscriptionFilter",
  "DependsOn": "DatadogForwarderLambdaPermissiontest-1",
  "Properties": {
    "DestinationArn": "arn:aws:lambda:us-east-2:444611906471:function:datadog-forwarder-Forwarder-1B5TNWGJ98EDJ",
    "FilterPattern": "?ERROR ?WARN ?INFO ?DEBUG",
    "FilterName": "DatadogForwarder",
    "LogGroupName": "/aws/lambda/ecobox-provision-lambda-staging-test-1"
  }
},
"DatadogForwarderLambdaPermissiontest-1": {
  "Type": "AWS::Lambda::Permission",
  "Properties": {
    "FunctionName": "arn:aws:lambda:us-east-2:444611906471:function:datadog-forwarder-Forwarder-1B5TNWGJ98EDJ",
    "Action": "lambda:InvokeFunction",
    "Principal": "logs.us-east-2.amazonaws.com",
    "SourceArn": {
      "Fn::Sub": "arn:aws:logs:us-east-2:${AWS::AccountId}:log-group:/aws/lambda/ecobox-provision-lambda-staging-test-1:*"
    },
    "SourceAccount": {
      "Fn::Sub": "${AWS::AccountId}"
    }
  }
},
"DatadogForwarderLambdaLogFiltertest-2": {
  "Type": "AWS::Logs::SubscriptionFilter",
  "DependsOn": "DatadogForwarderLambdaPermissiontest-2",
  "Properties": {
    "DestinationArn": "arn:aws:lambda:us-east-2:444611906471:function:datadog-forwarder-Forwarder-1B5TNWGJ98EDJ",
    "FilterPattern": "?ERROR ?WARN ?INFO ?DEBUG",
    "FilterName": "DatadogForwarder",
    "LogGroupName": "/aws/lambda/ecobox-provision-lambda-staging-test-2"
  }
},
"DatadogForwarderLambdaPermissiontest-2": {
  "Type": "AWS::Lambda::Permission",
  "Properties": {
    "FunctionName": "arn:aws:lambda:us-east-2:444611906471:function:datadog-forwarder-Forwarder-1B5TNWGJ98EDJ",
    "Action": "lambda:InvokeFunction",
    "Principal": "logs.us-east-2.amazonaws.com",
    "SourceArn": {
      "Fn::Sub": "arn:aws:logs:us-east-2:${AWS::AccountId}:log-group:/aws/lambda/ecobox-provision-lambda-staging-test-2:*"
    },
    "SourceAccount": {
      "Fn::Sub": "${AWS::AccountId}"
    }
  }
}

Issue:

As you can see, the resource names such as DatadogForwarderLambdaLogFiltertest-1 and DatadogForwarderLambdaLogFiltertest-2 are generated with hyphens, which causes issues with some AWS resources that don’t allow hyphens in their names. It would be really helpful to have the ability to replace hyphens or modify the resource name structure to ensure they are valid and unique. Event use of foreach index could solve it I think.

Could support for this feature be added to handle resource names in this way? Thank you!

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