How to properly do regex expression in Fn::Sub





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















In a cloudformation template JSON file, I am trying to specify a metricfilter for multiple LogGroupNames using RegEx like the samples below.



I did look at the java.util.regex.Pattern documentation which AWS says is what it bases its regex syntax on but that did not help.



"LogGroupName": {
"Fn::Sub": "/aws/lambda/${Vpc}-lambdaname\*"


or



"Fn::Sub": "/aws/lambda/${Vpc}-lambdaname\[A-Z0-9]+"


But I continue to get this error message: ...'failed to satisfy constraint: Member must satisfy regular expression pattern: [.-_/#A-Za-z0-9]+ (Service: AWSLogs; Status Code: 400; Error Code: InvalidParameterException...'



What is the proper regex syntax that I need or is there another way to specify the multiple log groups? Thanks.










share|improve this question





























    0















    In a cloudformation template JSON file, I am trying to specify a metricfilter for multiple LogGroupNames using RegEx like the samples below.



    I did look at the java.util.regex.Pattern documentation which AWS says is what it bases its regex syntax on but that did not help.



    "LogGroupName": {
    "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname\*"


    or



    "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname\[A-Z0-9]+"


    But I continue to get this error message: ...'failed to satisfy constraint: Member must satisfy regular expression pattern: [.-_/#A-Za-z0-9]+ (Service: AWSLogs; Status Code: 400; Error Code: InvalidParameterException...'



    What is the proper regex syntax that I need or is there another way to specify the multiple log groups? Thanks.










    share|improve this question

























      0












      0








      0








      In a cloudformation template JSON file, I am trying to specify a metricfilter for multiple LogGroupNames using RegEx like the samples below.



      I did look at the java.util.regex.Pattern documentation which AWS says is what it bases its regex syntax on but that did not help.



      "LogGroupName": {
      "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname\*"


      or



      "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname\[A-Z0-9]+"


      But I continue to get this error message: ...'failed to satisfy constraint: Member must satisfy regular expression pattern: [.-_/#A-Za-z0-9]+ (Service: AWSLogs; Status Code: 400; Error Code: InvalidParameterException...'



      What is the proper regex syntax that I need or is there another way to specify the multiple log groups? Thanks.










      share|improve this question














      In a cloudformation template JSON file, I am trying to specify a metricfilter for multiple LogGroupNames using RegEx like the samples below.



      I did look at the java.util.regex.Pattern documentation which AWS says is what it bases its regex syntax on but that did not help.



      "LogGroupName": {
      "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname\*"


      or



      "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname\[A-Z0-9]+"


      But I continue to get this error message: ...'failed to satisfy constraint: Member must satisfy regular expression pattern: [.-_/#A-Za-z0-9]+ (Service: AWSLogs; Status Code: 400; Error Code: InvalidParameterException...'



      What is the proper regex syntax that I need or is there another way to specify the multiple log groups? Thanks.







      amazon-web-services amazon-cloudformation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 20:19









      ray500ray500

      537




      537
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Fn::Sub doesn't do regular expressions and what you're after is also not a regular expression, it's a wildcard, but then looking at AWS::Logs::MetricFilter documentation I don't think it supports wildcard expressions. You need to specify exact log group reference so you'll have to create a separate MetricFilter for each log group name (if there indeed are many). Maybe what you mean is all the log streams in the log group? In that case you are fine without the /* suffix because the filter applies to all streams in the group. For a lambda function the log group is in the form /aws/lambda/<FunctionName> so in your case "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname" and you're done.



          The exact error you were having was saying that (back-slash) is not a valid part of a log group name. When you say \* it becomes *. You probably meant /*, but as explained above you don't need it.



          Fn::Sub:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html



          AWS::Logs::MetricFilter:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-metricfilter.html






          share|improve this answer
























          • Thanks jbasko. I'm trying to setup metric for every log group that starts with specific string, not logstream. We have several deployments and new log groups get created so I was hoping specifying just a regex would be an easy solution.

            – ray500
            Jan 4 at 19:34











          • Yes, then you need a separate MetricFilter for each. What I'd do is define it next to where I define each LogGroup (which for me personally is next to every Lambda::Function; it's not necessary, but I do it because then I can control log expiration; unfortunately you can't do it after log groups have already been created outside of CloudFormation's watchful eye) and if there is some pattern that you don't want to be updating in all the places, I'd use a Parameter for that with value looked up from SSM Parameter Store.

            – jbasko
            Jan 4 at 20:29











          • I'm trying to avoid creating a separate MetricFilter because new log groups keep getting created after deployments and the log group name is generated with a dynamic id(I don't know how to capture in my cloudformation template) at the end of the name. Example: '/aws/lambda/<lamba name>-FVU22IVJVGP

            – ray500
            Jan 7 at 14:12













          • If you specify FunctionName for your lambdas then they won't have random names. If you create the LogGroup yourself then you're in control of it. And you can reference the generated lambda's name with !Ref. Here's my typical log group: LogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub /aws/lambda/${Function} RetentionInDays: 30

            – jbasko
            Jan 7 at 19:04











          • Thanks jbasko. I went ahead and metricfilter for each.

            – ray500
            Jan 14 at 20:53












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029230%2fhow-to-properly-do-regex-expression-in-fnsub%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Fn::Sub doesn't do regular expressions and what you're after is also not a regular expression, it's a wildcard, but then looking at AWS::Logs::MetricFilter documentation I don't think it supports wildcard expressions. You need to specify exact log group reference so you'll have to create a separate MetricFilter for each log group name (if there indeed are many). Maybe what you mean is all the log streams in the log group? In that case you are fine without the /* suffix because the filter applies to all streams in the group. For a lambda function the log group is in the form /aws/lambda/<FunctionName> so in your case "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname" and you're done.



          The exact error you were having was saying that (back-slash) is not a valid part of a log group name. When you say \* it becomes *. You probably meant /*, but as explained above you don't need it.



          Fn::Sub:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html



          AWS::Logs::MetricFilter:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-metricfilter.html






          share|improve this answer
























          • Thanks jbasko. I'm trying to setup metric for every log group that starts with specific string, not logstream. We have several deployments and new log groups get created so I was hoping specifying just a regex would be an easy solution.

            – ray500
            Jan 4 at 19:34











          • Yes, then you need a separate MetricFilter for each. What I'd do is define it next to where I define each LogGroup (which for me personally is next to every Lambda::Function; it's not necessary, but I do it because then I can control log expiration; unfortunately you can't do it after log groups have already been created outside of CloudFormation's watchful eye) and if there is some pattern that you don't want to be updating in all the places, I'd use a Parameter for that with value looked up from SSM Parameter Store.

            – jbasko
            Jan 4 at 20:29











          • I'm trying to avoid creating a separate MetricFilter because new log groups keep getting created after deployments and the log group name is generated with a dynamic id(I don't know how to capture in my cloudformation template) at the end of the name. Example: '/aws/lambda/<lamba name>-FVU22IVJVGP

            – ray500
            Jan 7 at 14:12













          • If you specify FunctionName for your lambdas then they won't have random names. If you create the LogGroup yourself then you're in control of it. And you can reference the generated lambda's name with !Ref. Here's my typical log group: LogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub /aws/lambda/${Function} RetentionInDays: 30

            – jbasko
            Jan 7 at 19:04











          • Thanks jbasko. I went ahead and metricfilter for each.

            – ray500
            Jan 14 at 20:53
















          1














          Fn::Sub doesn't do regular expressions and what you're after is also not a regular expression, it's a wildcard, but then looking at AWS::Logs::MetricFilter documentation I don't think it supports wildcard expressions. You need to specify exact log group reference so you'll have to create a separate MetricFilter for each log group name (if there indeed are many). Maybe what you mean is all the log streams in the log group? In that case you are fine without the /* suffix because the filter applies to all streams in the group. For a lambda function the log group is in the form /aws/lambda/<FunctionName> so in your case "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname" and you're done.



          The exact error you were having was saying that (back-slash) is not a valid part of a log group name. When you say \* it becomes *. You probably meant /*, but as explained above you don't need it.



          Fn::Sub:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html



          AWS::Logs::MetricFilter:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-metricfilter.html






          share|improve this answer
























          • Thanks jbasko. I'm trying to setup metric for every log group that starts with specific string, not logstream. We have several deployments and new log groups get created so I was hoping specifying just a regex would be an easy solution.

            – ray500
            Jan 4 at 19:34











          • Yes, then you need a separate MetricFilter for each. What I'd do is define it next to where I define each LogGroup (which for me personally is next to every Lambda::Function; it's not necessary, but I do it because then I can control log expiration; unfortunately you can't do it after log groups have already been created outside of CloudFormation's watchful eye) and if there is some pattern that you don't want to be updating in all the places, I'd use a Parameter for that with value looked up from SSM Parameter Store.

            – jbasko
            Jan 4 at 20:29











          • I'm trying to avoid creating a separate MetricFilter because new log groups keep getting created after deployments and the log group name is generated with a dynamic id(I don't know how to capture in my cloudformation template) at the end of the name. Example: '/aws/lambda/<lamba name>-FVU22IVJVGP

            – ray500
            Jan 7 at 14:12













          • If you specify FunctionName for your lambdas then they won't have random names. If you create the LogGroup yourself then you're in control of it. And you can reference the generated lambda's name with !Ref. Here's my typical log group: LogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub /aws/lambda/${Function} RetentionInDays: 30

            – jbasko
            Jan 7 at 19:04











          • Thanks jbasko. I went ahead and metricfilter for each.

            – ray500
            Jan 14 at 20:53














          1












          1








          1







          Fn::Sub doesn't do regular expressions and what you're after is also not a regular expression, it's a wildcard, but then looking at AWS::Logs::MetricFilter documentation I don't think it supports wildcard expressions. You need to specify exact log group reference so you'll have to create a separate MetricFilter for each log group name (if there indeed are many). Maybe what you mean is all the log streams in the log group? In that case you are fine without the /* suffix because the filter applies to all streams in the group. For a lambda function the log group is in the form /aws/lambda/<FunctionName> so in your case "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname" and you're done.



          The exact error you were having was saying that (back-slash) is not a valid part of a log group name. When you say \* it becomes *. You probably meant /*, but as explained above you don't need it.



          Fn::Sub:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html



          AWS::Logs::MetricFilter:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-metricfilter.html






          share|improve this answer













          Fn::Sub doesn't do regular expressions and what you're after is also not a regular expression, it's a wildcard, but then looking at AWS::Logs::MetricFilter documentation I don't think it supports wildcard expressions. You need to specify exact log group reference so you'll have to create a separate MetricFilter for each log group name (if there indeed are many). Maybe what you mean is all the log streams in the log group? In that case you are fine without the /* suffix because the filter applies to all streams in the group. For a lambda function the log group is in the form /aws/lambda/<FunctionName> so in your case "Fn::Sub": "/aws/lambda/${Vpc}-lambdaname" and you're done.



          The exact error you were having was saying that (back-slash) is not a valid part of a log group name. When you say \* it becomes *. You probably meant /*, but as explained above you don't need it.



          Fn::Sub:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html



          AWS::Logs::MetricFilter:
          https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-metricfilter.html







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 15:54









          jbaskojbasko

          4,81012639




          4,81012639













          • Thanks jbasko. I'm trying to setup metric for every log group that starts with specific string, not logstream. We have several deployments and new log groups get created so I was hoping specifying just a regex would be an easy solution.

            – ray500
            Jan 4 at 19:34











          • Yes, then you need a separate MetricFilter for each. What I'd do is define it next to where I define each LogGroup (which for me personally is next to every Lambda::Function; it's not necessary, but I do it because then I can control log expiration; unfortunately you can't do it after log groups have already been created outside of CloudFormation's watchful eye) and if there is some pattern that you don't want to be updating in all the places, I'd use a Parameter for that with value looked up from SSM Parameter Store.

            – jbasko
            Jan 4 at 20:29











          • I'm trying to avoid creating a separate MetricFilter because new log groups keep getting created after deployments and the log group name is generated with a dynamic id(I don't know how to capture in my cloudformation template) at the end of the name. Example: '/aws/lambda/<lamba name>-FVU22IVJVGP

            – ray500
            Jan 7 at 14:12













          • If you specify FunctionName for your lambdas then they won't have random names. If you create the LogGroup yourself then you're in control of it. And you can reference the generated lambda's name with !Ref. Here's my typical log group: LogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub /aws/lambda/${Function} RetentionInDays: 30

            – jbasko
            Jan 7 at 19:04











          • Thanks jbasko. I went ahead and metricfilter for each.

            – ray500
            Jan 14 at 20:53



















          • Thanks jbasko. I'm trying to setup metric for every log group that starts with specific string, not logstream. We have several deployments and new log groups get created so I was hoping specifying just a regex would be an easy solution.

            – ray500
            Jan 4 at 19:34











          • Yes, then you need a separate MetricFilter for each. What I'd do is define it next to where I define each LogGroup (which for me personally is next to every Lambda::Function; it's not necessary, but I do it because then I can control log expiration; unfortunately you can't do it after log groups have already been created outside of CloudFormation's watchful eye) and if there is some pattern that you don't want to be updating in all the places, I'd use a Parameter for that with value looked up from SSM Parameter Store.

            – jbasko
            Jan 4 at 20:29











          • I'm trying to avoid creating a separate MetricFilter because new log groups keep getting created after deployments and the log group name is generated with a dynamic id(I don't know how to capture in my cloudformation template) at the end of the name. Example: '/aws/lambda/<lamba name>-FVU22IVJVGP

            – ray500
            Jan 7 at 14:12













          • If you specify FunctionName for your lambdas then they won't have random names. If you create the LogGroup yourself then you're in control of it. And you can reference the generated lambda's name with !Ref. Here's my typical log group: LogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub /aws/lambda/${Function} RetentionInDays: 30

            – jbasko
            Jan 7 at 19:04











          • Thanks jbasko. I went ahead and metricfilter for each.

            – ray500
            Jan 14 at 20:53

















          Thanks jbasko. I'm trying to setup metric for every log group that starts with specific string, not logstream. We have several deployments and new log groups get created so I was hoping specifying just a regex would be an easy solution.

          – ray500
          Jan 4 at 19:34





          Thanks jbasko. I'm trying to setup metric for every log group that starts with specific string, not logstream. We have several deployments and new log groups get created so I was hoping specifying just a regex would be an easy solution.

          – ray500
          Jan 4 at 19:34













          Yes, then you need a separate MetricFilter for each. What I'd do is define it next to where I define each LogGroup (which for me personally is next to every Lambda::Function; it's not necessary, but I do it because then I can control log expiration; unfortunately you can't do it after log groups have already been created outside of CloudFormation's watchful eye) and if there is some pattern that you don't want to be updating in all the places, I'd use a Parameter for that with value looked up from SSM Parameter Store.

          – jbasko
          Jan 4 at 20:29





          Yes, then you need a separate MetricFilter for each. What I'd do is define it next to where I define each LogGroup (which for me personally is next to every Lambda::Function; it's not necessary, but I do it because then I can control log expiration; unfortunately you can't do it after log groups have already been created outside of CloudFormation's watchful eye) and if there is some pattern that you don't want to be updating in all the places, I'd use a Parameter for that with value looked up from SSM Parameter Store.

          – jbasko
          Jan 4 at 20:29













          I'm trying to avoid creating a separate MetricFilter because new log groups keep getting created after deployments and the log group name is generated with a dynamic id(I don't know how to capture in my cloudformation template) at the end of the name. Example: '/aws/lambda/<lamba name>-FVU22IVJVGP

          – ray500
          Jan 7 at 14:12







          I'm trying to avoid creating a separate MetricFilter because new log groups keep getting created after deployments and the log group name is generated with a dynamic id(I don't know how to capture in my cloudformation template) at the end of the name. Example: '/aws/lambda/<lamba name>-FVU22IVJVGP

          – ray500
          Jan 7 at 14:12















          If you specify FunctionName for your lambdas then they won't have random names. If you create the LogGroup yourself then you're in control of it. And you can reference the generated lambda's name with !Ref. Here's my typical log group: LogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub /aws/lambda/${Function} RetentionInDays: 30

          – jbasko
          Jan 7 at 19:04





          If you specify FunctionName for your lambdas then they won't have random names. If you create the LogGroup yourself then you're in control of it. And you can reference the generated lambda's name with !Ref. Here's my typical log group: LogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub /aws/lambda/${Function} RetentionInDays: 30

          – jbasko
          Jan 7 at 19:04













          Thanks jbasko. I went ahead and metricfilter for each.

          – ray500
          Jan 14 at 20:53





          Thanks jbasko. I went ahead and metricfilter for each.

          – ray500
          Jan 14 at 20:53




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029230%2fhow-to-properly-do-regex-expression-in-fnsub%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Can't read property showImagePicker of undefined in react native iOS

          Pushsharp Apns notification error: 'InvalidToken'