Using Java, Amazon Web Services (AWS) and Ant; a function can be created/deployed that performs the role of a scheduled task. The code can be used to make web service calls, run backup tasks, clean a database table, etc.
Source code is discussed, and a walk through goes over the process of registering the function with AWS.
All source code can be found on GitHub.
- Make sure all software prerequisites are downloaded/installed/configured. The user should be able to run “$ java -version” and “$ ant -version” successfully from the command line.
- Clone/download the source code from GitHub, and build using “$ ant”. If successful, the deployment file will be “./dist/HelloWorld.jar”.
- Log in to AWS, and create a new Lambda function. Click ‘Next’ to not use a blueprint.
- Configure a trigger, using a ‘CloudWatch Event – Schedule’.
- Configure the trigger to run once a minute.
- Configure the function with all of the following values. When you get to ‘Create a custom role’, you will be brought to a new screen.
- You’ll want a lambda_basic_execution role. If auto-population doesn’t work, the policy document is also in “./README.md”.
- Change the memory to ‘256’, and make the rest of the screen appear as follows.
- You will then be brought to a review page.
- After clicking ‘Create function’, you should see a confirmation page with a green bar at the top.
- Clicking on the different tabs will allow you to monitor/edit all facets of the function.
- Upon clicking on ‘View logs in CloudWatch’, you should be able to view the printout of the first successful execution.
- Code running every minute:
Using Java, Ant and AWS; we’ve created a scheduled task that runs Java code regularly. Lambda is used for ‘code in the cloud’, and CloudWatch events/logs are used to execute and oversee the function. The function runs once a minute, and prints information to the log.
There is much room for follow-on work including:
- Querying a web service with the code.
- Accessing other parts of the AWS environment, e.g RDS/DynamoDB.
- Configuring the code to run in response to a user generated event.
- Deploying a function with Python and/or Node.js.