Skip to content

Instantly share code, notes, and snippets.

@palumbo
Created January 10, 2018 01:06
Show Gist options
  • Save palumbo/71c03825c2bbe968de282f3ab7e1941e to your computer and use it in GitHub Desktop.
Save palumbo/71c03825c2bbe968de282f3ab7e1941e to your computer and use it in GitHub Desktop.
JS for pulling CloudWatch alarm data into zapier
/*
EXAMPLE:
input.messy_data_from_previous_trigger_or_action = 'This is a single string of text that contains lots of different data. {"name": "John"} Sometimes when you are building a Zap, you will need to pull out one piece of data from a long, messy string of text. {"email": "[email protected]"} Thankfully, you can use the "Formatter by Zapier" app or the "Code by Zapier" app to extract the information you need.'
*/
function extract_piece_of_data(entire_string_of_text, characters_before_desired_data, characters_after_desired_data) {
var desired_data_with_stuff_after_it = entire_string_of_text.substr(entire_string_of_text.indexOf(characters_before_desired_data) + characters_before_desired_data.length);
var desired_data = desired_data_with_stuff_after_it.substr(0, desired_data_with_stuff_after_it.indexOf(characters_after_desired_data));
return desired_data;
}
// Get the messy data string from Zapier's built-in "input" variable.
// var messy_data = input.messy_data_from_previous_trigger_or_action;
// Remember to escape any double quotes in your function arguments with the backslash (\) character
var service = extract_piece_of_data(inputData.body, "Service:", "Time:");
var event = extract_piece_of_data(inputData.body, "Event:", "AccountId:");
var time = extract_piece_of_data(inputData.body, "Time: ", "RequestId:");
var cause = extract_piece_of_data(inputData.body, "Cause: ", "StartTime:");
var instance_id = extract_piece_of_data(inputData.body, "EC2InstanceId: ", "Details:");
var customer = extract_piece_of_data(inputData.customer, "cloud.", "@mediatemple.net");
// var name = extract_piece_of_data(messy_data, "\"name\": \"", "\"} Sometimes");
// var email = extract_piece_of_data(messy_data, "\"email\": \"", "\"} Thankfully");
// Output the data as an object called "output". The object's keys can be whatever you want. In this example, I chose to use the keys first_name and email_address.
console.log(inputData.customer);
output = {aws_service: service, aws_event: event, time: time, cause: cause, instance_id: instance_id, customer: customer};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment