Couchbase Server

AWS IoT Button, Lambda and Couchbase

4 MIN DE LEITURA

Getting Started with Serverless FaaS and AWS Lambda shows how to use a simple Java function to store a JSON document to Couchbase using AWS Lambda. This blog builds upon that and shows how an AWS IoT Button can be used as a trigger for that Lambda function. By end of this blog, you’ll learn:

  • How to configure AWS IoT Button
  • Use IoT Button as trigger for Lambda Function
  • Test IoT button

The overall flow will be:
serverless-iot-couchbaseAWS Iot button click will invoke HelloCouchbaseLambda Lambda function. This function uses the Couchbase Java SDK to create a JSON document in Couchbase.

This blog is also playing catch up with Collecting iBeacon Data with Couchbase and Raspberry Pi IoT Devices by Nic and The CouchCase by Matthew on their summer projects. One last blog will be published in this series. That will show how multiple AWS IoT buttons can be used for some fun.

Let’s get started!

Configure IoT Button

The fastest way to configure IoT button  is using the mobile app for iOS or Android.

 

More details about configuring IoT Button using mobile app. Here are some snapshots from configuring button using the mobile app.

Bring up the app, click on + to start configuring a new button:

Enter button’s serial number:
aws-iot-button-configure-2
Register the button:
aws-iot-button-configure-3
Configure the button with wifi network:
aws-iot-button-configure-4
Upload all the certificates etc:
aws-iot-button-configure-5

After this, the button is configured and ready to use. This blog skipped the part where a template Lambda Function is associated with the button click. If  mobile app cannot be used then the button can be configured manually.

Use IoT Button as Trigger for Lambda Function

A aws lambda create-event-source-mapping CLI allows to create an event source for Lambda function. As of AWS CLI version 1.11.21, only Amazon Kinesis stream or an Amazon DynamoDB stream can be used. But for this blog, we’ll use IoT button as a trigger. And this has to be configured using AWS Lambda Console. IoT Button is only supported in a limited number of regions. For example, it is not supported in the us-west-1 region but us-west-2 region works. The list of regions not supported are greyed out in the following list:
aws-iot-buttons-supported-region
Lambda Function can be triggered by several events. Lambda Function is invoked when any of these events occur. By default, no triggers are associated with a Lambda Function. For our HelloCouchbaseLambda function, these can be seen at  us-west-2.console.aws.amazon.com/lambda/home?region=us-west-2#/functions/HelloCouchbaseLambda?tab=triggers.
lambda-default-triggers
Click on Add trigger to add a new trigger:
lambda-add-trigger-default
Select on the empty square to create a new trigger, and select AWS IoT:
lambda-add-trigger-iot
For the button previously registered, get the serial number from us-west-2.console.aws.amazon.com/iotv2/home?region=us-west-2#/thinghub:

aws-iot-things-hub-1024x416
Specify the serial number of the button in the AWS IoT trigger:
aws-iot-add-trigger-1024x391
Click on Submit to create the trigger:

And this confirms that the trigger has been added.

Test IoT Button

Before testing the button, let’s login to the Couchbase instance and verify the number of JSON documents in the bucket:
aws-iot-button-couchbase-console-default-
This can be verified at https://:8091/index.html#sec=buckets. As expected, no documents exists in the bucket. Press the button once, and refresh the page. It shows that one document is now stored in the bucket. This is verified in the Couchbase Web Console:
 aws-iot-button-couchbase-console-one-document
Click on Documents to see the complete list of documents:
 aws-iot-button-couchbase-one-document-2
Click on the document ID to see more details about the document:
aws-iot-button-couchbase-one-document-details-1024x328
Only timestamp is stored in this JSON document. Now, let’s update HelloCouchbaseLambda code to include request id in the document as well. This can be achieved by adding the following line of code in the Java class:

A new deployment package can be built and uploaded using the following command:

Now clicking the button will update the number of documents. But the updated document will have an additional attribute populated as shown:
aws-iot-button-couchbase-second-document-details-1024x640
How are you going to take AWS IoT button and use it with Lambda and Couchbase? Let us know at Couchbase Forums.

References

Compartilhe este artigo

Autor

Arun Gupta é o vice-presidente de defesa de desenvolvedores (developer advocacy) na Couchbase. Ele construiu e liderou comunidades de desenvolvedores por mais de 10 anos na Sun, Oracle e Red Hat. Ele possui vasta experiência na liderança de equipes multifuncionais para desenvolver e executar estratégias, planejamento e execução de conteúdos, campanhas de marketing e programas. Antes disso, ele liderou equipes de engenharia na Sun e é um membro fundador da equipe do Java EE. Gupta é autor de mais de 2.000 posts em blogs sobre tecnologia. Ele tem ampla experiência em palestras em mais de 40 países sobre uma miríade de tópicos e é um JavaOne Rock Star por três anos consecutivos. Gupta também fundou o capítulo do Devoxx4Kids nos EUA e continua a promover a educação tecnológica entre crianças. Autor de vários livros sobre tecnologia, um corredor ávido, um viajante do mundo, um Java Champion, líder de JUG, membro do NetBeans Dream Team e um Docker Captain, ele é facilmente acessível em @arungupta.

Uma resposta

  1. Avatar de Pruthvidumpa

    Hello… I have one question, I want to know.. is it possible to register IoT with out having aws logins is it possible? with out access i want to register the iot and give to cutomers.. so if they have it they can just useit.. instead of adding trigger to lambda function.. any suggestions.

Deixe um comentário

Pronto para começar com o Couchbase Capella?

Começar a construir

Confira nosso portal para desenvolvedores para explorar o NoSQL, navegar por recursos e começar com tutoriais.

Use o Capella free

Coloque a mão na massa com o Couchbase em apenas alguns cliques. O Capella DBaaS é a maneira mais fácil e rápida de começar.

Entre em contato

Quer saber mais sobre as ofertas do Couchbase? Deixe-nos ajudar.