Objective
Show you how you can create a delivery stream that will ingest sample data, transforms it and store both the source and the transformed data.
Executive Summary
Amazon Kinesis Firehose is one of the easiest ways to prepare and load streaming data into the AWS ecosystem. Firehose was first released on October 2015 and it has evolved from just a simple solution to store your data without any modification to a delivery stream with transformation features. On July 2017 the delivery stream console was updated to offer you more options and so reduce the amount of work required to store and transform your data. This post offers you a guide to setup a proof of concept that will:
- Filter and transform sample data with n AWS Lambda function and store the results in S3.
- Keep the sample data to S3 for future analysis.
- Check the capabilities of the console, like encryption and compression.
- Take advantage of Firehose sample data producer (you won’t need to create any script).
Prerequisites
- You will need an AWS account.
Steps
STEP 0: ACCESS THE KINESIS FIREHOSE SERVICE
- Login into the AWS console.
- Search for the Kinesis service with “Find a service …” text box or as an item of the “Analytics” list.
- Click on “Create delivery stream”. In case you have not created a Kinesis stream before, you will need to press on “Get Started” first.
STEP 1: NAME AND SOURCE
- Select a name for your delivery stream, for this demo I will use “deliveryStream2018”.
- Choose a “Source”, for this demo select “Direct PUT or other resources”. Essentially you have two options here: Use a Kinesis Stream as the input for the delivery stream or you can send the records by other means:
- PUT API: You will use this option if your custom application will feed the delivery stream directly with the AWS SDK.
- Kinesis Agent: Use the agent to send information from logs produced by your applications, in other words, the agent will track the changes in your log files and send the information to the delivery stream.
- AWS IoT: If you have an IoT ecosystem, you can use the rules to send messages to your Firehose stream.
- CloudWatch Logs: Sends any incoming log events that match a defined filter to your delivery stream.
- CloudWatch Events: Deliver information of events when a CloudWatch rule is matched.
- Click “Next”.
STEP 6: TEST YOUR WORK
Firehose allows you to send demo data to your stream, let’s try it out.
- Select your stream radio button to enable the “Test with demo data” button.
- Click the “Test with demo data” button. You will see the “Test with demo data” section
- Select “Start sending demo data”.
- Do not leave this page until you complete the next steps, but be sure to stop the demo to save money once you see the results in your S3 bucket(s), if you close the tab, the demo data should stop too.
- In this same page, go down and check the “Monitoring” tab. Wait two minutes and use the refresh button to see the changes in the metrics.
- Wait up to 5 minutes then check your bucket for results, they will be inside folders representing the date. Download the files produced and see the results. Your “source_recods” folder has the backup data.
- What if something goes wrong? Where are the logs? Well, you can take check your logs in Cloudwatch. In the “Monitoring” tab, you will see a link to CloudWatch console, once there, select “Logs” on the menu, then look for your Lambda or Firehose logs in the list.
- Go back to the Firehose tab and select “Stop sending demo data”.
- Select “Create”, you will be taken back to the Function editor.
- Make sure to press “Save” to save your changes in the editor.
- Now run your test by selecting your test in the dropdown and press “Test”.
- You should get quick green results, check the details of the execution to know more.
- If you expand the “Details” section you will be able to see the output.
- You may want to look at the Base64 decoded object.
- In this case, we are filtering and transforming the stocks where price is 5.0 or greater. The one that we are using for testing has a 4.73 as price, so this record ends as a “Dropped” record, indicating that is not going to be part of the transformation set, but it did not provoke an error.
- A record that will be part of the transformation set will have a result attribute of “OK”.
- You can remove the filter if you want to transform all your data.
- Now you can go back to the Kinesis Firehose tab, you can return to to this tab later if you want to dig deeper.
- Back into the Firehose delivery stream wizard, close the “Choose Lambda blueprint” dialog.
- Select your newly created function in the “Lambda function” dropdown, refresh if necessary.
- Ignore the timeout warning, this lambda function does not require too much time to execute, so keep going and select “Next”.
{
"ticker_symbol":"NGC",
"sector":"HEALTHCARE",
"change":-0.08,
"price":4.73
}
{
"records": [
{
"recordId": "49583354031560888214100043296632351296610463251381092354000000",
"approximateArrivalTimestamp": 1523204766865,
"data": "eyJ0aWNrZXJfc3ltYm9sIjoiTkdDIiwic2VjdG9yIjoiSEVBTFRIQ0FSRSIsImNoYW5nZSI6LTAuMDgsInByaWNlIjo0LjczfQ=="
}
],
"region": "us-east-1",
"deliveryStreamArn": "arn:aws:kinesis:EXAMPLE",
"invocationId": "invocationIdExample"
}
- Go back to the function menu (the header), look for the dropdown where you can create a new test, it is right before the “Test” button, select “Configure Test Event” in the dropdown. A secondary window will appear.
- Select “Create new test event” to create a new test and “Kinesis Firehose” as “Event template”.
- Select an “Event name”.
- Copy and paste the next JSON object into the editor to use it as the input for your test:
'use strict';
console.log('Loading function');
/* Stock Ticker format parser */
const parser = /^{"ticker_symbol":"[A-Z]+","SECTOR":"[A-Z]+","change":[-.0-9]+,"price":[-.0-9]+}/i;
//"ticker_symbol":"NGC","sector":"HEALTHCARE","change":-0.08,"price":4.73
exports.handler = (event, context, callback) => {
let success = 0; // Number of valid entries found
let failure = 0; // Number of invalid entries found
let dropped = 0; // Number of dropped entries
/* Process the list of records and transform them */
const output = event.records.map((record) => {
const entry = (new Buffer(record.data, 'base64')).toString('utf8');
console.log("Entry: ", entry);
let match = parser.exec(entry);
if (match) {
let parsed_match = JSON.parse(match);
var milliseconds = new Date().getTime();
/* Add timestamp and convert to CSV */
const result = `${milliseconds},${parsed_match.ticker_symbol},${parsed_match.sector},${parsed_match.change},${parsed_match.price}`+"n";
const payload = (new Buffer(result, 'utf8')).toString('base64');
if (parsed_match.sector !== 'RETAIL') {
/* Dropped event, notify and leave the record intact */
dropped++;
return {
recordId: record.recordId,
result: 'Dropped',
data: record.data,
};
}
else {
/* Transformed event */
success++;
return {
recordId: record.recordId,
result: 'Ok',
data: payload,
};
}
}
else {
/* Failed event, notify the error and leave the record intact */
console.log("Failed event : "+ record.data);
failure++;
return {
recordId: record.recordId,
result: 'ProcessingFailed',
data: record.data,
};
}
});
console.log(`Processing completed. Successful records ${output.length}.`);
callback(null, { records: output });
STEP 2: TRANSFORM RECORDS
- Once data is available in a delivery stream, we can invoke a Lambda function to transform it. To our relief, some ready-to-use blueprints are offered by AWS and you can adapt them according to your data format. In this tutorial, we will transform sample data offered by Firehose, so select “Enabled”.
- Select “Create New”.
- You will see a list of blueprints for you to use. We will process custom data so select the first one “General Firehose Processing”. You will be taken to a new page, do not close the previous one, we will be back to it.
- The Lambda “Create function” page will open.
- Choose a “Name” for your function.
- the “Role” dropdown, select “Create new role from template(s)”, this will create a new role to allow this Lambda function to logging to CloudWatch. Choose a “Name role”, you may want to remember this one to delete it quickly when we are done with the tutorial.
- Leave the “Policy templates” field empty.
- Once you are ready select “Create function” and wait for the editor to appear.
STEP 2.1: INTO THE LAMBDA REALM
- Scroll down until you see the “Function code” section.
- Change “Runtime” to “Node.js 8.10”.
- The “index.js” file should be available to edit, if it is not, open the file with a double click in the file name on the left side.
- Remove all the code and copy the next function and paste it into the editor.
'use strict';
console.log('Loading function');
/* Stock Ticker format parser */
const parser = /^{"ticker_symbol":"[A-Z]+","SECTOR":"[A-Z]+","change":[-.0-9]+,"price":[-.0-9]+}/i;
//"ticker_symbol":"NGC","sector":"HEALTHCARE","change":-0.08,"price":4.73
exports.handler = (event, context, callback) => {
let success = 0; // Number of valid entries found
let failure = 0; // Number of invalid entries found
let dropped = 0; // Number of dropped entries
/* Process the list of records and transform them */
const output = event.records.map((record) => {
const entry = (new Buffer(record.data, 'base64')).toString('utf8');
console.log("Entry: ", entry);
let match = parser.exec(entry);
if (match) {
let parsed_match = JSON.parse(match);
var milliseconds = new Date().getTime();
/* Add timestamp and convert to CSV */
const result = `${milliseconds},${parsed_match.ticker_symbol},${parsed_match.sector},${parsed_match.change},${parsed_match.price}`+"n";
const payload = (new Buffer(result, 'utf8')).toString('base64');
if (parsed_match.sector !== 'RETAIL') {
/* Dropped event, notify and leave the record intact */
dropped++;
return {
recordId: record.recordId,
result: 'Dropped',
data: record.data,
};
}
else {
/* Transformed event */
success++;
return {
recordId: record.recordId,
result: 'Ok',
data: payload,
};
}
}
else {
/* Failed event, notify the error and leave the record intact */
console.log("Failed event : "+ record.data);
failure++;
return {
recordId: record.recordId,
result: 'ProcessingFailed',
data: record.data,
};
}
});
console.log(`Processing completed. Successful records ${output.length}.`);
callback(null, { records: output });
- Go back to the function menu (the header), look for the dropdown where you can create a new test, it is right before the “Test” button, select “Configure Test Event” in the dropdown. A secondary window will appear.
- Select “Create new test event” to create a new test and “Kinesis Firehose” as “Event template”.
- Select an “Event name”.
- Copy and paste the next JSON object into the editor to use it as the input for your test:
{
"records": [
{
"recordId": "49583354031560888214100043296632351296610463251381092354000000",
"approximateArrivalTimestamp": 1523204766865,
"data": "eyJ0aWNrZXJfc3ltYm9sIjoiTkdDIiwic2VjdG9yIjoiSEVBTFRIQ0FSRSIsImNoYW5nZSI6LTAuMDgsInByaWNlIjo0LjczfQ=="
}
],
"region": "us-east-1",
"deliveryStreamArn": "arn:aws:kinesis:EXAMPLE",
"invocationId": "invocationIdExample"
}
The data attribute is encoded in base64, this is the type of data received by Firehose. The value of this data after being parsed is:
{
"ticker_symbol":"NGC",
"sector":"HEALTHCARE",
"change":-0.08,
"price":4.73
}
- Select “Create”, you will be taken back to the Function editor.
- Make sure to press “Save” to save your changes in the editor.
- Now run your test by selecting your test in the dropdown and press “Test”.
- You should get quick green results, check the details of the execution to know more.
- If you expand the “Details” section you will be able to see the output.
- You may want to look at the Base64 decoded object.
- In this case, we are filtering and transforming the stocks where price is 5.0 or greater. The one that we are using for testing has a 4.73 as price, so this record ends as a “Dropped” record, indicating that is not going to be part of the transformation set, but it did not provoke an error.
- A record that will be part of the transformation set will have a result attribute of “OK”.
- You can remove the filter if you want to transform all your data.
- Now you can go back to the Kinesis Firehose tab, you can return to to this tab later if you want to dig deeper.
- Back into the Firehose delivery stream wizard, close the “Choose Lambda blueprint” dialog.
- Select your newly created function in the “Lambda function” dropdown, refresh if necessary.
- Ignore the timeout warning, this lambda function does not require too much time to execute, so keep going and select “Next”.
STEP 3: CHOOSE A DESTINATION
We have configured a serverless function to transform our records, but we have not selected where to store them, and neither if we want to keep the raw records. In this case, we will use both options.
- Select “Amazon S3” as destination for simplicity. This will be the service where we will store our transformed data.
- Select an existing bucket or create one.
- You may select a secondary prefix for your files, I will use “transformed” to distinguish it from the source files. Firehose will add a timestamp automatically in any case.
- In “S3 backup”, select “Enable” the store the raw data too. Select the destination bucket or create one, you may select a prefix for this too.
- Go ahead and press “Next”.
STEP 4: CONFIGURE SETTINGS
- Leave your S3 buffer conditions as they are. They indicate the maximum amount of time that must be passed or the maximum quantity of data that must be gathered before to execute your Lambda function. This is an OR condition, meaning when any of these rules are satisfied, the Lambda function will execute.
- If you want to save space and secure your data, you can select your desired compression and encryption options. I am using the defaults for this tutorial.
- Error logging is enabled by default, you can keep it like that in case you want to debug your code later.
- We need an IAM role to access the corresponding resources from Firehose, like S3. In the “IAM role” choose to “Create new, or Choose”, a new tab will open.
- As we have selected to use S3 in the previous steps, the IAM policy that we need has already been prepared for us, reviewed if you are interested and press on “Allow”. The role will be created and the tab will be closed.
- The new role will be listed in the “IAM role” dropdown, you can select more if needed.
- Select “Next” when ready.
STEP 5: REVIEW YOUR CONFIGURATION
- Take a moment to check the options that you have indicated, when ready select “Create delivery stream”.
- You will be taken to the “Firehose delivery stream” page, you should see your new stream active after some seconds.
STEP 6: TEST YOUR WORK
Firehose allows you to send demo data to your stream, let’s try it out.
- Select your stream radio button to enable the “Test with demo data” button.
- Click the “Test with demo data” button. You will see the “Test with demo data” section
- Select “Start sending demo data”.
- Do not leave this page until you complete the next steps, but be sure to stop the demo to save money once you see the results in your S3 bucket(s), if you close the tab, the demo data should stop too.
- In this same page, go down and check the “Monitoring” tab. Wait two minutes and use the refresh button to see the changes in the metrics.
- Wait up to 5 minutes then check your bucket for results, they will be inside folders representing the date. Download the files produced and see the results. Your “source_recods” folder has the backup data.
- What if something goes wrong? Where are the logs? Well, you can take check your logs in Cloudwatch. In the “Monitoring” tab, you will see a link to CloudWatch console, once there, select “Logs” on the menu, then look for your Lambda or Firehose logs in the list.
- Go back to the Firehose tab and select “Stop sending demo data”.
Cleaning
Once that you feel comfortable understanding the flow and the services used in this tutorial, it is a good idea to delete these resources. If you are under the Free Tier, you will only incur in costs when your Firehose delivery stream is being fed, and if you are outside of the Lambda and S3 free tier limits, so as long as you are not producing and inserting data into the stream, you will not be charged. Still, it is a good idea to remove all when you are done.
DELETE THE DELIVERY STREAM
- Go to the Firehose console page.
- Select your delivery stream.
- Press on the “Delete” or “Delete Delivery Stream” button depending on your location.
DELETE S3 FILES AND/OR BUCKET
- Go to the S3 console page.
- Note: To select and item on S3, do not press on the link, select the row or checkbox.
- You may want to remove the files only, in that case, access the S3 console, then select the folders inside the bucket, select them and on the “More” menu, select “Delete”.
- If you want to delete the bucket too, go back to the S3 console and select the destination bucket that you have used for this tutorial. Press on the “Delete Bucket” or “Delete Delivery Stream” button depending on your location.
DELETE THE LAMBDA FUNCTION
- Access the Lambda console.
- On the left menu, select “Functions”.
- Select your Lambda function and in the “Actions” menu, select “Delete”.
- You can also delete the function directly into the Function editor using “Actions” and then “Delete function”.
DELETE THE ROLES
- Remember that you have created two roles during this tutorial, one for Lambda and one for Firehose.
- Access the IAM console.
- Select “Roles” on the left menu.
- The one for Lambda was chosen by you in a previous step, look for it and select it. If you are not sure about it, you can check the creation time of the roles using the gear on top of the list to show extra information, this and the firehose role should have been created during the same period of time.
- The role created for Firehose should be named “firehose_delivery_role” unless you have chosen a different name.
- To delete them, select them using the checkboxes next to the item and then click on “Delete Role”. You will be presented with information about the roles to confirm they are the ones that you want.
Conclusion
Firehose is fully managed service and it will automatically scale to match your throughput requirements without any ongoing administration, you can extend its capabilities with Lamda functions as we have demonstrated in this tutorial where we have ingested data from a system that produces sample stock records, then we have filtered and transformed it to a different format and we are also keeping copy of the raw data for future analysis in S3.
References: