Search in sources :

Example 1 with SQSMessage

use of com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage in project aws-doc-sdk-examples by awsdocs.

the class SQSEventDeserializer method deserialize.

@Override
public SQSEvent deserialize(JsonElement eventJson, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    SQSEvent event = new SQSEvent();
    logger.info("DESERIALIZING TEST EVENT");
    logger.info("EVENT JSON: " + eventJson.toString());
    // Records key is capitalized in test event, but lowercase in type
    JsonArray recordsArray = eventJson.getAsJsonObject().get("Records").getAsJsonArray();
    ArrayList<SQSMessage> records = gson.fromJson(recordsArray, sqsMessageArray);
    event.setRecords(records);
    return event;
}
Also used : JsonArray(com.google.gson.JsonArray) SQSEvent(com.amazonaws.services.lambda.runtime.events.SQSEvent) SQSMessage(com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage)

Example 2 with SQSMessage

use of com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage in project aws-doc-sdk-examples by awsdocs.

the class Handler method handleRequest.

@Override
public String handleRequest(SQSEvent event, Context context) {
    String response = new String();
    // call Lambda API
    logger.info("Getting account settings");
    CompletableFuture<GetAccountSettingsResponse> accountSettings = lambdaClient.getAccountSettings(GetAccountSettingsRequest.builder().build());
    // log execution details
    logger.info("ENVIRONMENT VARIABLES: {}", gson.toJson(System.getenv()));
    logger.info("CONTEXT: {}", gson.toJson(context));
    logger.info("EVENT: {}", gson.toJson(event));
    // process event
    for (SQSMessage msg : event.getRecords()) {
        logger.info(msg.getBody());
    }
    // process Lambda API response
    try {
        GetAccountSettingsResponse settings = accountSettings.get();
        response = gson.toJson(settings.accountUsage());
        logger.info("Account usage: {}", response);
    } catch (Exception e) {
        e.getStackTrace();
    }
    return response;
}
Also used : GetAccountSettingsResponse(software.amazon.awssdk.services.lambda.model.GetAccountSettingsResponse) SQSMessage(com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage) ServiceException(software.amazon.awssdk.services.lambda.model.ServiceException)

Aggregations

SQSMessage (com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage)2 SQSEvent (com.amazonaws.services.lambda.runtime.events.SQSEvent)1 JsonArray (com.google.gson.JsonArray)1 GetAccountSettingsResponse (software.amazon.awssdk.services.lambda.model.GetAccountSettingsResponse)1 ServiceException (software.amazon.awssdk.services.lambda.model.ServiceException)1