by Rob Bugh
Step-by-step Guide to Creating a Groovy Lambda
Serverless technology is becoming more and more popular amongst data centers deployed on top of Amazon Web Services (AWS). One of the serverless components AWS offers is a Lambda. Before Lambda was available to run your code, you would have to provision and manage an EC2 instance. Now, with Lambda, it is as simple as uploading a zip file of your code to AWS--there's no server to manage. You can read more about AWS Lambda here.
AWS documents describe how to create a Lambda in several programming languages: Node.js, Python, and Java. They don't specifically say how to use the Groovy language. However, Groovy is a JVM language and is compatible with Java-- therefore Groovy works as well. This article will show the steps you need to take to build a Groovy-based Lambda.
Let's start by looking at a simple Lambda that reads files from S3, Amazon's distributed object storage service, and do something with it. What the Lambda does with the files is not important--just note that we will set up the Lambda to received events from S3 whenever an object in that bucket is created or updated. The event will identify the object that was created or updated and the Lambda will load the object and do something with it. An object can be a directory or a file. The Lambda will ignore directories and only process files.
Sample Lambda in Groovy
Gradle will be used to build and package the Lambda. If you are familiar with Gradle or Maven, you know that you define how to build your project in a build file. For Gradle, this file is called, build.gradle, and is shown below. Note: the settings are almost identical to those you would use if building a Java-based Lambda, with just the addition of the Groovy runtime dependency.
Groovy Runtime Dependency
This build.gradle is fairly simple. The things to note are the dependencies and the buildZip task. The AWS dependencies can all be found in Maven Central along with the Groovy runtime so they do not have to be downloaded and included directory in your project.
The buildZip task will package your Lambda code along with any dependency together in a zip file called GroovyLambda.zip. This is a requirement of the AWS Lambda. All dependencies must be packaged together along with your code into the zip file. See the AWS documentation for more information. Build your project and then run the buildZip task to create the GroovyLambda.zip file. The file will be created under the build/distributions directory.
Upload your code
You can upload your code via an AWS command line interface or through their web console.
This article describes how to upload and configure your Lambda through the AWS web console:
- Log into the AWS web console and navigate to the Lambda page, click the Create function button, then the Author from scratch button.
- Give your Lambda a name and an appropriate IAM role. Click Create function button.
- On the next page, under the Code entry type selection box, click Upload a .ZIP file. Select your zip file and upload it.
- Under the Runtime selection box choose Java 8. Under the Handler text box add the name of your handler method, e.g., com.reachforce.SimpleLambda::handler. Note the syntax of the handler name is, <package name>::<method name>.
- Go to the Triggers tab and select Add Trigger. On the next screen click on the dotted trigger symbol and select the S3 trigger from the list. On the trigger configuration page, enter the S3 bucket to use and under Event type select Object Created (All).
You have configured S3 to send your Lambda an event every time a file is created or updated in your bucket. The lambda handler method receives the event which identifies the file, loads the file from the S3 bucket and does something with the file.
That's it! There are other settings for configuring the environment, performance tuning, and runtime roles that are not discussed here. They are well documented on the AWS website.
If you already have experience creating Lambda services in AWS using Java and Gradle, then switching to Groovy is trivial. Just include the Groovy runtime dependency along with the AWS Java SDK dependencies in your build.gradle.