{"id":1423,"date":"2018-02-26T11:39:05","date_gmt":"2018-02-26T00:39:05","guid":{"rendered":"http:\/\/ntsblog.homedev.com.au\/?p=1423"},"modified":"2018-02-26T11:39:05","modified_gmt":"2018-02-26T00:39:05","slug":"aws-lambda-canary-node-js","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2018\/02\/26\/aws-lambda-canary-node-js\/","title":{"rendered":"AWS Lambda-Canary Node Js"},"content":{"rendered":"<div id=\"ntsbl-4033121146\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><h2>The Scenario<\/h2>\n<p>I am learning about AWS Lambda functions and doing a small tutorial that works through using the lambda-canary blueprint which uses python 2.7<\/p>\n<h2>The Problem<\/h2>\n<p>I want to do this same canary function in Node.js 6.10, but I could not find out how to do this.<\/p>\n<h2>The solution<\/h2>\n<p>Below is the code sample that I came up with to implement the lambda-canary-nodejs lambda function<\/p>\n<p>[csharp]<br \/>\nvar http = require(&#8216;http&#8217;);<\/p>\n<p>exports.handler = (event, context, callback) =&gt; {<\/p>\n<p>    var site = process.env[&#8216;site&#8217;];<br \/>\n    var expected = process.env[&#8216;expected&#8217;];<\/p>\n<p>    console.log(&#8216;Checking website: &#8216; + site);<\/p>\n<p>    http.get(site, (resp) =&gt; {<br \/>\n        let body = &#8221;;<\/p>\n<p>        \/\/ read the data<br \/>\n        resp.on(&#8216;data&#8217;, (chunk) =&gt; {body += chunk; });<\/p>\n<p>        \/\/ The whole response has been received.<br \/>\n        resp.on(&#8216;end&#8217;, () =&gt; {<br \/>\n            \/\/ validate that the expected value is in the returned stream<br \/>\n            if(body.indexOf(expected) &gt; 0){<br \/>\n                console.log(&#8216;website is up&#8217;)<br \/>\n                callback(null, event[&#8216;time&#8217;]);<br \/>\n            } else {<br \/>\n                console.log(&#8216;website did not respond as expected&#8217;)<br \/>\n                var error = new Error(&#8216;website did not respond as expected&#8217;);<br \/>\n                callback(error);<br \/>\n            }<br \/>\n        });<\/p>\n<p>    }).on(&quot;error&quot;, (err) =&gt; {<br \/>\n        console.log(&quot;Error: &quot; + err.message);<br \/>\n        callback(err);<br \/>\n    });<\/p>\n<p>};<br \/>\n[\/csharp]<\/p>\n<p>Items to note about this code:<\/p>\n<ol>\n<li>process.env[&#8216;key&#8217;] is how you access the environment varialbes\/li>\n<li>environment variables are case sensitive, so the string index must match variable name as defined on the lambda function<\/li>\n<li>callback accepts an error object as the first parameter when it fails<\/li>\n<\/ol>\n<p>So to implement this you would:<\/p>\n<ol>\n<li>Create a new blank function<\/li>\n<li>Past in this code<\/li>\n<li>Setup 2 x environment variables, site &amp; expected<\/li>\n<li>Create a test of type scheduled event<\/li>\n<li>Test the code<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>The Scenario I am learning about AWS Lambda functions and doing a small tutorial that works through using the lambda-canary blueprint which uses python 2.7 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[32,33],"tags":[],"class_list":["post-1423","post","type-post","status-publish","format-standard","hentry","category-aws","category-lambda"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=1423"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1423\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=1423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=1423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=1423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}