Deploy to AWS ElasticBeanstalk

Learn how easy is to deploy a Micronaut App to Elastic Beanstalk.

Authors: Sergio del Amo

Micronaut Version: 1.0.0.RC2

1 Getting Started

In this guide, you will learn how to deploy a Micronaut app to AWS Elastic Beanstalk.

AWS Elastic Beanstalk is an easy-to-use service for deploying and scaling web applications and services developed with Java.

1.1 What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

You will need also an AWS Account.

1.2 Solution

We recommend you to follow the instructions in the next sections and create the app step by step. However, you can go right to the completed example.

or

Then, cd into the complete folder which you will find in the root project of the downloaded/cloned project.

2 Writing the App

Create the microservice:

mn create-app example.micronaut.complete

Micronaut management dependency adds support for monitoring of your application via endpoints: special URIs that return details about the health and state of your application.

Add the management dependency:

build.gradle
    compile "io.micronaut:management"

Once you include the management dependency a /health endpoint is exposed.

Create a test which verifies it:

src/test/java/example/micronaut/HealthTest.java
package example.micronaut;

import io.micronaut.context.ApplicationContext;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.HttpClient;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Map;

public class HealthTest {

    private static EmbeddedServer server;
    private static HttpClient client;

    @BeforeClass
    public static void setupServer() {
        server = ApplicationContext.run(EmbeddedServer.class); (1)
        client = server
                .getApplicationContext()
                .createBean(HttpClient.class, server.getURL()); (2)
    }

    @AfterClass
    public static void stopServer() {
        if (server != null) {
            server.stop();
        }
        if (client != null) {
            client.stop();
        }
    }

    @Test
    public void testHealthRespondsOK()  {
        Map m = client.toBlocking().retrieve(HttpRequest.GET("/health"), Map.class); (3)
        assertNotNull(m);
        assertTrue(m.containsKey("status"));
        assertEquals(m.get("status"), "UP");
    }
}
1 To run the application from a unit test you can use the EmbeddedServer interface
2 Register a RxClient bean in the application context and point it to the embedded server URL. The EmbeddedServer interface provides the URL of the server under test which runs on a random port.
3 Creating HTTP Requests is easy thanks to Micronaut’s fluid API.

3 Deploy to Elastic Beanstalk

Generate a Fat Jar with gradle command.

$ ./gradlew shadowJar
$ du -h build/libs/complete-0.1-all.jar
 11M    build/libs/complete-0.1-all.jar

The next screenshots illustrate the steps necessary to deploy an micronaut app to AWS Elastic Beanstalk:

elasticbeanstalk1
elasticbeanstalk2
elasticbeanstalk3
  • Select Java Platform.

  • Click Upload button

elasticbeanstalk4

Upload your JAR.

elasticbeanstalk5

Before you create the enviroment click "Configure more options". If you forget, you could change this after you created the environment.

By default, Micronaut applications will listen on port 8080. Elastic Beanstalk assumes that the application will listen on port 5000. There are two ways to fix this discrepancy: change the port Elastic Beanstalk is configured to use, or change the port the Micronaut application listens on. For this post, we will change the port the Micronaut application listens on.

The easiest way to do this is to specify the MICRONAUT_SERVER_PORT environment variable in the Elastic Beanstalk environment and set the value to 5000. (The configuration property name is micronaut.server.port, but Micronaut allows you to specify a more environment variable-friendly name).

On the Configuration page in your environment, under Software Configuration, click the settings icon.

elasticbeanstalk6
elasticbeanstalk7

Now you are ready to click Create Environment.

elasticbeanstalk8

After a few minutes you will be able to access the /health endpoint.

elasticbeanstalk9

4 Next Steps

Explore more features with Micronaut Guides.