Search code examples
amazon-web-servicesaws-cloudformationamazon-cloudwatchnlbaws-elb

CloudFormation !Select with !Split function with load balancer arn


I am trying to create cloudwatch alarm for Network ELB using cloudformation template. I am using Select function with the combination of split function to fetch require element.

NLB:- arn:aws:elasticloadbalancing:eu-west-1:123456:loadbalancer/net/dev-nlb-3HGD5SO64D/7GL51FD3

NLBTargetGroup:- arn:aws:elasticloadbalancing:eu-west-1:123456:targetgroup/dev-nlb-2A6W3JC4R/9DH34SJY

  Alarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      ActionsEnabled: True
      AlarmDescription: test alarm
      ComparisonOperator:  GreaterThanOrEqualToThreshold
      Dimensions:
        - Name: LoadBalancer
          Value: !Select ["5", !Split [":", !Ref NLB]]
        - Name: TargetGroup
          Value: !Select ["5", !Split [":", !Ref NLBTargetGroup]]
      EvaluationPeriods: 1
      MetricName: HealthyHostCount
      Namespace: AWS/NetworkELB
      AlarmActions:
        - arn:aws:sns:eu-west-1:123456:TestSNS
      OKActions:
        - arn:aws:sns:eu-west-1:123456:TestSNS
      Period: 60
      Statistic: Average
      Threshold: 2.0
      TreatMissingData: missing

With the above config, I am able to select value for "TargetGroup" as "targetgroup/dev-nlb-2A6W3JC4R/9DH34SJY" but for "LoadBalancer" value is getting as "loadbalancer/net/dev-nlb-3HGD5SO64D/7GL51FD3". My aim is to get only "net/dev-nlb-3HGD5SO64D/7GL51FD3"

How can I achive this?


Solution

  • You can get it using for NLB:

    !GetAtt YourNLB.LoadBalancerFullName
    

    and for target group:

    !GetAtt YourTG.TargetGroupFullName
    

    Thus you can do:

          Dimensions:
            - Name: LoadBalancer
              Value: !GetAtt YourNLB.LoadBalancerFullName
            - Name: TargetGroup
              Value: !GetAtt YourTG.TargetGroupFullName
    

    In case you really need to split the string, for NLB, your Split-Join combo would be:

     Value: !Join
          - '/'
          - - !Select [1, !Split [ '/', !Select ["5", !Split [":", !Ref NLB]]]]
            - !Select [2, !Split [ '/', !Select ["5", !Split [":", !Ref NLB]]]]
            - !Select [3, !Split [ '/', !Select ["5", !Split [":", !Ref NLB]]]]
    

    and for TargetGroup:

      Value: !Join
        - '/'
        - - !Select [1, !Split [ '/', !Select ["5", !Split [":", !Ref NLBTargetGroup]]]]
          - !Select [2, !Split [ '/', !Select ["5", !Split [":", !Ref NLBTargetGroup]]]]