Skip to content

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

@eyalya

Description

@eyalya

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions