Search code examples
amazon-ec2ansiblemetricsalarm

Getting "AlarmName must not contain leading or trailing whitespace" when using ec2_metric_alarm


I am trying to use ansible module ec2_metric_alarm, to create Instance state alarms for my EC2 Instances, but i do keep getting error message "AlarmName must not contain leading or trailing whitespace", not sure what am i doing wrong here i am not using any spaces in my Alarm Name, but still i keep getting the same error. Any suggestion will be appreciated.

Here is my code:

- name: Create Instance Check Alarm
  ec2_metric_alarm:
    state: present
    security_token: "{{security_token}}"
    region: "{{region}}"
    name: "{{cluster_name}}_StatusCheckFailed_Instance_{{new_instance_id}}"
    metric: "StatusCheckFailed_Instance"
    namespace: "AWS/EC2"
    statistic: "Maximum"
    evaluation_periods: 2
    comparison: ">="
    threshold: 1.0
    period: 60
    unit: "Count"
    description: "This will alarm will Reboot Instance when there is a Instance Check fails"
    dimensions: {'InstanceId': "{{new_instance_id}}"}
    alarm_actions: ["arn:aws:sns:{{region}}:SNS_TOPIC:NotifyMe","arn:aws:automate:{{region}}:ec2:reboot"]
  delegate_to: localhost

- name: Create System Fail Alarm
  ec2_metric_alarm:
    state: present
    security_token: "{{security_token}}"
    region: "{{region}}"
    name: "{{cluster_name}}_StatusCheckFailed_Instance_{{new_instance_id}}"
    metric: "StatusCheckFailed_System"
    namespace: "AWS/EC2"
    statistic: "Maximum"
    evaluation_periods: 2
    comparison: ">="
    threshold: 1.0
    period: 60
    unit: "Count"
    description: "This will alarm will Auto Recover Instance when there is a System Failure Identified"
    dimensions: {'InstanceId': "{{new_instance_id}}"}
    alarm_actions: ["arn:aws:sns:{{region}}:SNS_TOPIC:NotifyMe","arn:aws:automate:{{region}}:ec2:recover"]
  delegate_to: localhost



 The full traceback is:
Traceback (most recent call last):
  File "/uhome/user/.ansible/tmp/ansible-tmp-1573183650.92-219299995957066/AnsiballZ_ec2_metric_alarm.py", line 113, in <module>
    _ansiballz_main()
  File "/uhome/user/.ansible/tmp/ansible-tmp-1573183650.92-219299995957066/AnsiballZ_ec2_metric_alarm.py", line 105, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/uhome/user/.ansible/tmp/ansible-tmp-1573183650.92-219299995957066/AnsiballZ_ec2_metric_alarm.py", line 48, in invoke_module
    imp.load_module('__main__', mod, module, MOD_DESC)
  File "/tmp/ansible_ec2_metric_alarm_payload_F0yFcU/__main__.py", line 320, in <module>
  File "/tmp/ansible_ec2_metric_alarm_payload_F0yFcU/__main__.py", line 314, in main
  File "/tmp/ansible_ec2_metric_alarm_payload_F0yFcU/__main__.py", line 171, in create_metric_alarm
  File "/uhome/user/lib/python2.7/site-packages/boto/ec2/cloudwatch/__init__.py", line 382, in describe_alarms
    [('MetricAlarms', MetricAlarms)])
  File "/uhome/user/lib/python2.7/site-packages/boto/connection.py", line 1186, in get_list
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.BotoServerError: BotoServerError: 400 Bad Request
<ErrorResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
  <Error>
    <Type>Sender</Type>
    <Code>ValidationError</Code>
    <Message>AlarmName must not contain leading or trailing whitespace</Message>
  </Error>
  <RequestId>ab8f10ca-d9cd-4964-945e-34727c77a09a</RequestId>
</ErrorResponse>

Solution

  • i am not using any spaces in my Alarm Name, but still i keep getting the same error

    I would bet, based only on your provided code snippet, that cluster_name has a leading space. I can demonstrate with

    - hosts: all
      vars:
        cluster_name: " x"
      tasks:
      - set_fact:
          alarm_name: "{{cluster_name}}y"
      - debug:
          msg: alarm name has {{alarm_name|length}} characters
    

    which, of course, emits alarm name has 3 characters because of the leading space.

    You can solve that problem one of two ways: actually track down where the space is being introduced into cluster_name, or side-step that problem and filter cluster_name with | trim for that one task