Using AWS Lambda and the API Gateway

What is AWS Lamda?

This is a serverless system provided by AWS. Serverless means that you don't have to be aware of the machine running the program, such as CPU and memory. It's a philosophy that allows you to run programs. By using this, you can run Nodejs programs without worrying about the server. It is.

It's especially good for event-driven execution, which makes it easier to support webhooks, etc., and allows you to create a variety of things with minimal cost.

Here is an example of using obniz with AWS Lambda.

Creating a Node file

First, create a node package to run on lambda and install obniz

mkdir obniz_lambda
cd obniz_lambda
npm install obniz

We need "index.js" for lambda, so we'll create that too

touch index.js

Then, the folder structure looks like this.

In index.js, you need to write the obniz code that you want to run.
For example, the following code displays "Hello World" on the display, and drives a servo motor connected to PIN0~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");

Replace YOUR OBNIZ_ID_HERE with your own obnizID

There are several points in the code.

It is loaded in require("obniz").

exports.handler = function … you can use lambda to write Creates a function to be used.
This function is called when some event occurs (e.g. a web hook or a file is created in S3).
We will decide later on when the event is called.

callback(null, "success"); is a callback function of Node. js is an asynchronous language, so it is necessary to call the callback after the process is finished.
You should pay attention to WebSocket. If you don't break the connection, the lambda function will not terminate and will time out, so the
You need to call obniz.close(); properly

Configuring AWS Lambda

The code is uploaded to Lambda as a zip file, so we create a zip file.
This zip file contains the necessary node_modules and index.js is at the top It is necessary.
Be careful not to compress the whole folder by mistake. (index.js comes to a lower level, such as obniz_lambda/index.js, and is used in (I'll be done.)

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

This is the structure of the file.

Access AWS Lambda and log in.

Select the Lambda service and change the region to your preferred location. (In this case, I've chosen Tokyo.)

Press "Create Function".

This time, we are going to build the program from scratch, so we choose "Author from scratch" and fill in the fields.

Name : Whatever … I've changed it to "obniz_lambda".
Runtime : Node.js 8.10
Role : Choose an existing role
Existing role : lambda_basic_execution

When you are done, press the "Create Function" button.

In the Function code section, upload the zipped code.
Set the code entry type to "Upload a .ZIP file" and add "obniz_" to the Upload the "" file.

Don't forget to press the Save button in the upper right corner.
If your code is likely to run for more than 3 seconds, you can extend the timeout period by selecting "Basic setting".

So, let's run some tests.
Select "Configure test events" from the "select a test event" section

When you name the event, create it. You can leave the other fields as they are.

To test it, connect the obniz to the power supply, motor and Wi-Fi.

Press the "test" button to start the test, and "Hello World" is displayed for a moment in obniz. appears and the servo motor has moved.
The screen will show "Execution result : successed.

If this fails, please review your code and settings.
In particular, you should pay attention to whether "YOUR OBNIZ CODE" is changed or not, and whether the timeout is appropriate.

Configuring the AWS API Gateway

Although there are many different lambda events, in this article, we will configure the API Gateway to be accessible from the external web.

Select "API Gateway" in the Designer, and

"Configure triggers" will appear, and you can fill in the form.

  • API name: Whatever … The default of "LambdaMicroservice" will be fine.
  • Deployment stage : Whatever … "prod" defaults are fine.
  • Security :"Open". You can connect from anywhere without security.

This is what it looks like when you save it

Click on the API Gateway name (in the image, "vbm34885sg") to go to the API Gateway configuration page.
However, all the settings are done automatically.

In this state, it is a private API, so we deploy it and make it public.
Press Actions → Deploy API.

When the modal view appears, set the Deployment stage to "prod" and deploy.

Try it from your browser.

When you deploy, you will be automatically moved to the "prod Stage Editor".
The base address of the entire API is written next to the Invoke URL (in Japanese, the URL call).
You can find the URL for lambda in prod → / → /obniz_lambda → GET page

The URL next to the Invoke URL is the address of the API.
If you click on the GET button, the lambda is called, so you can click on it and obniz will start working

Demo and Code

github :