如何提取CloudFormation从弹性魔豆环境负载平衡器名称(How To Extract Lo

2019-10-29 03:48发布

我已经创造了一个CloudFormation弹性青苗和CloudWatch的警报用下面的代码片段:

        "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"
                }
        }

你可以看到,如果弹性魔豆的负载平衡器收到5XX错误的CloudWatch的警报配置警告。 但是我不能让这将是这个样子的负载均衡器名称属性:

awseb-e-a-AWSEBLoa-AY8LC6V30OAW

相反,FN :: GetAtt(“EndpointURL”)属性返回负载平衡器的DNSNAME这看起来是这样的:

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

这将无法正确地创建CloudWatch的警报,因为它期望得到负载平衡器名称不DNSNAME。

什么是让负载平衡器的名称的最好方法? 我不希望有创建负载平衡器外部资源,如“AWS :: ElasticLoadBalancing ::负载平衡器”,或尝试使用一些子法提取的DNSNAME字符串名称的字符串。

Answer 1:

如果你有机会到环境中的名字,你可以执行DescribeEnvironmentResources API调用。 响应将包括有关环境的负载均衡器的信息。



Answer 2:

OK我发现了一个解决方案,其相当的黑客,我已经能够提取使用几个分流/选择从端点LoadBalancerName /加入呼吁DNSNAME串从中返回 - FN :: GetAtt(“EndpointURL”)。 下面是代码:

"CloudWatchBackend500XXAlarm": {
"Type": "AWS::CloudWatch::Alarm",
"Properties" : {
    "AlarmDescription": "Elastic Beanstalk Has Received 5XX Backend Connection Errors",
    "AlarmActions": ["arn:aws:sns:us-east-1:1234567890:mysnstopic"],
    "Namespace": "AWS/ELB",
    "Dimensions": [
        {
            "Name": "LoadBalancerName",
            "Value": {
                "Fn::Join": ["", [{
                    "Fn::Select": [
                        "0",
                        {
                            "Fn::Split": ["AWSEBLoa-",
                                {
                                    "Fn::GetAtt": [
                                        "ElasticBeanstalkEnvironment",
                                        "EndpointURL"
                                    ]
                                }
                            ]
                        }
                    ]
                },
                "AWSEBLoa-",
                {
                    "Fn::Select": [
                        "0",
                        {
                            "Fn::Split": ["-",
                                {
                                    "Fn::Select": [
                                        "1",
                                        {
                                            "Fn::Split": ["AWSEBLoa-",
                                                {
                                                    "Fn::GetAtt": [
                                                        "ElasticBeanstalkEnvironment",
                                                        "EndpointURL"
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]]
        }
        }
    ],                  
    "MetricName": "HTTPCode_Backend_5XX",
    "Statistic": "Sum",
    "Period": "60",
    "EvaluationPeriods": "1",
    "ComparisonOperator": "GreaterThanOrEqualToThreshold",
    "Threshold": "1"
}

}

即上面的代码能够从“awseb-k-3-AWSEBLoa-11B26NY4PQB9A-739614614.us-east-1.elb.amazonaws.com”和CloudWatch的报警工程提取“awseb-K-3-AWSEBLoa-11B26NY4PQB9A”完美。 我想我将不得不调整代码内部负载平衡器,即格式化“内部 - ”在DNSNAME开始工作,但我很好,现在。

我已经提交到AWS的请求,要求他们加入CloudFormation方法返回的负载平衡器名称,而不仅仅是DNSNAME。



文章来源: How To Extract Load Balancer Name from Elastic Beanstalk Environment in CloudFormation