aws lambda microservice, serverless

Posted on Actualizado enn

lambda allows us to execute  C#, NodeJS, Java8 o Python 2.7, without configure a server or virtual machine.

We are going to create a java class to be executed inside Lambda and we are going to insert a row in dynamodb.

Create a table in dynamodb, BitacoraLectura, primary key SensorID (string).

We create our jar to insert sensorid and temperature, we need to add the lib folder to our jar, then we need to upload it to S3

The lambda function has to be created as follow:

Blueprint: Blank Function
Triggers: none
Name: RegisterLecture
Description: RegisterLecture
RunTime: Java 8
Code entry type: Amazon S3
S3 Link: the ones that correspond
Handler: the ones that correspond package.class
Role: Create a new role from template(s)
Role Name: LambdaAddLecture
Policy templates: empty

Go to roles to modify the one created / services, iam, roles section, select “AmazonDynamoDBFullAccess”

Then click on the test button and send a json like this
{
“sensorId”: “temp001”,
“temp”: “28.30”
}

package lambdaaws;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.HashMap;
import java.util.Map;

public class LambdaAWS implements RequestHandler<RequestClass, String> {

    @Override
    public String handleRequest(RequestClass i, Context cntxt) {
        cntxt.getLogger().log(("Entrada: "+i.sensorID+" "+ i.temperatura + "\n"));

        AmazonDynamoDB db = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_WEST_2).build();
        String tableName = "BitacoraLectura";
        Map<String, AttributeValue> item = new HashMap<>();
        item.put("sensorID", new AttributeValue(i.sensorID));
        item.put("temperatura", new AttributeValue(i.temperatura));
        db.putItem(tableName, item);
        return "Agregado: "+i.sensorID+" "+i.temperatura;

    }
}

 

package lambdaaws;

public class RequestClass {
    String sensorID;
    String temperatura;

    public RequestClass() {
    }

    public String getSensorID() {
        return sensorID;
    }

    public void setSensorID(String sensorID) {
        this.sensorID = sensorID;
    }

    public String getTemperatura() {
        return temperatura;
    }

    public void setTemperatura(String temperatura) {
        this.temperatura = temperatura;
    }

}

Captura

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s