{"@attributes":{"version":"2.0"},"channel":{"title":"The Well Architected Life","description":"Building a Well Architected Life one habit at a time.\n","link":"https:\/\/chrislarson.me\/","pubDate":"Fri, 14 Jul 2017 15:17:14 -0500","lastBuildDate":"Fri, 14 Jul 2017 15:17:14 -0500","generator":"Jekyll v3.4.0","item":[{"title":"Deploy a Scala Project to AWS Lambda","description":"<p>In this tutorial I show you how to package and deploy a simple Scala project to AWS Lambda. Then I show you how to add the lambda to API Gateway to run in response to GET requests.<\/p>\n\n<h1 id=\"build-the-scala-project\">Build the Scala Project<\/h1>\n\n<ul>\n  <li>Create the <code class=\"highlighter-rouge\">build.sbt<\/code> file<\/li>\n  <li>Write the main scala program, <code class=\"highlighter-rouge\">src\/main\/scala\/example\/Main.scala<\/code><\/li>\n  <li>In the terminal run <code class=\"highlighter-rouge\">sbt<\/code> then in the sbt prompt run <code class=\"highlighter-rouge\">assembly<\/code>.<\/li>\n  <li>This creates the jar which will be located at <code class=\"highlighter-rouge\">target\/scala-2.11\/lambda-demo-assembly-1.0.jar<\/code><\/li>\n<\/ul>\n\n<h3 id=\"buildsbt\">build.sbt<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>javacOptions ++= Seq(\"-source\", \"1.8\", \"-target\", \"1.8\", \"-Xlint\")\n\nlazy val root = (project in file(\".\")).\n  settings(\n    name := \"lambda-demo\",\n    version := \"1.0\",\n    scalaVersion := \"2.11.4\",\n    retrieveManaged := true,\n    libraryDependencies += \"com.amazonaws\" % \"aws-lambda-java-core\" % \"1.0.0\",\n    libraryDependencies += \"com.amazonaws\" % \"aws-lambda-java-events\" % \"1.0.0\"\n  )\n\nmergeStrategy in assembly :=\n{\n  case PathList(\"META-INF\", xs @ _*) =&gt; MergeStrategy.discard\n  case x =&gt; MergeStrategy.first\n}\n<\/code><\/pre>\n<\/div>\n\n<h3 id=\"srcmainscalaexamplemainscala\">src\/main\/scala\/example\/Main.scala<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>package example;\n\nclass Main {\n  import java.io.{InputStream, OutputStream, PrintStream}\n\n  def greeting(input: InputStream, output: OutputStream): Unit = {\n    val result = s\"Yo!\"\n    output.write(result.getBytes(\"UTF-8\"))\n  }\n}\n<\/code><\/pre>\n<\/div>\n\n<h1 id=\"deploy-the-scala-project-to-aws-lambda\">Deploy the Scala Project to AWS Lambda<\/h1>\n\n<ul>\n  <li>Select Java 8<\/li>\n  <li>Upload the jar which will be located at <code class=\"highlighter-rouge\">\/target\/scala-2.11\/lambda-demo-assembly-1.0.jar<\/code><\/li>\n  <li>Test the Lambda Function from the AWS Lambda UI<\/li>\n<\/ul>\n\n<p>At this point you could setup API Gateway to use the Scala Lambda in a <a href=\"\/blog\/tutorial\/lambda\/2016\/12\/16\/1x1_pixel_aws_lambda.html\">similar way that I set it up for the node.js Lambda Function.<\/a><\/p>\n\n","pubDate":"Fri, 31 Mar 2017 08:14:00 -0500","link":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/03\/31\/aws-lambda-scala.html","guid":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/03\/31\/aws-lambda-scala.html","category":["blog","tutorial","lambda"]},{"title":"CORS for API Gateway and Lambda Functions","description":"<p>AWS has a great <a href=\"http:\/\/docs.aws.amazon.com\/apigateway\/latest\/developerguide\/how-to-cors.html\">blog post about enabling CORS on API Gateway.<\/a> I followed the AWS blog and CORS works well for most requests. The one thing it didn\u2019t work for was secure request made from JavaScript.<\/p>\n\n<h3 id=\"javascript-get-request\">JavaScript GET request<\/h3>\n<p>This code worked, but would not send cookies that were set for the example.com domain.<\/p>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code> xhr.open('GET', 'https:\/\/subdomain.example.com\/ab.json\u2019, true);\n  xhr.withCredentials = true;\n  xhr.onreadystatechange= function() {\n    if (this.readyState!==4) return;\n    if (this.status!==200) return; \/\/ or whatever error handling you want\n    document.getElementById('ab').innerHTML= this.responseText;\n  };\n  xhr.send(null);\n<\/code><\/pre>\n<\/div>\n\n<h3 id=\"chrome-browser-error\">Chrome Browser Error<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>XMLHttpRequest cannot load https:\/\/subdomain.example.com\/ab.json. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'https:\/\/example.com' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.\n<\/code><\/pre>\n<\/div>\n\n<p>The method in the AWS blog works well but sets Access-Control-Allow-Origin to \u2018*\u2019 which is not compatible with secure requests. In the case that a cross origin request is made for an HTTPS resource the browser will throw an error if the Access-Control-Allow-Origin is not EXACTLY the same as the Origin, this includes requests from subdomains.<\/p>\n\n<p>The solution is to pass the request Origin to Access-Control-Allow-Origin. Since I am using AWS Lambda functions with my API Gateway, I use the Lambda function to read the Origin, filter it and return it to API Gateway.<\/p>\n\n<p>First I setup API Gateway to respond with the required headers.<\/p>\n\n<h3 id=\"create-the-options-method\">Create the OPTIONS Method<\/h3>\n\n<p>Under Method Response add a 200 response.\nAdd the CORS headers to the 200 response.<\/p>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>Access-Control-Allow-Headers\nAccess-Control-Allow-Origin\nAccess-Control-Allow-Credentials\nAccess-Control-Allow-Methods\n<\/code><\/pre>\n<\/div>\n\n<p>Under Integration Response add the values to each of the CORS headers<\/p>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>Access-Control-Allow-Headers      'Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with'\nAccess-Control-Allow-Origin       '*'\nAccess-Control-Allow-Credentials  'true'\nAccess-Control-Allow-Methods      'GET'\n<\/code><\/pre>\n<\/div>\n\n<h3 id=\"create-or-modify-the-get-method\">Create or modify the GET Method<\/h3>\n\n<p>The GET method may already be present if you are adding CORS to an existing API<\/p>\n\n<p>Under Method Response add a 200 response.\nAdd the CORS headers to the 200 response.<\/p>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>Access-Control-Allow-Headers\nAccess-Control-Allow-Origin\nAccess-Control-Allow-Credentials\nAccess-Control-Allow-Methods\n<\/code><\/pre>\n<\/div>\n\n<p>Under Integration Response add the values to each of the CORS headers<\/p>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>Access-Control-Allow-Headers      'Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with'\nAccess-Control-Allow-Origin       integration.response.body.Origin\nAccess-Control-Allow-Credentials  'true'\nAccess-Control-Allow-Methods      'GET'\n<\/code><\/pre>\n<\/div>\n\n<p>Then in the Lambda Function associated with the GET method I send back the Origin. I do some simple filtering on the Origin and only pass it through if it is one of my subdomains.<\/p>\n\n<h3 id=\"indexjs\">index.js<\/h3>\n\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>\u2018use strict\u2019\n\nexports.handler = function (event, context, callback) {\n  ...\n  const origin = filter(event.params.header.Origin);\n  const someHtml = \u201c&lt;h1&gt;Title&lt;\/h1&gt;\u201d;\n  \u2026\n  callback(null, { html: someHtml, Origin:origin });\n}\n\nfunction filter(origin) {\n  var result = origin ? origin : '*';\n  if( result === \u201chttps:\/\/subdomain.example.com\u201d ) {\n    \/\/ just a simple filter example, not to be used in production\n    return \u201chttps:\/\/subdomain.example.com\u201d;\n  }\n  return '*';\n}\n<\/code><\/pre>\n<\/div>\n\n<p>Now when I visit example.com and make secure requests to subdomain.example.com cookies are sent without error or warning.<\/p>\n","pubDate":"Fri, 10 Mar 2017 15:05:00 -0600","link":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/03\/10\/cors-api-gateway-lambda.html","guid":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/03\/10\/cors-api-gateway-lambda.html","category":["blog","tutorial","lambda"]},{"title":"Deploy node.js package to AWS Lambda","description":"<p>This is the fifth iteration of my tracking pixel experiment:<\/p>\n\n<p><a href=\"\/blog\/tutorial\/lambda\/2016\/12\/16\/1x1_pixel_aws_lambda.html\">AWS Lambda function to return a tracking pixel<\/a><\/p>\n\n<p><a href=\"\/blog\/tutorial\/lambda\/2016\/12\/19\/lambda-elasticsearch.html\">AWS Lambda Tracking Pixel with Elasticsearch<\/a><\/p>\n\n<p><a href=\"\/blog\/tutorial\/lambda\/2017\/01\/04\/lambda-kinesis-firehose-elasticsearch.html\">AWS Lambda, Kinesis Firehose and Elasticsearch<\/a><\/p>\n\n<p><a href=\"\/blog\/tutorial\/lambda\/2017\/02\/06\/lambda-detect-device.html\">AWS Lambda Detect Android or iOS device node.js<\/a><\/p>\n\n<p>In this tutorial I show you how to create a node.js project, include node_modules, zip the project and deploy the project to AWS Lambda.<\/p>\n\n<p>I\u2019m going to take the code from the <a href=\"\/blog\/tutorial\/lambda\/2016\/12\/16\/1x1_pixel_aws_lambda.html\">first tutorial<\/a> and use the <a href=\"http:\/\/usecuid.org\/\">node-cuid module<\/a> to create a unique cookie id.<\/p>\n\n<p>First I create an empty directory <code class=\"highlighter-rouge\">mkdir lambda-test<\/code>, then <code class=\"highlighter-rouge\">cd lambda-test<\/code> and install the aws-sdk and node-cuid modules.<\/p>\n\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>mkdir lambda-test\ncd lambda-test\nnpm init\nnpm install --save aws-sdk\nnpm install --save cuid\n<\/code><\/pre>\n<\/div>\n\n<p>Once the modules are installed I can create index.js and copy this code to it<\/p>\n\n<h3 id=\"indexjs\">index.js<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>'use strict';\nvar cuid = require('cuid');\n\nexports.handler = function(event, context, callback) {\n  const cookie_name = \"_cl\";\n  var date = new Date();\n  var cookieVal = cuid(); \/\/ Generate a random cookie string\n  var cookieString = cookie_name+\"=\"+cookieVal;\n  var cookie = '';\n  \n    if (event.hasOwnProperty('params') &amp;&amp; event.params.hasOwnProperty('header') &amp;&amp; event.params.header.hasOwnProperty('Cookie')) {\n        cookie = getCookie(cookie_name, event.params.header.Cookie);\n        if (!cookie) {\n            cookieString = cookie_name + \"=\" + cookieVal;\n        } else {\n            cookieString = cookie_name + \"=\" + cookie;\n        }\n    }\n\n  \/\/ Get Unix milliseconds at current time plus 365 days\n  date.setTime(+ date + (365 * 86400000)); \/\/24 \\* 60 \\* 60 \\* 100\n  cookieString = cookieString+\"; \"+\"expires=\"+date.toGMTString()+\";\";\n\n  var imageHex = \"\\x42\\x4d\\x3c\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x36\\x00\\x00\\x00\\x28\\x00\"+ \n  \"\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x18\\x00\\x00\\x00\"+ \n  \"\\x00\\x00\\x06\\x00\\x00\\x00\\x27\\x00\\x00\\x00\\x27\\x00\\x00\\x00\\x00\\x00\"+\n  \"\\x00\\x00\\x00\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x00\\x00\";\n\n  callback(null, { \"body\":imageHex, \"Cookie\":cookieString });\n\n};\n\nfunction getCookie(name, cookies) {\n  var value = \"; \" + cookies;\n  var parts = value.split(\"; \" + name + \"=\");\n  if (parts.length &gt;= 2) return parts.pop().split(\";\").shift();\n}\n<\/code><\/pre>\n<\/div>\n\n<p>At this point I could zip the node.js project and use the AWS GUI to upload and deploy. Instead of testing on AWS, I like to do a quick test on my local machine.<\/p>\n\n<p>Install node-lambda globally. This module makes it easy to test AWS Lambda functions locally.<\/p>\n\n<p><code class=\"highlighter-rouge\">npm install -g node-lambda<\/code><\/p>\n\n<p>Then run <code class=\"highlighter-rouge\">$ node-lambda setup<\/code> in the lambda-test directory<\/p>\n\n<p>This will create 3 files: deploy.env, event.json, and context.json<\/p>\n\n<p>Open and edit <code class=\"highlighter-rouge\">event.json<\/code> to match the test events you may already have defined for your AWS Lambda functions<\/p>\n\n<h3 id=\"eventjson\">event.json<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code><span class=\"p\">{<\/span><span class=\"w\">\n  <\/span><span class=\"nt\">\"params\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"nt\">\"path\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nt\">\"querystring\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nt\">\"header\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n      <\/span><span class=\"nt\">\"Accept\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,*\/*;q=0.8\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n      <\/span><span class=\"nt\">\"Accept-Encoding\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"gzip, deflate, sdch, br\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n      <\/span><span class=\"nt\">\"Accept-Language\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"en-US,en;q=0.8\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n      <\/span><span class=\"nt\">\"Cache-Control\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"no-cache\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">}<\/span><span class=\"w\">\n  <\/span><span class=\"p\">},<\/span><span class=\"w\">\n  <\/span><span class=\"nt\">\"context\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"nt\">\"http-method\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"GET\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nt\">\"resource-path\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\/test.gif\"<\/span><span class=\"w\">\n  <\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre>\n<\/div>\n\n<p>Save event.json and run <code class=\"highlighter-rouge\">$ node-lambda run<\/code> on the console. This will simulate a lambda test and use event.json as the input. If everything works you will see a success in the console and a print out of the pixel and cookie values.<\/p>\n\n<h3 id=\"example-response-from-node-lambda-run\">example response from node-lambda run<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>Success:\n{\"body\":\"BM&lt;\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u00006\\u0000\\u0000\\u0000(\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0001\\u0000\\u0018\\u0000\\u0000\\u0000\\u0000\\u0000\\u0006\\u0000\\u0000\\u0000'\\u0000\\u0000\\u0000'\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\u00ff\u00ff\u00ff\u00ff\\u0000\\u0000\",\"Cookie\":\"_cl=cizqc3be800008ouxn5l6vdw6; expires=Thu, 01 Mar 2018 02:19:26 GMT;\"}\n<\/code><\/pre>\n<\/div>\n\n<p>Now to create a zip: run <code class=\"highlighter-rouge\">$ node-lambda package<\/code> this will create a zip file that will be located in the build folder. In the AWS Lambda Console upload this zip file in the function package. If the main file is named index.js (as I show above) then the default Lambda values are fine. Then save and test. For more details checkout the <a href=\"http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/nodejs-create-deployment-pkg.html\">AWS Tutorial - Deployment Package (Node.js)<\/a>.<\/p>\n\n<h3 id=\"api-gateway\">API Gateway<\/h3>\n\n<p>At this point the Lambda function should be successfully deployed and tested. Now I integrate it with AWS API Gateway. I setup the API Gateway as I did in the <a href=\"\/blog\/tutorial\/lambda\/2017\/02\/06\/lambda-detect-device.html\">other tutorials<\/a>. In addition, I added Set-Cookie to the Response Header in the Method Response.<\/p>\n\n<p>Once Set-Cookie is added to the Method Response it is available to edit in the Integration Response. In the Integration Response I expand the table, then expand the Header Mappings table. In the Header Mappings table I edit the Set-Cookie and add the value <code class=\"highlighter-rouge\">integration.response.body.Cookie<\/code>, click the tiny checkmark and click save.<\/p>\n\n<p>Also in the Integration Response, I expand the Body Mappings Templates and verify I only have <code class=\"highlighter-rouge\">image\/gif<\/code> set in the Content-Type table.<\/p>\n\n<h3 id=\"integration-response---body-mappings-templates---content-type---imagegif\">Integration Response -&gt; Body Mappings Templates -&gt; Content-Type -&gt; \u2018image\/gif\u2019<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>#set($inputRoot = $input.path('$'))\n$inputRoot.body\n<\/code><\/pre>\n<\/div>\n\n<p>Now I deploy the API Gateway to my dev stage and visit the endpoint with Chrome. The response should be an invisible GIF and a cookie should be set. If I visit the endpoint again the cookie should remain the same. If I clear the cookie then visit the endpoint I should have a new cookie value set.<\/p>\n","pubDate":"Tue, 28 Feb 2017 19:05:00 -0600","link":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/02\/28\/lambda-from-nodejs-package.html","guid":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/02\/28\/lambda-from-nodejs-package.html","category":["blog","tutorial","lambda"]},{"title":"AWS Lambda Node.js Function to Detect Android or iOS device","description":"<p>This is the fourth iteration of my AWS Lambda experiments:<\/p>\n\n<p><a href=\"\/blog\/tutorial\/lambda\/2016\/12\/16\/1x1_pixel_aws_lambda.html\">AWS Lambda function to return a tracking pixel<\/a><\/p>\n\n<p><a href=\"\/blog\/tutorial\/lambda\/2016\/12\/19\/lambda-elasticsearch.html\">AWS Lambda Tracking Pixel with Elasticsearch<\/a><\/p>\n\n<p><a href=\"\/blog\/tutorial\/lambda\/2017\/01\/04\/lambda-kinesis-firehose-elasticsearch.html\">AWS Lambda, Kinesis Firehose and Elasticsearch<\/a><\/p>\n\n<p>In this tutorial I demonstrate how to serve different HTML based on the device type. Detecting the device type allows me to serve iTunes links to iPhone and Google Play links to Android. In the previous posts I demonstrated how to set a cookie with a web token, log requests to Elasticsearch, and scale using Kinesis Firehose.<\/p>\n\n<p>Here\u2019s the AWS Lambda function in node.js to read the User-Agent string and return different HTML for iPhone or Android devices. If the device cannot be determined I return HTML with links for each type of device.<\/p>\n\n<h3 id=\"aws-lambda-function-to-detect-device-type-and-return-html\">AWS Lambda Function to detect device type and return HTML<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>'use strict';\n\nexports.handler = function (event, context) {\n    var userAgent = '';\n    var htmlLink = '';\n    var oss = 'unknown';\n\n    if (event.hasOwnProperty('params') &amp;&amp; event.params.hasOwnProperty('header') &amp;&amp; event.params.header.hasOwnProperty('User-Agent')) {\n        userAgent = event.params.header[\"User-Agent\"];\n    }\n\n    if (userAgent.match(\"iPhone\")) {        \n        oss = \"ios\";    \n    } else if (userAgent.match(\"Android\")) {       \n        oss = \"android\";\n    }\n\n    switch(oss) {\n        case \"ios\":\n            htmlLink = \"&lt;h1&gt;Click the link below to subscribe to the Podcast&lt;\/h1&gt;\"+ \n            \"&lt;a href='https:\/\/itunes.apple.com\/us\/podcast\/internet-ballers-michael-pasha\/id1108577427'&gt;Subscribe on iTunes&lt;\/a&gt;\"+ \n            \"\"+\n            \"\";\n            break;\n        case \"android\":\n            htmlLink = \"&lt;h1&gt;Click the link below to subscribe to the Podcast&lt;\/h1&gt;\"+ \n            \"&lt;a href='http:\/\/subscribeonandroid.com\/www.internetballers.com\/feed\/podcast\/'&gt;Subscribe on Google Play&lt;\/a&gt;\"+ \n            \"\"+\n            \"\";\n            break;\n        default:\n            htmlLink = \"&lt;h1&gt;Click the link below to subscribe to the Podcast&lt;\/h1&gt;\"+\n            \"&lt;a href='https:\/\/itunes.apple.com\/us\/podcast\/internet-ballers-michael-pasha\/id1108577427'&gt;Subscribe on iTunes&lt;\/a&gt;\"+\n            \"&lt;br &gt;\" +\n            \"&lt;a href='http:\/\/subscribeonandroid.com\/www.internetballers.com\/feed\/podcast\/'&gt;Subscribe on Google Play&lt;\/a&gt;\"+\n            \"&lt;br &gt;\" +\n            \"&lt;a href='http:\/\/www.internetballers.com\/feed\/podcast\/'&gt;Subscribe to RSS&lt;\/a&gt;\"+ \n            \"\"+\n            \"\";\n        }\n\n    context.done(null, { \"body\": htmlLink });\n\n};\n\n<\/code><\/pre>\n<\/div>\n\n<p>The setup is the same as it is in the previous tutorials.<\/p>\n\n<p>I setup an AWS API Gateway in front of this Lambda function. To get the header data in the Lambda function I had to modify the <em>Integration Method<\/em>.<\/p>\n\n<p>I setup a GET method for my API Resource. For the GET method I modified the <em>Integration Request<\/em> by setting the <em>Body Mapping Templates<\/em> to the third option, Never.<\/p>\n\n<p>Then I added a Content-Type of application\/json and for the template I selected Generate template: <em>Method request passthrough<\/em>. This creates a mapping that passes all the header data to the Lambda function.<\/p>\n\n<h3 id=\"integration-request-body-mapping-templates-applicationjson-method-request-passthrough-mapping-template\">Integration Request, Body Mapping Templates, application\/json, Method request passthrough mapping template<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>##  See http:\/\/docs.aws.amazon.com\/apigateway\/latest\/developerguide\/api-gateway-mapping-template-reference.html\n##  This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body\/payload\n#set($allParams = $input.params())\n{\n\"params\" : {\n#foreach($type in $allParams.keySet())\n    #set($params = $allParams.get($type))\n\"$type\" : {\n    #foreach($paramName in $params.keySet())\n    \"$paramName\" : \"$util.escapeJavaScript($params.get($paramName))\"\n        #if($foreach.hasNext),#end\n    #end\n}\n    #if($foreach.hasNext),#end\n#end\n},\n\"context\" : {\n    \"account-id\" : \"$context.identity.accountId\",\n    \"api-id\" : \"$context.apiId\",\n    \"api-key\" : \"$context.identity.apiKey\",\n    \"authorizer-principal-id\" : \"$context.authorizer.principalId\",\n    \"caller\" : \"$context.identity.caller\",\n    \"cognito-authentication-provider\" : \"$context.identity.cognitoAuthenticationProvider\",\n    \"cognito-authentication-type\" : \"$context.identity.cognitoAuthenticationType\",\n    \"cognito-identity-id\" : \"$context.identity.cognitoIdentityId\",\n    \"cognito-identity-pool-id\" : \"$context.identity.cognitoIdentityPoolId\",\n    \"http-method\" : \"$context.httpMethod\",\n    \"stage\" : \"$context.stage\",\n    \"source-ip\" : \"$context.identity.sourceIp\",\n    \"user\" : \"$context.identity.user\",\n    \"user-agent\" : \"$context.identity.userAgent\",\n    \"user-arn\" : \"$context.identity.userArn\",\n    \"request-id\" : \"$context.requestId\",\n    \"resource-id\" : \"$context.resourceId\",\n    \"resource-path\" : \"$context.resourcePath\"\n    }\n}\n<\/code><\/pre>\n<\/div>\n","pubDate":"Mon, 06 Feb 2017 10:36:00 -0600","link":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/02\/06\/lambda-detect-device.html","guid":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/02\/06\/lambda-detect-device.html","category":["blog","tutorial","lambda"]},{"title":"Unboxing the Photon Maker Kits","description":"<p>For Christmas I received a Photon Maker Kit from <a href=\"https:\/\/www.particle.io\/\">Particle<\/a><\/p>\n\n<p>This is the kit I will be using for the IIoT tutorials and blog posts. The kit includes just about everything to build IIoT devices. The one thing it does not include is a 24 VDC power converter. Most Industrial power is 24 VDC. I can make due with the USB power connector or now.<\/p>\n\n<h1 id=\"the-travel-case\">The Travel Case<\/h1>\n<amp-img width=\"600\" height=\"450\" alt=\"Unboxing the Photon Maker kit from Particle\" layout=\"responsive\" src=\"\/img\/photon-maker-kit-02.jpg \"><\/amp-img>\n\n<amp-img width=\"600\" height=\"450\" alt=\"Unboxing the Photon Maker kit from Particle\" layout=\"responsive\" src=\"\/img\/photon-maker-kit-01.jpg \"><\/amp-img>\n\n<h1 id=\"the-photon-and-breadboard\">The Photon and Breadboard<\/h1>\n\n<amp-img width=\"600\" height=\"450\" alt=\"Unboxing the Photon Maker kit from Particle\" layout=\"responsive\" src=\"\/img\/photon-maker-kit-photon-02.jpg \"><\/amp-img>\n\n<amp-img width=\"600\" height=\"450\" alt=\"Unboxing the Photon Maker kit from Particle\" layout=\"responsive\" src=\"\/img\/photon-maker-kit-photon-01.jpg \"><\/amp-img>\n\n<h1 id=\"the-parts-and-jumper-wires\">The Parts and Jumper Wires<\/h1>\n\n<amp-img width=\"600\" height=\"450\" alt=\"Unboxing the Photon Maker kit from Particle\" layout=\"responsive\" src=\"\/img\/photon-maker-kit-parts-02.jpg \"><\/amp-img>\n\n<amp-img width=\"600\" height=\"450\" alt=\"Unboxing the Photon Maker kit from Particle\" layout=\"responsive\" src=\"\/img\/photon-maker-kit-parts-01.jpg \"><\/amp-img>\n\n<h3 id=\"heres-everything-that-comes-in-the-kit\">Here\u2019s everything that comes in the kit:<\/h3>\n\n<ul>\n  <li>Photon with Headers (1)<\/li>\n  <li>USB Micro B Cable (1)<\/li>\n  <li>Flex antenna (1)<\/li>\n  <li>Mini Breadboard (1)<\/li>\n  <li>Proto-board (1)<\/li>\n  <li>Deluxe Jumper Wire Pack (1)<\/li>\n  <li>Male to Female Jumper Wires (10)<\/li>\n  <li>Battery holder - 4xAA (1)<\/li>\n  <li>Headers (7)<\/li>\n  <li>Ceramic Capacitors (10 each)<\/li>\n  <li>Electrolytic Capacitor 100uF (5)<\/li>\n  <li>LEDs (10)<\/li>\n  <li>RGB LED (1)<\/li>\n  <li>Diode (6)<\/li>\n  <li>IR LED (1)<\/li>\n  <li>Resistors (30)<\/li>\n  <li>Photoresistors (2)<\/li>\n  <li>10K Rotary Potentiometer (1)<\/li>\n  <li>Temperature Sensor (1)<\/li>\n  <li>Temperature Sensor - Sealed (1)<\/li>\n  <li>Piezo Buzzer (1)<\/li>\n  <li>Mini Pushbuttons (3)<\/li>\n  <li>SPDT Switch (2)<\/li>\n  <li>SPDT Relay (1)<\/li>\n  <li>NPN Transistor (1)<\/li>\n  <li>PIR sensor (1)<\/li>\n  <li>Pancake Vibration Motor (1)<\/li>\n  <li>Micro Servo (1)<\/li>\n  <li>Serial OLED Screen,0.96\u201d(1)<\/li>\n<\/ul>\n","pubDate":"Thu, 05 Jan 2017 20:30:00 -0600","link":"https:\/\/chrislarson.me\/blog\/tutorial\/2017\/01\/05\/photon-maker-kit.html","guid":"https:\/\/chrislarson.me\/blog\/tutorial\/2017\/01\/05\/photon-maker-kit.html","category":["blog","tutorial"]},{"title":"AWS Lambda, Kinesis Firehose and Elasticsearch","description":"<p>This is the third iteration of my tracking pixel experiment:<\/p>\n\n<p><a href=\"\/blog\/tutorial\/lambda\/2016\/12\/16\/1x1_pixel_aws_lambda.html\">AWS Lambda function to return a tracking pixel<\/a><\/p>\n\n<p><a href=\"\/blog\/tutorial\/lambda\/2016\/12\/19\/lambda-elasticsearch.html\">AWS Lambda Tracking Pixel with Elasticsearch<\/a><\/p>\n\n<p>The Lambda to Elasticsearch function works well, but what happens when there is a traffic spike?<\/p>\n\n<p>In this tutorial I add <a href=\"https:\/\/aws.amazon.com\/kinesis\/firehose\/\">Kinesis Firehose<\/a> so my app scales quickly. The incoming requests are written to Kinesis Firehose and later sent to Elasticsearch and also backed up in S3.<\/p>\n\n<p>First, I used <a href=\"https:\/\/aws.amazon.com\/elasticsearch-service\/\">AWS Elasticsearch service<\/a> to setup a t2.micro instance running Kibana and Elasticsearch.<\/p>\n\n<p>Second, I setup <a href=\"https:\/\/aws.amazon.com\/kinesis\/firehose\/\">Kinesis Firehose<\/a> to publish to S3 and the Elasticsearch cluster I just created.<\/p>\n\n<p>Third, I setup API Gateway as I did in the previous blog posts.<\/p>\n\n<p>Finally, I updated the lambda function to publish to Kinesis Firehose instead of Elasticsearch.<\/p>\n\n<h3 id=\"lambda-function-to-log-events-to-kinesis-firehose\">Lambda Function to log events to Kinesis Firehose<\/h3>\n<div class=\"highlighter-rouge\"><pre class=\"highlight\"><code>'use strict';\n\nvar AWS = require('aws-sdk');\nvar setRegion = process.env['us-east-1'];\nAWS.config.update({ region: 'us-east-1' });\nvar path = require('path');\n\nvar fh = new AWS.Firehose({\n\t\tapiVersion : '2015-08-04',\n\t\tregion : setRegion\n\t    });\n\nvar imageHex = \"\\x42\\x4d\\x3c\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x36\\x00\\x00\\x00\\x28\\x00\" +\n    \"\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x18\\x00\\x00\\x00\" +\n    \"\\x00\\x00\\x06\\x00\\x00\\x00\\x27\\x00\\x00\\x00\\x27\\x00\\x00\\x00\\x00\\x00\" +\n    \"\\x00\\x00\\x00\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x00\\x00\";\nvar es_data = {};\n\nexports.handler = function (event, context) {\n    \/\/ Add date for Elasticsearch index\n    var d = new Date();\n    es_data.date = d.toISOString();\n    es_data.context = event.context;\n    es_data.params = event.params;\n\n    var jsonDoc = JSON.stringify(es_data);\n\n    fh.putRecord(\n        {\n            DeliveryStreamName: 'kinesis-firehose-stream-name',\n            Record: { Data: jsonDoc }\n        },\n        function (err, data) {\n            if (err) {\n                console.log(err);            \/\/ an error occurred\n            } else {\n                console.log(data);           \/\/ successful response\n            }\n            context.done(err, { \"body\": imageHex });\n        });\n\n};\n<\/code><\/pre>\n<\/div>\n","pubDate":"Wed, 04 Jan 2017 09:43:00 -0600","link":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/01\/04\/lambda-kinesis-firehose-elasticsearch.html","guid":"https:\/\/chrislarson.me\/blog\/tutorial\/lambda\/2017\/01\/04\/lambda-kinesis-firehose-elasticsearch.html","category":["blog","tutorial","lambda"]},{"title":"Vacation","description":"<p>Finished out 2016 on a relaxing vacation with my favorite people, read lots more books and enjoyed life.<\/p>\n\n<p>Welcome 2017!<\/p>\n","pubDate":"Tue, 03 Jan 2017 20:47:00 -0600","link":"https:\/\/chrislarson.me\/30days\/iiot\/2017\/01\/03\/day23.html","guid":"https:\/\/chrislarson.me\/30days\/iiot\/2017\/01\/03\/day23.html","category":["30days","iiot"]},{"title":"Getting value from your data","description":"<p>Collecting data is easy.<\/p>\n\n<p>Analyzing data is fun.<\/p>\n\n<p>Preparing collected data for analysis is difficult.<\/p>\n\n<p>Recently I started moving my websites off of Google Analytics. Instead of using Google, I\u2019ve setup my own web traffic analytics. Collecting traffic data and links was easy. It took about an hour to set up.<\/p>\n\n<p>Analyzing data is easy as well. There are a couple amazing projects that handle large amounts of data. I\u2019m using <a href=\"https:\/\/www.elastic.co\/products\/kibana\">Kibana from elastic.co<\/a>. Kibana has a fun interface for creating all sorts of amazing looking graphs.<\/p>\n\n<p>The difficult part is transforming the log files from my web server and loading them into <a href=\"https:\/\/www.elastic.co\/products\/elasticsearch\">Elasticsearch<\/a>, the backend for Kibana.<\/p>\n\n<p>The data is stored in individually compressed files created once an hour from each of my webservers. The log files are not consistent. The format for each log entry is roughly the same. Data might be appended or omitted depending on the information available at the time of the log event.<\/p>\n\n<p>I need a complex piece of software to uncompress new log files, look for duplicate entries, combine files from each server into one file, and then load that file into Elasticsearch. Most of the time a Data Scientist isn\u2019t doing data analysis. Most of the time is spent transforming data from one format to another.<\/p>\n","pubDate":"Fri, 23 Dec 2016 07:38:00 -0600","link":"https:\/\/chrislarson.me\/30days\/iiot\/2016\/12\/23\/day22.html","guid":"https:\/\/chrislarson.me\/30days\/iiot\/2016\/12\/23\/day22.html","category":["30days","iiot"]},{"title":"Predictive Maintenance Myth","description":"<p>The dashboard light on my car came on last month. Along with the light a message was displayed each time I turned on the car. The brake pad monitor was telling me the brake pads had worn thin. This type of predictive maintenance was built in when the car was new.<\/p>\n\n<p>Most cars can\u2019t tell when the brake pads are thin. It requires a special type of brake pad with a built in sensor. In order for an old car to take advantage of predictive maintenance it would need to be fitted with custom brake pads that include a sensor. Then a computer and wiring would need to be added to monitor the new sensor.<\/p>\n\n<p>Marketing materials sell predictive maintenance as a magical device set on an old machine that automatically knows when to alert of maintenance issues. I imagine it to be like tossing an Amazon Echo into the car\u2019s glove box and expecting it to report when the brake pads are thin.<\/p>\n\n<p>Predictive Maintenance is not that simple.<\/p>\n\n<p>Instead of adding sensors and computers, most car owners would benefit from taking their car in for inspection every 10,000 miles or 6 months. The mechanic checks 140 or so things and one of them is the thickness of each brake pad. Fancy IIoT is not required most of the time. Most of the time what is required is a clipboard.<\/p>\n\n<p>IIoT is nothing until good daily practices are in place at the factory.<\/p>\n","pubDate":"Wed, 21 Dec 2016 08:18:00 -0600","link":"https:\/\/chrislarson.me\/30days\/iiot\/2016\/12\/21\/day21.html","guid":"https:\/\/chrislarson.me\/30days\/iiot\/2016\/12\/21\/day21.html","category":["30days","iiot"]},{"title":"What to measure?","description":"<p>Like a writer staring at a blank page, engineers are overwhelmed when starting a new IIoT project. \nWhat is important to measure?<\/p>\n\n<p>How will we measure it?<\/p>\n\n<p>How often will we measure it?<\/p>\n\n<p>How long will we keep the measurements?<\/p>\n\n<p>Where will we keep the measurements?<\/p>\n\n<p>How will we display the measurements?<\/p>\n\n<p>How will we query the measurements?<\/p>\n\n<p>How much disk storage will we need?<\/p>\n\n<p>Who will need access to the measurements?<\/p>\n\n<p>How will we power the measurement devices?<\/p>\n\n<p>How will we network the measurement devices?<\/p>\n\n<p>\u2026<\/p>\n\n<p>\u2026<\/p>\n\n<p>And so many more questions<\/p>\n\n<p>\u2026<\/p>\n","pubDate":"Tue, 20 Dec 2016 08:25:00 -0600","link":"https:\/\/chrislarson.me\/30days\/iiot\/2016\/12\/20\/day20.html","guid":"https:\/\/chrislarson.me\/30days\/iiot\/2016\/12\/20\/day20.html","category":["30days","iiot"]}]}}