问题
I want to add a cloudwatch alarm to an elastic beanstalk environment using ebextensions.
So I created a .ebextensions/cloudwatch-alarms.confg file that looks like the following:
Resources:
ELBLatencyAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: { "Fn::Join" : ["", [{ "Ref" : "AWSEBEnvironmentName" }, ": HIGH ELB latency." ]]}
Namespace: AWS/ApplicationELB
MetricName: Latency
Dimensions:
- Name: LoadBalancer
Value : "arn:aws:elasticloadbalancing:xx-xxxxxx-1:123456789012:loadbalancer/app/awseb-AWSEB-oooooooooooo/12b68287xxxxxxxx"
Statistic: Average
Period: 300
EvaluationPeriods: 1
Threshold:
Fn::GetOptionSetting:
OptionName: ELBLatency
DefaultValue: "0.5"
ComparisonOperator: GreaterThanThreshold
One thing that I haven't figured out is how to use functions to get the ARN of the load balancer.
The arn of the load balancer, arn:aws:elasticloadbalancing:xx-xxxxxx-1:123456789012:loadbalancer/app/awseb-AWSEB-oooooooooooo/12b68287xxxxxxxx, is created by elastic beanstalk. I don't know it before the environment got created.
I think probably I can use the functions described here to get the arn of the load balancer. But I haven't figured how to do it.
Anyone knows how to do this?
回答1:
According to the docs:
When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the Amazon Resource Name (ARN) of the load balancer.
So if you're using a current-generation load balancer (ie ALB or NLB, not CLB), you should just be able to use the Ref function on the load balancer's logical name:
Value: !Ref MyLoadBalancer
I have also found this list of 'built-in' beanstalk resource-names very useful for getting properties associated with pieces of the architecture that are auto-generated for use in templates.
FYI the "logical name" of a resource is usually one you assign it, as a child of the Resources property in your template, e.g.
Resources:
MyLoadBalancer:
...
MyS3Bucket:
...
The logical names here are MyLoadBalancer and MyS3Bucket. If you have not defined them then use the names in the list linked above, so your load balancer will probably be either AWSEBLoadBalancer or AWSEBV2LoadBalancer, depending on whether it's a classic or current-gen load balancer.
来源:https://stackoverflow.com/questions/57723251/how-to-get-the-arn-of-the-load-balancer-in-ebextensions