Contents


Overview

Skill Level: Beginner

This recipe provides a step-by-step guide to running a node-red app on AWS EBS and creating a flow which decorates incoming SQS messages with IBM Weather data based on a supplied location and then forwards them on another SQS queue.

Ingredients

1: AWS account with Elastic Beanstalk, SQS and S3 enabled

2: EB command line downloaded and installed on your local computer (see http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/GettingStarted.html)

3: AWS credentials created and saved in a local file (~/.aws/config or Usersusername.awsconfig) as below

[profile eb-cli]
aws_access_key_id = key id
aws_secret_access_key = access key

4: IBM Bluemix account

Step-by-step

  1. create a new EB environment

    1: create a new directory (e.g. demoapp)

    2: cd to that directory

    3: run eb init to create a new elastic beanstalk project. select preferred region and use node.js as the plaform

    s1

     

    4: Login to the AWS Console on your browser, select Identity and Access Management (IAM) and add the AmazonS3FullAccess policy to the aws-elasticbeanstalk-ec2-role. Note: this gives full access from EBS to S3 and you may wish to tailor this policy to meet your own security needs.

    role after adding policy

  2. create node-red environment

    1: Create a package.json file with the following content

    {
         “name”: “demoapp”,
         “version”: “1.0.0”,
         “description”: “node red demo app”,
         “main”: “”,
         “scripts”: {
             “start”: “./node_modules/.bin/node-red -s ./settings.js”
         },
         “engines”: {
             “node”: “0.12.*”
         },
         “dependencies”: {
             “node-red”: “0.14.x”,
             “serialport”: “2.1.x”,
             “aws-sdk”: “2.4.x”,
             “node-red-contrib-aws-sqs”: “0.0.x”,
             “node-red-contrib-storage-s3”: “0.0.x”,
             “when”: “3.7.x”
         },
         “author”: “”,
         “license”: “ISC”
    }

    2: Copy the default node-red settings.js file at this link and store in the demoapp directory

    3: Edit the settings.js file to add the following entries to module.exports :

    awsRegion: 'eu-west-1',
    awsS3Appname: 'demoapp',
    storageModule: require('node-red-contrib-storage-s3'),

     

    4: at the command prompt make sure you are in the demoapp directory and run the command “eb create” you may wish to specify a more unique  application name. This will take a long time to run but eventually will return successfully.

     

    eb create output

     

    node red is now running on aws but there is still some configuration to do.

  3. configuring node-red access

    Node red is now accessible directly from the web url of the application. However this is insecure and does not work very well for logging. Instead we will configure direct access to the administration port of node-red on the ec2 instance it is using.

    1: In the AWS Console, select EC2, then select security groups and edit inbound settings for the security group associated with the EBS instance. Add a new rule to allow all traffic from your own browser's IP address. setting inbound rules

    2: save the setting

    3: select the EC2 instance which is running the node-red application. copy its IP address

    4: Enter the IP address in the browser with a port of 8081. This should provide direct access to the node-red adminstration console.

    note: the public IP address also provides access to the node-red application and it would be good practice to remove that access at the same time  i.e. the HTTP rule for port 80.

     

  4. accessing node-red

    You can now access node red on aws. the example below shows a simple flow to trigger a debug message.

    If you look at the S3 console you can see the node-red configuration saved in an s3 bucket.

    s5

  5. creating a simple node-red flow for SQS messaging

    1: access the amazon SQS service and create two queues – weather-request-q and weather-response-q

    s6

     

    2: return to the node-red adminstration screen and create a simple queue filter flow. create an sqs in node with a queue url of the weather-request-url and an sqs output node with  a queue url of the sqs-response-q. Ensure message payload only is sent on the output q.

    s7

     

    3: add another flow (flow 2). This has a trigger node which sends a message to a “change” node and then on to an sqs out node configured with the url of the weather-request-q. an sqs in node is configured with the url of the weather-response-q. the change node sets the message payload to JSON with a value as follows: { “location”: { “lat”: 45.43, “lon” : -75.68}}. You may enter your own location coordinates if you wish.

    s8

     

    4: press the deploy button and the new node-red configuration should be deployed on node-red. now trigger a message in flow 2. you should see it logged on entry in flow 1 and then a new debug message logged when the response is received in flow 2. we now have a simple pass-thru node red flow to handle SQS queues and a means of testing the flow.s13

    6: finally switch to flow 2 and trigger a message. you should see a delay and then a response received with weather data:

     

  6. create weather service on bluemix

    1: Log on to IBM Bluemix

    2: In the console select “data and analytics” and then add services

    3: select “Weather Company Data for IBM Bluemix” and create a new service

    4: click on the service and then view credentials – copy the URL

    s9

  7. create node-red flow to decorate SQS messages with weather data

    1:switch ack to the node-red admin screen and select flow 1

    2: add a weather request node. Use a url of   https://XXXXX/api/weather/v1/geocode/{{{lat}}}/{{{lon}}}/forecast/daily/10day.json?units=m&language=en where XXXX is the url copied from the bluemix weather servicein the previous step. the {lat} and {lon} values are parameters to be taken from received messages.

    s10

     

    3: add a change node. give it a name of “setup weather request” and configure the following rules

    Set mgs.headers to JSON {“Content-Type”: “application/json;charset=utf-8”, “Accept”: “application/json”}

    set msg.lat to msg.payload.location.lat

    set msg.lon to payload.location.lon

    set msg._oldPayload to msg.payload

    s11

    4: create a function node with name handle weather result and add the following javascript code to create a simple summary of the forecasted weather together with the original payload of the request:

    var result = msg.payload ;
    var i = 0 ;
    var summaryArr = [] ;
    for (i = 0 ; i < result.forecasts.length;i++) {
    var elem = {} ;
    var forecastDate = new Date(result.forecasts[i].fcst_valid * 1000);
    elem.validOn = forecastDate.toString() ;
    elem.max_temp = result.forecasts[i].max_temp ;
    elem.min_temp = result.forecasts[i].min_temp ;
    elem.precip = result.forecasts[i].qpf ;
    if (result.forecasts[i].day && result.forecasts[i].day.wspd > result.forecasts[i].night.wspd) {
    elem.wind_speed = result.forecasts[i].day.wspd ;
    }
    else {
    elem.wind_speed = result.forecasts[i].night.wspd;
    }
    summaryArr.push(elem);
    }
    if (msg._oldPayload) {
    msg.payload = msg._oldPayload ;
    msg.payload.weather = summaryArr ;
    }
    else {
    msg.payload = { weather: summaryArr } ;
    }

    return msg;

    s12

    5: link the nodes together to provide a full weather decorator flow as show below. then deploy:

    s13

     

    6: switch to flow 2 and trigger a message. You should see in the debug panel the message received by the flow followed some time later by the respone with weather data

    s14

     

Join The Discussion