CloudFormation !Select with !Split function with load balancer arn
CloudFormation !Select with !Split function with load balancer arn
我正在尝试使用 cloudformation 模板为网络 ELB 创建 cloudwatch 警报。我正在使用 Select 函数与拆分函数的组合来获取需要的元素。
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
使用上述配置,我可以将“TargetGroup”的 select 值设置为“targetgroup/dev-nlb-2A6W3JC4R/9DH34SJY”,但“LoadBalancer”的值将设置为“loadbalancer/net/dev-nlb-3HGD5SO64D/7GL51FD3”。
我的目标是只获得“net/dev-nlb-3HGD5SO64D/7GL51FD3”
我怎样才能做到这一点?
如果这些资源与您的警报在同一个 CloudFormation 堆栈中,您实际上可以使用以下功能来获取这两种资源的名称:
!GetAtt: NLB.LoadBalancerFullName
!GetAtt: NLBTargetGroup.TargetGroupFullName
对于您的堆栈,这将变为
Alarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: True
AlarmDescription: test alarm
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: LoadBalancer
Value: NLB.LoadBalancerFullName
- Name: TargetGroup
Value: NLBTargetGroup.TargetGroupFullName
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
如果这是嵌套堆栈的一部分,您当然也可以将这些值用作 output。
否则,如果它们是单独堆栈的一部分(而不是嵌套堆栈),而您只是将这些值复制到(或从导出中引用),您将需要执行多次拆分以检索您要获取的信息正在寻找。
你可以使用 for NLB:
!GetAtt YourNLB.LoadBalancerFullName
和 target group:
!GetAtt YourTG.TargetGroupFullName
因此你可以这样做:
Dimensions:
- Name: LoadBalancer
Value: !GetAtt YourNLB.LoadBalancerFullName
- Name: TargetGroup
Value: !GetAtt YourTG.TargetGroupFullName
如果您确实需要拆分字符串,对于 NLB,您的 Split-Join 组合将是:
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]]]]
对于目标组:
Value: !Join
- '/'
- - !Select [1, !Split [ '/', !Select ["5", !Split [":", !Ref NLBTargetGroup]]]]
- !Select [2, !Split [ '/', !Select ["5", !Split [":", !Ref NLBTargetGroup]]]]
我正在尝试使用 cloudformation 模板为网络 ELB 创建 cloudwatch 警报。我正在使用 Select 函数与拆分函数的组合来获取需要的元素。
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
使用上述配置,我可以将“TargetGroup”的 select 值设置为“targetgroup/dev-nlb-2A6W3JC4R/9DH34SJY”,但“LoadBalancer”的值将设置为“loadbalancer/net/dev-nlb-3HGD5SO64D/7GL51FD3”。 我的目标是只获得“net/dev-nlb-3HGD5SO64D/7GL51FD3”
我怎样才能做到这一点?
如果这些资源与您的警报在同一个 CloudFormation 堆栈中,您实际上可以使用以下功能来获取这两种资源的名称:
!GetAtt: NLB.LoadBalancerFullName
!GetAtt: NLBTargetGroup.TargetGroupFullName
对于您的堆栈,这将变为
Alarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: True
AlarmDescription: test alarm
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: LoadBalancer
Value: NLB.LoadBalancerFullName
- Name: TargetGroup
Value: NLBTargetGroup.TargetGroupFullName
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
如果这是嵌套堆栈的一部分,您当然也可以将这些值用作 output。
否则,如果它们是单独堆栈的一部分(而不是嵌套堆栈),而您只是将这些值复制到(或从导出中引用),您将需要执行多次拆分以检索您要获取的信息正在寻找。
你可以使用 for NLB:
!GetAtt YourNLB.LoadBalancerFullName
和 target group:
!GetAtt YourTG.TargetGroupFullName
因此你可以这样做:
Dimensions:
- Name: LoadBalancer
Value: !GetAtt YourNLB.LoadBalancerFullName
- Name: TargetGroup
Value: !GetAtt YourTG.TargetGroupFullName
如果您确实需要拆分字符串,对于 NLB,您的 Split-Join 组合将是:
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]]]]
对于目标组:
Value: !Join
- '/'
- - !Select [1, !Split [ '/', !Select ["5", !Split [":", !Ref NLBTargetGroup]]]]
- !Select [2, !Split [ '/', !Select ["5", !Split [":", !Ref NLBTargetGroup]]]]