1
votes

I'd like to create a number of cloudwatch alarms when specific resources are created in our AWS account (EC2 instances, ELBs, etc)

In my serverless.yml I can do the following, but this ends up creating multiple cloudwatch rules that listen for the same event.

functions:
  createLatencyAlarm:
    handler: createLatencyAlarm.main
    events:
      - cloudwatchEvent:
          event:
            source:
              - "aws.elasticloadbalancing"
            detail-type:
              - "AWS API Call via CloudTrail"
            detail:
              eventSource:
                - "elasticloadbalancing.amazonaws.com"
              eventName:
                - "CreateLoadBalancer"
  createUnhealthHostAlarm:
    handler: createUnhealthyHostAlarm.main
    events:
      - cloudwatchEvent:
          event:
            source:
              - "aws.elasticloadbalancing"
            detail-type:
              - "AWS API Call via CloudTrail"
            detail:
              eventSource:
                - "elasticloadbalancing.amazonaws.com"
              eventName:
                - "CreateLoadBalancer"

I could also set up the events to be the same sns topic, but then I have to separately (outside of serverless) create a cloudwatch rule to listen for the event and publish to the sns topic

functions:
  createLatencyAlarm:
    handler: createLatencyAlarm.main
    events:
      - sns: create-elb
  createUnhealthHostAlarm:
    handler: createUnhealthyHostAlarm.main
    events:
      - sns: create-elb

Can I set up in serverless.yml an event to share amongst functions?

1

1 Answers

1
votes

At present, Serverless creates a separate rule for each cloudwatch alarm. You can submit an issue on their repo if you'd like this changed.

In the mean time, if you'd prefer to have a single alarm, have a single lambda listen to it, then publish to an sns topic.