Deploying The Java Chart Generator To Aws Lambda On Schedule
This blog post provides a minimal, step-by-step procedure for deploying the ChartGeneratorLambdaFunction Java JAR as an AWS Lambda function and scheduling it to run every 5 minutes.
Every 5 minutes is enough to generate up-to-date chart, although data points are uploaded every minute. If AWS cost is not a question, it can be every minute, or the lambda function can be run by each data point upload to DynamoDB.
The system design idea is described in my previous post - Migrating To Serverless: Raspberry Pi To Dynamodb Plus Lambda Function For Chart Generation
1. Build the Deployment Package
Compile the “fat JAR” containing all dependencies:
mvn clean package
Result: target/ChartGeneratorLambdaFunction-1.0-SNAPSHOT.jar
2. Create the Lambda Function
- Open the AWS Lambda Console.
- Click Create function > Author from scratch.
- Function name: ChartGenerator (or your preferred name).
- Runtime: Java 21.
- (Optional) Architecture: Turn on
ARM64 architecture.
3. Upload to S3 and Configure Lambda
AWS suggests to use S3 for files larger than 10MB. Because the Chart Generator JAR is 17MB, it is best to upload via S3:
- Upload to S3:
- Go to your S3 bucket (e.g.,
chart-generator). - Upload
target/ChartGeneratorLambdaFunction-1.0-SNAPSHOT.jar. - Copy the S3 URI (e.g.,
s3://chart-generator/ChartGeneratorLambdaFunction-1.0-SNAPSHOT.jar).
- Go to your S3 bucket (e.g.,
- Configure Lambda:
- In the Code tab, click Update > Update from a file in Amazon S3.
- Paste the S3 URI you copied.
- In Runtime settings, click Edit and set the Handler to:
com.nobudev7.ChartGeneratorHandler::handleRequest
- Configure Timezone
The current set up is assuming that the data point is using local time zone for the data point. To make the JAR runs on the same time zone as RaspberryPi (although, this is a Sump Pump application constraint), set the Java time zone for the lambda function.
- Open Configuration tab.
- Navigate to Environment variables from the left panel.
- Click Edit to add an environment variable.
- Set JAVA_TOOL_OPTIONS for Key, and set
-Duser.timezone=<TIME_ZONE_STRING>for the value. For example,-Duser.timezone=America/New_York. - Click Save.
4. Configure IAM Permissions
The Lambda function must be authorized to read from DynamoDB and write to S3. Follow these steps to attach the necessary permissions:
- Navigate to Permissions: In the Lambda function console, click on the Configuration tab and select Permissions from the left-hand menu.
- Access the Role: Click on the link under Role name (e.g.,
ChartGenerator-role-xxxx). This will open the IAM Console in a new tab. - Create Inline Policy:
- In the IAM Console, click Add permissions on the right side and select Create inline policy.
- Switch to the JSON editor tab.
- Apply the Policy: Paste the following JSON block, replacing
<REGION>,<ACCOUNT_ID>, and<BUCKET_NAME>with your actual values:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DynamoDBRead",
"Effect": "Allow",
"Action": [
"dynamodb:Query"
],
"Resource": "arn:aws:dynamodb:us-east-1:<YOUR_ACCOUNT_ID>:table/Sump_Water_Level"
},
{
"Sid": "LambdaCodeRead",
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::sump-chart-generator/*"
},
{
"Sid": "ChartOutputWrite",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject"
],
"Resource": "arn:aws:s3:::sump-water-level/output/*"
}
]
}
5. Adjust Resources
JFreeChart requires sufficient memory and time for image processing:
- Memory: 512 MB to 384 MB.
- Timeout: 30 seconds.
6. Schedule Execution
- Click Add trigger > EventBridge (CloudWatch Events).
- Select Create a new rule.
- Rule type: Schedule expression.
- Schedule expression:
rate(5 minutes). - Click Add.
7. Verify and Monitor Execution
To confirm your Lambda is running every 5 minutes, you can check the AWS CloudWatch Logs:
- Monitor Tab: In the Lambda console, click the Monitor tab at the top.
- View CloudWatch Logs: Click the View CloudWatch logs button. This opens the CloudWatch console in a new tab.
- Log Streams: You will see a list of Log streams. Each time the Lambda container starts or runs, it logs to these streams.
- Check Timestamps: Open the latest log stream. You should see entries like
START RequestId: ...andEND RequestId: ....- Verify that the Event time for these entries occurs every 5 minutes.
- Look for the output from the Java code (e.g.,
Processing data for date: ...) to ensure the logic is executing successfully.
- Troubleshooting: If you don’t see logs or see
ERRORentries, check the logs for specific Java exceptions (likeAccessDeniedExceptionif IAM permissions are wrong).