AWS SDK Plugins for Grails 3

Benoit Hediard
Stories by Agorapulse
5 min readApr 20, 2016

We’ve just released (in beta) several AWS SDK plugins for Grails: S3, SES, SNS, SQS, DynamoDB and Kinesis!

The aim is to to get you started quickly by providing friendly lightweight utility Grails service wrappers, around the official AWS SDK for Java (which is great but very “java-esque”).

AWS SDK plugin for Grails 2

Launched in 2012 for Grails 2, the initial AWS SDK Grails Plugin was based on the full AWS SDK for Java.

AWS has an amazing pace of innovation, but it has some downsides: as of today the entire java SDK pulls around 34Mo of dependencies for 64 services! Not very “microservices friendly”…

That’s why AWS switched in 2014 to a modular project structure that allows you to selectively pick up which components of the SDK you want.

AWS SDK plugins for Grails 3

The migration from Grails2 to Grails3 was a good opportunity to split the initial plugin into several ones and benefit from AWS java SDK modular approach.

For this initial release, there are 5 services supported:

Some are pretty basic (S3, SES, SNS, SQS), other are more advanced (DynamoDB, Kinesis) and include a lot of code we’ve been using for the last past years while running Agorapulse on AWS.

Amazon S3

Amazon Simple Storage Service (Amazon S3) provides developers and IT teams with secure, durable, highly-scalable cloud storage. S3 is easy to use object storage, with a simple web service interface to store and retrieve any amount of data from anywhere on the web.

// Example to store a file
File file = new File('/Users/ben/Desktop/photo.jpg')
amazonS3Service.storeFile('asset/foo/someKey.jpg', file)
// Example to store an uploaded file
MultipartFile multipartFile = request.getFile('file')
amazonS3Service.storeMultipartFile(
'asset/foo/' + multipartFile.originalFilename,
multipartFile
)

See AWS SDK S3 plugin README for more info.

Amazon SES

Amazon Simple Email Service (Amazon SES) is a cost-effective email service built on the reliable and scalable infrastructure that Amazon.com developed to serve its own customer base.

// Example to send an HTML email
String body = '''
<html>
<body>
The email content
</body>
</html>
'''
amazonSESService.send(
'recipient@foo.com',
'Some subject',
htmlBody
)

The plugin also allows you to send email based on GSP templates.
See AWS SDK SES plugin README for more info.

Amazon SNS

Amazon Simple Notification Service (SNS) is a push messaging service that makes it simple & cost-effective to push to mobile devices & distributed services.

// Example to register an ios device
endpointArn = amazonSNSService.registerDevice(
'ios', // or 'android'
deviceToken,
[data: 'some custom user data'].toString()
)
// Example to send a message to an ios device
amazonSNSService.sendIosAppNotification(
endpointArn,
[
alert: 'Some message',
data: '{"foo": "some bar"}',
badge: 9,
sound: 'default'
]
)

See AWS SDK SNS plugin README for more info.

Amazon SQS

Amazon Simple Queue Service (SQS) is a fast, reliable, scalable, fully managed message queuing service. SQS makes it simple and cost-effective to decouple the components of a cloud application.

// Example to send a message
amazonSQSService.sendMessage(queueName, messageBody)
// Example to consume messages
amazonSQSService.receiveMessages(queueName, 10).each { message ->
String body = message.body
// Put your business logic here
amazonSQSService.deleteMessage(queueName, message.receiptHandle)
}

To consume messages, you can use Quartz Grails Plugin and create worker jobs that will run periodically.
See AWS SDK SQS plugin README for more info.

Amazon DynamoDB

Amazon DynamoDB is a fast and flexible NoSQL database service for all applications that need consistent, single-digit millisecond latency at any scale. It is a fully managed cloud database and supports both document and key-value store models.

The plugin is based on the official java DynamoDBMapper to model data.

Just create simple groovy beans in src/main/groovy with specific DynamoDB java annotations (no GORM or JDBC here).

@DynamoDBTable(tableName="FooItem")
class FooItem {

@DynamoDBHashKey
Long accountId // Hash key
@DynamoDBRangeKey
@DynamoDBAutoGeneratedKey

String id // Range key, with an automatically generated ID

@DynamoDBAttribute
int count = 0
@DynamoDBAttribute
@DynamoDBIndexRangeKey(localSecondaryIndexName="creationDate")

Date creationDate
@DynamoDBAttribute
String message

}

Once you have modeled your data, create one DB Grails service per table by extending AbstractDBService.

class FooItemDBService extends AbstractDBService<FooItem> {

FooItemDBService() {
super(FooItem)
}

}

Then, use the DB service which provides plenty of methods to manipulate your data.

// Example to save an item
foo = new FooItem(
accountId: hashKey,
creationDate: new Date(),
message: 'Some message...',
title: 'Some title'
)
fooItemDBService.save(foo)
// Example to get an item
foo = fooItemDBService.get(hashKey, rangeKey)

See AWS SDK DynamoDB plugin README for more info.

Amazon Kinesis

Amazon Kinesis is a platform for streaming data on AWS, offering powerful services to make it easy to load and analyze streaming data, and also providing the ability for you to build custom streaming data applications for specialized needs.

// Example to record an event
event = new MyEvent(
accountId: 123456789,
someDate: new Date() + 7,
foo: 'foo'
)

amazonKinesisService.putEvent('MyStream', event)

The plugin allows you to create multiple clients, based on the official java Kinesis Client Library, to consume your stream evens.

class MyStreamEventService extends AbstractEventService {

def handleEvent(MyEvent event) {
log.debug "Handling event ${event}"
// Put you consumer business logic here
}

}

See AWS SDK Kinesis plugin README for more info.

AWS SDK plugins are all fully open source, hosted on Github and currently in beta.
So feedbacks and pull requests are welcome!

Note: we’re hiring! Are you kick-ass fullstack or front-end dev that want to work on Angular 2, Java or Groovy? You must contact me to join our dream team in Paris!

If you liked this article, please hit the ❤ button to recommend it. This will make it easier for other Medium users to discover this.

--

--

Benoit Hediard
Stories by Agorapulse

GenAI Enthusiast | Ex-CTO & Co-Founder @Agorapulse | Angel Investor