This project contains an example of Lambda function using the serialization utilities module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the documentation.
The project contains two RequestHandler
s -
- APIGatewayRequestDeserializationFunction - Uses the serialization library to deserialize an API Gateway request body
- SQSEventDeserializationFunction - Uses the serialization library to deserialize an SQS message body
In both cases, the output of the serialized message will be printed to the function logs. The message format in JSON looks like this:
{
"id":1234,
"name":"product",
"price":42
}
This sample is based on Serverless Application Model (SAM). To deploy it, check out the instructions for getting started with SAM in the examples directory
To test the HTTP endpoint, we can post a product to the test URL:
curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/product/ -H "Content-Type: application/json" -d '{"id": 1234, "name": "product", "price": 42}'
The result will indicate that the handler has successfully deserialized the request body:
Received request for productId: 1234
If we look at the logs using sam logs --tail --stack-name $MY_STACK
, we will see the full deserialized request:
{
...
"level": "INFO",
"loggerName": "org.demo.serialization.APIGatewayRequestDeserializationFunction",
"message": "product=Product{id=1234, name='product', price=42.0}\n",
...
}
For the SQS handler, we have to send a request to our queue. We can either construct the Queue URL (see below), or find it from the SQS section of the AWS console.
aws sqs send-message --queue-url "https://sqs.[REGION].amazonaws.com/[ACCOUNT-ID]/sqs-event-deserialization-queue" --message-body '{"id": 1234, "name": "product", "price": 123}'
Here we can find the message by filtering through the logs for messages that have come back from our SQS handler:
sam logs --tail --stack-name $MY_STACK --filter SQS
{
...
"level": "INFO",
"loggerName": "org.demo.serialization.SQSEventDeserializationFunction",
"message": "products=[Product{id=1234, name='product', price=42.0}]\n",
...
}