1. Introduction
Gradle plugin for setting up mock AWS endpoints during test and development using LocalStack.
This plugin provides a number of helpful tasks for simplifying the creation of AWS resources for use in testing and development, as well as tasks for easily integrating Spring Boot with LocalStack, using simple Gradle DSL.
For detailed information on getting started with this plugin and how to configure specific AWS services, please refer to the documentation below.
This plugin requires Gradle >= 5.2 to work properly. |
1.1. Getting Started
The plugin can be applied with the buildscript
syntax or the plugin DSL.
2. Configuration
The plugin provides an extension with the namespace localstack
. The following properties can be configured:
Property Name | Type | Default Value | Description |
---|---|---|---|
|
|
Working directory of |
The directory where the |
|
|
|
Hostname of the LocalStack Docker container. |
|
|
|
Port number of the LocalStack Edge Service exposed by the Docker container. |
|
|
|
AWS region of the LocalStack environment. |
An example configuration can be seen below:
localstack {
workingDir = file('/localstack')
host = 'localhost'
port = 4566
signingRegion = 'us-east-1'
}
3. LocalStack
The plugin provides a set of tasks for easily starting and stopping the LocalStack environment in your project.
3.1. Tasks
The plugin applies default tasks, which can be executed from the Gradle command line:
Task Name | Type | Usage | Description |
---|---|---|---|
|
Default |
|
Initializes the project with a default LocalStack Docker Compose configuration. |
|
Default |
|
Starts the LocalStack environment and executes any of the AWS services tasks that are found in the project. |
|
Default |
|
Stops the LocalStack environment and preserves the |
|
Default |
|
Stops the LocalStack environment and deletes the |
|
Default |
|
Deletes the |
|
Default |
|
Restarts running LocalStack environment with clean |
The plugin also provides tasks that make working with the mock AWS endpoints in LocalStack easy. Currently, the plugin supports the following AWS services:
For more information on how to configure and use these tasks, see the documentation for each AWS service below.
4. CloudFormation
This plugin provides a set of tasks for working with AWS CloudFormation in your project.
CloudFormation support in LocalStack is incomplete and may not work for your use case. Please consult the LocalStack website for CloudFormation support issues. |
4.1. Tasks
The plugin provides both default and custom tasks.
Task | Task Name | Type | Description |
---|---|---|---|
|
Custom |
Creates a CloudFormation Stack |
|
|
Default |
Deletes a CloudFormation Stack |
|
|
Default |
Lists all CloudFormation Stacks |
4.2. Examples
Example configurations for the CloudFormation tasks. For information on all available task properties please refer to the Javadocs.
5. DynamoDB
This plugin provides a set of tasks for working with AWS DynamoDB in your project.
5.1. Tasks
The plugin provides both default and custom tasks.
Task | Task Name | Type | Description |
---|---|---|---|
|
Custom |
Creates a DynamoDB Table |
|
|
Default |
Deletes a DynamoDB Table |
|
|
Default |
Lists all DynamoDB Tables |
5.2. Table Initializer
The plugin not only supports creating DynamoDB tables, but also populating them with data through the use of a Table Initializer.
5.2.1. Create Initializer
In order to create a Table Initializer, create a class on the build classpath (this is most easily done by creating the class in the Gradle buildSrc
directory)
that implements the following rules:
-
Constructor that takes an
AmazonDynamoDB
client as a single argument. -
Implements a void
run
method with no arguments.
package example.buildsrc.dynamodb;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import org.apache.commons.lang3.StringUtils;
/**
* Loads test products into the localstack dynamodb "catalog.products" table.
*/
public class ProductTableInitializer {
private final DynamoDBMapper mapper;
/**
* Creates an instance of {@link ProductTableInitializer}.
*
* The LocalStack Gradle plugin requires a constructor that accepts the dynamodb client
* as a single argument.
*
* @param dynamoClient
*/
public ProductTableInitializer(AmazonDynamoDB dynamoClient) {
this.mapper = new DynamoDBMapper(dynamoClient);
}
/**
* Initializes the table.
*
* The LocalStack Gradle plugin requires a single void method named "run" that does not
* accept any arguments.
*/
public void run() {
for (int i = 0; i < 10; i++) {
final Product product = new Product();
product.setId(StringUtils.leftPad(Integer.toString(i), 5, "0"));
product.setName(String.format("Widget-%s", i));
product.setDescription(String.format("Description for Widget-%s", i));
product.setActive(true);
product.setCreatedAt(System.currentTimeMillis());
mapper.save(product);
}
}
/**
* Product record to load into dynamo table.
*/
@DynamoDBTable(tableName = "catalog.products")
public static class Product {
@DynamoDBHashKey
private String id;
private String name;
private String description;
private boolean active;
private long createdAt;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public long getCreatedAt() {
return createdAt;
}
public void setCreatedAt(long createdAt) {
this.createdAt = createdAt;
}
}
}
5.2.2. Configure Initializer
Table Initializers, once created, can be configured on a CreateDynamoDbTableTask
as follows:
task setupLocalTable(type: CreateDynamoDbTableTask) {
tableName = 'catalog.products'
keySchema = [
new KeySchemaElement("id", KeyType.HASH)
]
attributeDefinitions = [
new AttributeDefinition("id", ScalarAttributeType.S)
]
initializer = 'example.buildsrc.dynamodb.ProductTableInitializer'
}
5.3. Examples
Example configurations for the DynamoDB tasks. For information on all available task properties please refer to the Javadocs.
6. S3
This plugin provides a set of tasks for working with AWS S3 in your project.
6.1. Tasks
The plugin provides both default and custom tasks.
Task | Task Name | Type | Description |
---|---|---|---|
|
Custom |
Creates S3 Buckets |
|
|
Default |
Deletes S3 Buckets |
|
|
Default |
Lists all S3 Buckets |
|
|
Default |
Purges Objects from S3 Buckets |
6.2. Examples
Example configurations for the S3 tasks. For information on all available task properties please refer to the Javadocs.
7. SQS
This plugin provides a set of tasks for working with AWS SQS in your project.
7.1. Tasks
The plugin provides both default and custom tasks.
Task | Task Name | Type | Description |
---|---|---|---|
|
Custom |
Creates SQS Queues |
|
|
Custom |
Creates an SQS Queue with Attached Deadletter Queue |
|
|
Default |
Lists all SQS Queues |
|
|
Default |
Publishes Messages to SQS |
|
|
Default |
Purges Messages on SQS Queues |
7.2. Examples
Example configurations for the SQS tasks. For information on all available task properties please refer to the Javadocs.
7.2.1. Create Queues
task setupLocalQueue(type: CreateSqsQueuesTask) {
queueNames = [ 'catalog-product-change-notification' ]
queueAttributes = [
VisibilityTimeout: '10'
]
}
7.2.2. Create Queue with DLQ
task setupLocalQueue(type: CreateSqsQueueWithDlqTask) {
queueName = 'catalog-product-change-notification'
}
8. SNS
This plugin provides a set of tasks for working with AWS SNS in your project.
8.1. Tasks
The plugin provides both default and custom tasks.
Task | Task Name | Type | Description |
---|---|---|---|
|
Custom |
Creates SNS Topic |
|
|
Default |
Deletes an SNS Topic |
|
|
Default |
Lists all SNS Topics |
|
|
Default |
Lists all Subscriptions on SNS Topic |
8.2. Examples
Example configurations for the SNS tasks. For information on all available task properties please refer to the Javadocs.
9. Spring Boot
This plugin is designed to work seamlessly with Spring Boot. When the Spring Boot plugin is detected within the project this plugin will automatically add tasks that make developing and debugging your Spring Boot application within LocalStack seamless.
9.1. Tasks
The plugin applies default tasks, which can be executed from the Gradle command line:
Task Name | Type | Usage | Description |
---|---|---|---|
|
Default |
|
Starts LocalStack, configures the defined LocalStack environment, and then starts the Spring Boot application. |
|
Default |
|
Starts LocalStack, configures the defined LocalStack environment, and then starts the Spring Boot application in remote debug mode (waits for a debugger to be attached). |
9.2. Configuration
The plugin provides a nested extension with the namespace springboot
. The following properties can be configured:
Property Name | Type | Default Value | Description |
---|---|---|---|
|
|
|
Enables auto-configuration of the |
|
|
|
Debug port for |
|
|
|
List of Spring Profiles with which to start the application. |
|
|
|
List of JVM args with which to start the application. |
An example configuration can be seen below:
localstack {
// ... LocalStack configuration
springboot {
enabled = true
debugPort = 5055
profiles = [ 'local' ]
jvmArgs = [ '-Dlog4j.configurationFile=log4j2-local.xml' ]
}
}