我在CloudFormation中使用以下代码段创建了Elastic Beanstalk和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"
}
}
您可以看到CloudWatch警报已配置为警告Elastic Beanstalk的负载均衡器是否收到5XX错误。但是我无法获得看起来像这样的负载均衡器Name属性:
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 :: LoadBalancer”之类的外部资源,也不必尝试使用某些子字符串方法从DNSName字符串中提取Name字符串。
答案 0 :(得分:1)
使用扩展名更容易创建警报-因为获取ELB名称与值一样容易:{“ Ref”:“ AWSEBLoadBalancer”}
创建一个扩展名为.config的文件(例如-BackendErrors.config),并将其放置在名为“ .ebextensions”的文件夹中。完全的伸伸肌如下:
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文件夹应在应用程序源包的顶级创建:
~/workspace/my-application/
|-- .ebextensions
| |-- BackendErrors.config
答案 1 :(得分:0)
如果您有权访问环境名称,则可以执行DescribeEnvironmentResources
API调用。该响应将包括有关您的环境的负载平衡器的信息。
答案 2 :(得分:0)
好吧,我找到了一个解决方案,它相当不错,我已经能够对-Fn :: GetAtt(“ EndpointURL”)返回的DNSName字符串使用多个Split / Select / Join调用,从端点提取LoadBalancerName。这是代码:
"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”中提取“ awseb-k-3-AWSEBLoa-11B26NY4PQB9A” CloudWatch警报可完美运行。我认为我必须调整代码以与内部负载均衡器一起使用,即在DNSName的开头使用“ internal-”格式化,但我现在还可以。
我已向AWS提交了一个请求,要求他们添加CloudFormation方法以返回LoadBalancer名称,而不仅仅是DNSName。
答案 3 :(得分:0)
几年后来到这里,看起来亚马逊现在已经添加了该属性:
引用:
LoadBalancerFullName
负载均衡器的全名。例如,app / my-load-balancer / 50dc6c495c0c9188。
所以您现在可以做:
Dimensions:
- Name: LoadBalancer
Value: !GetAtt YourALBRef.LoadBalancerFullName