skip to Main Content

I am trying to disable the cloudwatch alarm actions using lambda function as provided by following article "https://medium.com/geekculture/enable-or-disable-aws-alarms-at-given-intervals-d2f867aa9aa4".

However i am facing timeouts during it’s execution. The http request for generated does not return any error.

The code is as below:

import logging
import os
import boto3

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def disable_alarms(alarm_names):
  try:
      logger.debug(f'Disabling alarms')
      disable_alarm = client.disable_alarm_actions(AlarmNames=alarm_names)
      return disable_alarm
  except Exception as e:
    logger.debug(f'Error while disabling alarms:, {e}')

def enable_alarms(alarm_names):
  try:
      logger.info(f'Enabling alarms: {alarm_names}')
      enable_alarm = client.enable_alarm_actions(AlarmNames=alarm_names)
      return enable_alarm
  except Exception as e:
    logger.debug(f'Error while enable alarms: {alarm_names}, {e}')


def lambda_handler(event, context):

    logger.info(f'## EVENT: {event}')
        
    global client
    client = boto3.client('cloudwatch')

    global list_of_alarm_arns
    list_of_alarm_names = ['HealthAlarm']
    
    logger.info(f'Sending To Function Disable')
    
    #enable_alarms(list_of_alarm_names)
    disable_alarms(list_of_alarm_names)

    return {
        'statusCode': 200,
    }  

The HealthAlarm existing actions is to be disabled.

2

Answers


  1. Chosen as BEST ANSWER

    The issue was with the lambda function being connected to the VPC as @JohnRotenstein mentioned. On disconnecting the same, the function ran without any timeouts.


  2. Try to check Lambda Execution role: it needs Cloudwatch permissions to performs actions on alarms.

    https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/permissions-reference-cw.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search