Alarm action definition in ec2_metric_alarm ansibl

2019-04-06 00:09发布

问题:

I am trying to set up an cloud watch alarm witch ansible ec2_metric_alarm module and I do not know how to set it to send an email on alarm

The code is

- name: add alarm
  ec2_metric_alarm:
    state: present
    region: eu-west-1
    name: "LoadAverage"
    metric: "LoadAverage"
    statistic: Average
    comparison: ">"
    threshold: 3.0
    evaluation_periods: 3
    period: 60
    unit: "None"
    description: "Load Average"
    dimensions: {'Role':{{itme[0]}}, Node:{{item[1]}} }
    alarm_actions: ["action1","action2"]

What is the syntax or what do I do to express that I want it to send emails on in alarm_actions?

回答1:

The documentation is crappy for this one:
http://docs.ansible.com/ec2_metric_alarm_module.html

Here is what I would try based on boto:
http://docs.pythonboto.org/en/latest/ref/cloudwatch.html#module-boto.ec2.cloudwatch.alarm

alarm_actions (list of strs) – A list of the ARNs of the actions to take in ALARM state

The current supported ARNS are SNS topics or autoscalling policies.

In your case:
You need to create an SNS topic and subscribe your email address to that topic (also confirm the subscription) and after that put the SNS topic ARN as a string in the alarm_actions param that you pass to the ansible ec2_metric_alarm_module.

Hope this helps.



回答2:

I agree with @Mircea's answer regarding the documentation and its quality. I personally found the solution to the same/similar problem by creating the alarm through the UI with the desired alert action and then using the AWS CLI to extract the alarm string for use with ansible

aws cloudwatch describe-alarms

In the result you can then find the action string:

ALARMACTIONS    arn:aws:sns:us-east-1:**Cust Account ID Here**:NotifyMe
ALARMACTIONS    arn:aws:swf:us-east-1:**Cust Account ID Here**:action/actions/AWS_EC2.InstanceId.Stop/1.0

In my case I had two actions, one to email me and the other to Stop the EC2 instance

These values can then be used in your ansible task:

alarm_actions: ["arn:aws:swf:{{ aws_region }}:{{ aws_cust_account_id }}:action/actions/AWS_EC2.InstanceId.Stop/1.0", "arn:aws:sns:{{ aws_region }}:{{ aws_cust_account_id }}:NotifyMe"]