如何从CloudFormation中的Elastic Beanstalk环境中提取负载均衡器名称 [英] How To Extract Load Balancer Name from Elastic Beanstalk Environment in CloudFormation

本文介绍了如何从CloudFormation中的Elastic Beanstalk环境中提取负载均衡器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CloudFormation中使用以下代码段创建了Elastic Beanstalk和CloudWatch警报:

I have created a Elastic Beanstalk and CloudWatch Alarm in CloudFormation with the following code snippet:

        "ElasticBeanstalkEnvironment": {
        "Type": "AWS::ElasticBeanstalk::Environment",
        "Properties": {
            "ApplicationName": "my-app",
            "EnvironmentName": "my-eb",
            "SolutionStackName": "64bit Amazon Linux 2018.03 v3.0.1 running Tomcat 8 Java 8",
            "OptionSettings": [
                {
                    "Namespace": "aws:elb:loadbalancer",
                    "OptionName": "CrossZone",
                    "Value": "true"
                },
                {
                    "Namespace": "aws:elb:listener:80",
                    "OptionName": "ListenerProtocol",
                    "Value": "HTTP"
                },
                {
                    "Namespace": "aws:elb:listener:80",
                    "OptionName": "InstancePort",
                    "Value": "80"
                },
                etc...
            ]
        },
        "CloudWatchBacken500XXAlarm": {
            "Type": "AWS::CloudWatch::Alarm",
            "Properties" : {
                "AlarmActions": ["arn:aws:sns:us-east-1:12345678:mysnstopic"],
                "Namespace": "AWS/ELB",
                "Dimensions": [{
                    "Name": "LoadBalancerName",
                    "Value" : {
                        "Fn::GetAtt": [
                            "ElasticBeanstalkEnvironment",
                            "EndpointURL"  
                        ]
                    }
                  }],
                "MetricName": "HTTPCode_Backend_5XX",
                "Statistic": "Sum",
                "Period": "60",
                "EvaluationPeriods": "1",
                "ComparisonOperator": "GreaterThanOrEqualToThreshold",
                "Threshold": "1"
                }
        }

您可以看到,CloudWatch警报已配置为在Elastic Beanstalk的负载均衡器收到5XX错误时发出警报。但是,我无法获得如下所示的负载均衡器Name属性:

You can see that the CloudWatch Alarm is configured to alert if the Elastic Beanstalk's load balancer receives 5XX Errors. However I am not able to get the load balancer Name attribute which would look something like this:

awseb-e-a-AWSEBLoa-AY8LC6V30OAW

相反,Fn :: GetAtt( EndpointURL)属性返回负载均衡器的DNSName,看起来像

Instead the Fn::GetAtt("EndpointURL") attribute returns the load balancer's DNSName which looks something like this:

awseb-e-a-AWSEBLoa-AY8LC6V30OAW-175133046.us-east-1.elb.amazonaws.com

这将无法正确创建CloudWatch警报,因为它希望获得负载均衡器名称而不是DNSName。

Which will fail to create the CloudWatch alarm correctly as it expects to get the load balancer Name not DNSName.

获得负载均衡器名称的最佳方法是什么?我不想将负载均衡器创建为诸如 AWS :: ElasticLoadBalancing :: LoadBalancer之类的外部资源,也不必尝试使用某些子字符串方法从DNSName字符串中提取Name字符串。

What's the best way to get the Load Balancer's Name? I don't want to have to create the Load Balancer as an external resource like "AWS::ElasticLoadBalancing::LoadBalancer" or try to use some substring method to extract the Name string from the DNSName string.

推荐答案

使用ebextensions创建警报会容易得多-因为获取ELB名称与Value一样容易:{ Ref :: AWSEBLoadBalancer}

Using ebextensions would be much easier to create an alarm - as fetching the ELB name is as easy as Value: { "Ref" : "AWSEBLoadBalancer" }

创建一个扩展名为.config的文件(例如-BackendErrors.config),并将其放置在名为 .ebextensions的文件夹中。完整的扩展名如下:

Create a file with .config extension (say - BackendErrors.config) and place it in a folder with name ".ebextensions". Complete ebextension will be as follows:

Resources:
  CloudWatchBacken500XXAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: "Elastic Beanstalk Has Received 5XX Backend Connection Errors"
      AlarmName: { "Fn::Join" : ["", [{ "Ref" : "AWSEBEnvironmentName" }, "-Backend-5XX-Alarm." ]]}
      AlarmActions:
        - "arn:aws:sns:us-east-1:123456789012:mysnstopic"
      Namespace: AWS/ELB
      Dimensions:
        - Name: LoadBalancerName
          Value: { "Ref" : "AWSEBLoadBalancer" }
      MetricName: HTTPCode_Backend_5XX
      Statistic: Sum
      Period: 60
      EvaluationPeriods: 1
      Threshold: 1
      ComparisonOperator: GreaterThanOrEqualToThreshold

.ebextensions文件夹应在顶部创建应用程序源包的级别:

The .ebextensions folder should be created at the top level of your application source bundle:

~/workspace/my-application/
|-- .ebextensions
|   |-- BackendErrors.config

这篇关于如何从CloudFormation中的Elastic Beanstalk环境中提取负载均衡器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆