S
S
stayHARD2015-07-28 17:26:23
Python
stayHARD, 2015-07-28 17:26:23

How to create an AWS cloudwatch alarm?

Good afternoon.
I'm trying to create an Alarm for email notifications (when the server crashed, etc.)
Now I'm trying to run the following test code:

import boto.ec2.cloudwatch
import sys
import os

AWS_KEY = os.environ.get("XXXX")
AWS_SECRET = os.environ.get("XXXX")
AWS_REGION = os.environ.get("AWS_EC2_REGION", "us-west-2")


TOPIC = 'EXAMPLE'

def create_status_alarm(instance_id):
    ec2_conn = boto.ec2.connect_to_region(AWS_REGION,
        aws_access_key_id=AWS_KEY,
        aws_secret_access_key=AWS_SECRET)
    cloudwatch_conn = boto.ec2.cloudwatch.connect_to_region(
        AWS_REGION,
        aws_access_key_id=AWS_KEY,
        aws_secret_access_key=AWS_SECRET)

    reservations = ec2_conn.get_all_instances(filters = {'instance-id': instance_id})
    if reservations and reservations[0].instances:
        instance = reservations[0].instances[0]
        instance_name = instance.tags['Name']
    else:
        print "Invalid instance-id!"
        sys.exit(1)
    alarm = boto.ec2.cloudwatch.alarm.MetricAlarm(
        connection = cloudwatch_conn,
        name = instance_name + "-status-alarm",
        metric = 'StatusCheckFailed',
        namespace = 'AWS/EC2',
        statistic = 'Maximum',
        comparison = '>=',
        description = 'status check for %s %s' % (instance_id, instance_name),
        threshold = 1.0,
        period = 60,
        evaluation_periods = 2,
        dimensions = {'InstanceId':instance_id},
        alarm_actions = [TOPIC],
        ok_actions = [TOPIC],
        insufficient_data_actions = [TOPIC])
    cloudwatch_conn.put_metric_alarm(alarm)


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "Usage: create_status_alarm.py <instanceid>"
        sys.exit(2)
    create_status_alarm(sys.argv[1])

I get this:
Traceback (most recent call last):
  File "ins.py", line 50, in <module>
    create_status_alarm(sys.argv[1])
  File "ins.py", line 15, in create_status_alarm
    aws_secret_access_key=AWS_SECRET)
  File "/usr/local/lib/python2.7/dist-packages/boto/ec2/__init__.py", line 66, in connect_to_region
    return region.connect(**kw_params)
  File "/usr/local/lib/python2.7/dist-packages/boto/regioninfo.py", line 187, in connect
    return self.connection_cls(region=self, **kw_params)
  File "/usr/local/lib/python2.7/dist-packages/boto/ec2/connection.py", line 103, in __init__
    profile_name=profile_name)
  File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1100, in __init__
    provider=provider)
  File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 569, in __init__
    host, config, self.provider, self._required_auth_capability())
  File "/usr/local/lib/python2.7/dist-packages/boto/auth.py", line 987, in get_auth_handler
    'Check your credentials' % (len(names), str(names)))
boto.exception.NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV4Handler'] Check your credentials

I think that the script does not see the environment variables. Googling how to set them correctly, everywhere I stumble upon the same solution, which did not help, in fact, you see it in the code.
How can I make it see environment variables?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question