-1

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.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • 1
    What timeout? How long does it run? It surely provides some logs in CloudWatch about the invocation, what are those? Is it in a VPC? – luk2302 Mar 15 '23 at 13:40
  • Is the AWS Lambda function connected to a VPC? If so, is there a particular reason why you have done this? Try disconnecting it from the VPC -- this will give it Internet access. When connected to a VPC, there is no Internet access by default, unless you have configured a NAT Gateway. – John Rotenstein Mar 15 '23 at 20:49
  • @JohnRotenstein, yeah you were on point for this. I disconnected this from my VPC and it has started working as expected. Thanks. – RagingRhino Mar 17 '23 at 12:22

2 Answers2

1

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.

-1

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