obniz with AWS Lambda

You can use obniz with serverless services like AWS Lambda.
It’s made easy for event driven one control. This post shows how to use ones with AWS Lambda.

Create node files

First, create folder and install packages.

mkdir obniz_lambda
cd obniz_lambda
npm install obniz

AWS Lambda calls function in “index.js”, so create it.

touch index.js

My folder contents are like this.

Write function in index.js. For example, this code is to display “Hello World” on obniz and change position of servo motor connected pin 0~2.

var Obniz = require("obniz");

exports.handler = function(event, context, callback) {
  var obniz = new Obniz("YOUR OBNIZ_ID_HERE");
  obniz.onconnect = async function () {

    var servo = obniz.wired("ServoMotor", {gnd:0, vcc:1, signal:2});

    obniz.display.print("Hello World");

    await obniz.wait(500)
    callback(null, "success");


Write your obniz id instead of YOUR OBNIZ_ID_HERE .

require(“obniz”) is load obniz module.

exports.handler = function … is event handler for AWS Lambda.
When Some event occured, AWS Lambda calls this function. We set config later.

callback(null, “success”); is callback for AWS Lambda. When my function have finished,We should call.
And before this, we should call obniz.close(); for close WebSocket connection.
If you keep it, AWS Lambda cannot finish and will be timeout.

Upload code and setting AWS Lambda

I compress my code. Because Index.js need existing top in zip file, you shouldn’t compress obniz_lambda folder.

zip -r obniz_lambda_codes.zip index.js node_modules/ package-lock.json

My foler contents arecurrently this.

To upload AWS Lambda, access AWS console site and login.

Access Lambda and select region you like (I select Asia Pacific (Tokyo) ).

Click “Create Function”

Select “Author from scratch” and below fields.

Name : someting you like. I input “obniz_lambda”
Runtime : Node.js 8.10
Role : Choose an existing role
Exisiting role : lambda_basic_execution

Click “Create Function” button.

In Function code section, select code entry type to “Upload a .ZIP file” and upload function package “obniz_lambda_codes.zip” (We made it before).

Click right top Save button.

If your code wil use over 3 sec, change timeout setting in “Basic setting” section.

Let’s test lambda function.

Drop down “select a test event” on left of Test button, and select “Configure test events”.

input Event name and click create button. Other fileds leave as default.

Test needs online obniz, so wired obniz to power supply servo motor and connect Wi-Fi.

Click test button to start test, and will display Execution result as successed.
Also, obniz display “Hello World” about 500ms, change servo motoer position.

if you failed, check your code, for example “YOUR OBNIZ CODE” or timeout setting.

Setting AWS API Gateway

On Designer section, choose “API Gateway” to add lambda trigger on http request.

Under “Configure triggers” section, appear when API Gateway selected, input fields and save.
API name: “LambdaMicroservice” or something you like
Deployment stage : “prod” or something you like
Security : “Open” for access from web without security.

After save, like this.

Click api gateway key like “vbm34885sg” to open API Gateway setting page.
It has been created resouces to access lambda function.(like “obniz_lambda” on this image)

Now, api is not deployed. So select Actions -> Deploy API. Select Deployment stage “prod” and press deploy button.

Access from browsers

You will move to “prod Stage Editor” and can see Invoke URL. It’s base URL for this API Gateway. To access lambda function from api, select Stages -> / -> /obniz_lambda -> GET.

Invoke URL in this page is URL for access api and control obniz.

demo movie


github : https://github.com/9wick/obniz_lambda

Next : With Runkit

You will Get in Few Days

Circuit for Starter “obniz Board” is available on Amazon and other online stores.
You can get it at below

Our products and resellers


Visit our developer’s forum to discuss and discover technologies.



Feel free to contact out support and technical team.

Contact us