I am pleased to share that the AWS Lambda plugin within the AWS Toolkit for Eclipse now facilitates serverless application development for Java. A serverless application, also referred to as a Lambda-based application, consists of functions activated by specific events. In this post, I will illustrate two examples that demonstrate how to efficiently create and deploy a serverless application using the Eclipse IDE.
Installing the AWS Toolkit for Eclipse
To obtain the latest version of the AWS Toolkit for Eclipse, navigate to this page and adhere to the installation instructions located at the top right. It’s essential to install the AWS Toolkit for Eclipse Core, AWS CloudFormation Tool, and AWS Lambda Plugin to fully utilize this feature. The installation wizard will prompt you to select these three components. After completing the installation, review and accept the license agreement, then restart Eclipse.
Creating a Serverless Project
To initiate a serverless project, click the AWS Toolbar and select “New AWS Serverless Project…”. This action opens a wizard. Alternatively, you can create a new serverless project in the conventional manner: Choose File, New, Other, AWS, and then select “AWS Serverless Java Project.” The Toolkit presents two blueprints for your convenience: “article” and “hello-world.”
- article – This straightforward serverless application manages articles through two Lambda functions triggered by API events: GetArticle and PutArticle. These functions handle the storage of articles in the backend service and retrieval of articles for the frontend. Additionally, this blueprint utilizes an Amazon S3 bucket for article content storage and an Amazon DynamoDB table for metadata.
- hello-world – This blueprint comprises a simple standalone Lambda function, HelloWorld, which is not triggered by any events or resources. It accepts a String and outputs it with the prefix “Hello.” If an empty String is provided, the output will be “Hello World.”
You can also create a serverless project using a serverless template by selecting “Select a Serverless template file” and importing the template file. This file is a simplified version of the SAM (AWS Serverless Application Model) file that defines the application resources stack. The following snippet is from the blueprint articles template, defining the Lambda function GetArticle. Unlike a standard SAM file, you won’t need to provide the CodeUri and Runtime properties, and you only need to specify the class name for the Handler property instead of the Fully Qualified Class Name. Importing a template file enables the AWS Toolkit for Eclipse to generate all the requisite Lambda function hooks and the Lambda Proxy Integration models utilized as API event Input and Output for the Lambda functions.
{
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "com.serverless.demo.GetArticle",
"Runtime": "Java",
"CodeUri": "s3://serverless-bucket/get-article.zip",
"Policies": [
"AmazonDynamoDBReadOnlyAccess",
"AmazonS3ReadOnlyAccess"
]
}
}
The startup view after creating the article blueprint project is depicted in the following figure. The AWS Toolkit for Eclipse organizes all the Lambda functions defined in the template into a function package and all necessary models into a model package. For further details, refer to the serverless.template file. As mentioned previously, this is a simplified version of a SAM file, originating from an AWS CloudFormation template. For more information, see this blog post here.
Deploying a Serverless Project
If your serverless project is derived from a blueprint, you can deploy it immediately to AWS. Note that the article blueprint will create an S3 bucket and a DynamoDB table for the Lambda functions. You may customize the resource names in the Parameters property section of the serverless.template file, as shown:
"Parameters" : {
"ArticleBucketName" : {
"Type" : "String",
"Default" : "serverless-blueprint-article-bucket",
"Description" : "Name of S3 bucket used to store the article content.",
"MinLength" : "0"
},
"ArticleTableName" : {
"Type" : "String",
"Default" : "serverless-blueprint-article-table",
"Description" : "Name of DynamoDB table used to store the article metadata.",
"MinLength" : "0"
}
}
To deploy this project to AWS, right-click on the project name in the explorer view, select “Amazon Web Services,” and then “Deploy Serverless Project.” Alternatively, you can right-click the workspace of any Lambda function file, select “AWS Lambda,” and then “Deploy Serverless Project.” A wizard will appear prompting you to choose the S3 bucket, enter the CloudFormation stack name, and select “Finish.” The AWS Toolkit for Eclipse will generate the fat JAR file for the underlying Lambda functions and upload it to your chosen S3 bucket, also converting the serverless.template file in memory into a real SAM file for upload. AWS CloudFormation will use this file to create the stack.
While the AWS CloudFormation stack is being established, a Stack Editor view will display the current status of the stack. This page refreshes automatically every five seconds, but you can manually refresh it by clicking the refresh icon in the top right corner. Once the status reads CREATE_COMPLETE, a link will appear next to the Output label, directing you to the Prod stage endpoint of the API Gateway API created by this serverless project.
Testing a Serverless Project
Once the article project is successfully deployed, you can test the two APIs by accessing the API Prod endpoint through browser tools or command line utilities.
Using the curl command line tool:
$ curl --data "This is an article!" https://s5cvlouqwe.execute-api.us-west-2.amazonaws.com/Prod?id=1
Successfully inserted article 1
$ curl -X GET https://s5cvlouqwe.execute-api.us-west-2.amazonaws.com/Prod?id=1
This is an article!
Using the Simple REST client plugin in Chrome, you can also send a POST request to the endpoint.
We would appreciate your feedback on the workflow involved in developing serverless applications utilizing the AWS Toolkit for Eclipse. Let us know if there are other features you’d like to see in this toolkit. Your input is valuable to us. For further insights, you can refer to this excellent resource.
For insights on serverless applications, check out this authoritative source.
Leave a Reply