AWSTemplateFormatVersion: 2010-09-09 Parameters: CoralogixLogURL: Type: String Description: 'Please enter the Coralogix log URL endpoint:' PrivateKey: Type: String Description: |- A private key which is used to validate your authenticity Please enter your private key: AppName: Type: String Description: |- The name of your main application Please enter your app name: SubSystem: Type: String Description: |- Your application probably has multiple subsystems Please enter your sub system name: Resources: CxNotifierLambdaRole: Type: 'AWS::IAM::Role' Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com Action: - 'sts:AssumeRole' Path: / CxLambdaRolePolicies: Type: 'AWS::IAM::Policy' Properties: PolicyName: LambdaPolicy PolicyDocument: Version: 2012-10-17 Statement: - Sid: Stmt12349896368829 Action: - 'logs:CreateLogGroup' - 'logs:CreateLogStream' - 'logs:PutLogEvents' Effect: Allow Resource: 'arn:aws:logs:*:*:*' Roles: - !Ref CxNotifierLambdaRole CoralogixNotifierLambda: Type: 'AWS::Lambda::Function' Properties: Handler: index.lambda_handler Role: !GetAtt - CxNotifierLambdaRole - Arn Code: ZipFile: Fn::Sub: | # Sample Lambda Function to post notifications to a slack channel when an AWS Health event happens import os import time import logging import json import urllib.error from urllib.request import Request, urlopen CORALOGIX_LOG_URL = os.getenv('CORALOGIX_LOG_URL') PRIVATE_KEY = os.getenv('PRIVATE_KEY') APP_NAME = os.getenv('APP_NAME') SUB_SYSTEM = os.getenv('SUB_SYSTEM') WARN = 4 TIMEOUT = os.getenv('CORALOGIX_TIMEOUT_HTTP', 30) RETRIES = os.getenv('CORALOGIX_RETRIES_HTTP', 2) logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): def send(e): message = { "privateKey": str(PRIVATE_KEY), "applicationName": str(APP_NAME), "subsystemName": str(SUB_SYSTEM), "logEntries": [{"timestamp": (time.time() * 1000), "severity": WARN, "text": event}] } jsondata = json.dumps(message).encode('utf-8') for attempt in range(int(RETRIES)): try: req = Request(CORALOGIX_LOG_URL) req.add_header('Content-Type', 'application/json; charset=utf-8') req.add_header('Content-Length', len(jsondata)) response = urlopen(req, data=jsondata,timeout=TIMEOUT) if response.getcode() == 200: logger.info("Health log published to Coralogix successfully 200 OK") return True else: logger.error("health log publish failed, status code %d, %b", response.getcode(), response.read) except urllib.error.URLError as e: logger.error("URL Error %s", e) except urllib.error.HTTPError as e: logger.error("HTTP Error %s", e) logger.info("attempt number %d", attempt + 1) time.sleep(5) entities = event.get("detail", {}).get("affectedEntities") resources = event.get("resources") if(type(entities) == list and type(resources) == list and sorted(list(map(lambda x: x["entityValue"], entities))) == sorted(resources) ): event["detail"].pop("affectedEntities", None) event.pop("resources", None) for entity in entities: event["detail"]["affectedEntity"] = entity event["resource"] = entity.get("entityValue") send(event) else: send(event) Environment: Variables: CORALOGIX_LOG_URL: !Ref CoralogixLogURL PRIVATE_KEY: !Ref PrivateKey APP_NAME: !Ref AppName SUB_SYSTEM: !Ref SubSystem Tags: - Key: coralogix.com/monitor Value: 'true' Runtime: python3.8 Timeout: '60' CxLambdaInvokePermission: Type: 'AWS::Lambda::Permission' Properties: FunctionName: !GetAtt - CoralogixNotifierLambda - Arn Action: 'lambda:InvokeFunction' Principal: events.amazonaws.com SourceArn: !GetAtt - CloudWatchRuleHealth - Arn CloudWatchRuleHealth: Type: 'AWS::Events::Rule' Properties: Description: EventRule for Coralogix EventPattern: source: - aws.health State: ENABLED Targets: - Arn: !GetAtt - CoralogixNotifierLambda - Arn Id: CoralogixNotifierLambda