signalfx / cloudwatch-event-forwarder

Sample code for the lambda which can be used to forward Cloudwatch events to SignalFx
0 stars 2 forks source link

SignalFx CloudWatch Event Forwarder

Overview

These instructions will describe the steps to deploy the Lambda function which forwards CloudWatch events to SignalFx as Custom Events. The deployed function uses SignalFx Lambda Wrapper and can be easily modified to send other types of Custom Events to SignalFx, or to transform CloudWatch events before sending (for example, to filter only selected fields or to use different field names).

There are two ways to install this Lambda function: using AWS Serverless Repository (recommended) or AWS Lambda Console.

Filtering CloudWatch Events to send to SignalFx

After installation, this Lambda function will be triggered by a CloudWatch Event or a CloudWatch Event rule. You should use the rules to filter the CloudWatch Events you want to be forwarded.

Prerequisites

1. Prepare SignalFx Access Token

To retrieve your access token:

This value will be later used as a SignalFx Access Token.

2. Locate SignalFx Ingest Endpoint

By default, this Function will send data to the us0 realm. As a result, if you are not in the us0 realm, then you must explicitly set your realm.

To locate your realm:

This value will be later used as a SignalFx Ingest Endpoint.

3. (Optional) Create a managed KMS key to encrypt the SignalFx Access Token in transit
Overview

You will need to provide the Lambda Function with a SignalFx Access Token stored in an environment variable. As a best security practice, it is recommended that the token is encrypted using the Amazon Key Management Service. For the overview of this process see "Securing Environment Variables" section of AWS documentation.

In both installation methods, you have the option to use either encrypted or plain token variable.

Creating a KMS key

In order to encrypt SignalFx Access Token, please make sure you have a managed Symmetric KMS key available for use.

Regardless of the key creation method, make sure you have access to the cipher text output as well as the key id of the encryption key you used.

Installation

You can install this function in two ways:

Option 1: (Recommended) Install from Serverless Application Repository

In this option, you will deploy an Application from Serverless Application Repository. A Lambda Layer, Lambda Function, CloudWatch Event Rule and all necessary configurations will be created for you. This is a recommended and less time-consuming option.

NOTE: If you choose a version with token encryption, it will be by default configured to omit the events from aws.kms. The reason for this behavior is to avoid an infinite loop: when the lambda runs, it decrypts the token and thus generates a Cloudwatch Event.

Step 1: Locate the application in Serverless Application Repository

Step 3: Deploy

Option 2: Manually with AWS Lambda Console

Overview

In this option, you will create a Lambda function and manually configure its dependencies, environment variables and a trigger. While more time consuming, it will give you better understanding of the inner workings of the process.

Step 1: Create a Lambda function in AWS Console

Step 1.1: Create a function
Step 1.2: Copy function code and set the handler

NOTE: Alternative scripts: Instead of the default cloudwatch-event-forwarder.js file, you can copy any of the following files if they better suit your needs:

Step 2: Add a Layer to the function

Step 3: Configure environment variables

Option 1) Setup without token encryption
Option 2) Setup with token encryption

Step 4: Add a trigger to the function

NOTE: the list of services available in the Lambda Designer is a short list of most common choices. If you need a richer editor and all the sources available, you can create the rule through CloudWatch Events console and select the target there.

Step 5: The integration is installed

You're ready! The integration is now configured. See here how to view and use events in SignalFx.

Additional Information

Details on CloudWatch event to SignalFx Custom Event transformation

SignalFx Custom Event needs to be sent as a key-value map. This Lambda function will transform any CloudWatch event to conform to naming and format restrictions of a SignalFx, and then forward it to SignalFx.

If the default behavior does not suit your needs, please see the examples on how to build and send SignalFx Custom Events.

By default, based on a Cloudwatch Event, this lambda will create a SignalFx Custom Event in a following way:

Other keys will be transformed in the ways listed below and sent as properties:

For example, a sample CloudWatch event:

{
  "id":"7bf73129-1428-4cd3-a780-95db273d1602",
  "detail-type":"EC2 Instance State-change Notification",
  "source":"aws.ec2",
  "account":"123456789012",
  "time":"2015-11-11T21:29:54Z",
  "region":"us-east-1",
  "resources":[
    "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111"
  ],
  "detail":{
    "instance-id":"i-abcd1111",
    "state":"pending",
    "obj": {"key": "val"}
  }
}

will be transformed to a Custom SignalFx Event:

{
  "category":"USER_DEFINED",
  "eventType":"CloudWatch",
  "dimensions":{
    "detailType":"EC2 Instance State-change Notification",
    "source":"aws.ec2",
    "account":"123456789012",
    "region":"us-east-1"
  },
  "properties":{
    "id":"7bf73129-1428-4cd3-a780-95db273d1602",
    "resources":"[\"arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111\"]",
    "detail_instance-id":"i-abcd1111",
    "detail_state":"pending",
    "detail_obj":"{\"key\":\"val\"}"
  },
  "timestamp":1447277394000
}

Useful links: