BookshelfFunction is an AWS Lambda function written in Java 11 that reads a list of books from the Bookshelf DynamoDB table. The function is designed to be used in conjunction with other services, such as an API Gateway, but during development and testing, the function can be executed from the AWS Management Console using the "Test" button. The function queries the Bookshelf table in DynamoDB for all books by a specified author, and returns the list of books as a JSON object. This Lambda function is a key component of the Bookshelf application, allowing users to view and search for books in the Bookshelf table.
The deployment of this Lambda function is done with AWS Serverless Application Model (SAM). SAM is a framework for building serverless applications using AWS CloudFormation. SAM extends CloudFormation to provide a simplified way of defining the Amazon API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables needed by your serverless application.
To set up a Java development environment with Java 11, you'll need to have the following prerequisites installed:
- Java SE Development Kit 11 or later: https://www.oracle.com/java/technologies/javase-jdk11-downloads.html
- Apache Maven: https://maven.apache.org/install.html
- Docker: https://www.docker.com/get-started -
Optional
Make sure to add the JDK and Maven to your system path.
You'll also need to install the AWS SAM CLI, which is a command line interface for building and deploying serverless applications using SAM. Here is the link to the AWS SAM CLI installation guide: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html
Once you have the AWS SAM CLI installed, you can use it to create, build, and deploy your serverless application using the SAM template and your Lambda function code.
To build the Lambda with SAM, follow these steps:
- Open a command prompt or terminal.
- Change the directory to the
lambdafolder. - Run the following command to build the Lambda:
sam build
This will use the template.yaml file to build the Lambda.
To deploy the Lambda with SAM, follow these steps:
- After the build process completes, run the following command to deploy the Lambda:
sam deploy --guided
This will use the template.yaml file to deploy the Lambda. You'll be prompted for information such as the AWS region and the S3 bucket to store the deployment package.
You can then use the Lambda function to interact with the Bookshelf table in DynamoDB. The source code for the Lambda function is located in the lambda/bookshelf/BookshelfFunction/src/main/java/bookshelf folder.
To test an AWS Lambda function in the management console, you can use the Test button in the Lambda function's detail page. This will allow you to create a test event and execute the function with that event. In our case it does not matter what is the content of the test event, we just need to press the Test button which triggers our lambda execution, contacting the Bookshelf DynamoDB table and showing the list of books on the Lambda console.
To read more about SAM command check the README.md
More about SAM and Lambda will come in Lambda, API Gateway and Cognito built together