Deploy a Micronaut app to AWS Elastic Beanstalk

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

Authors: Sergio del Amo

Micronaut Version: 2.5.0

1. Getting Started

In this guide we are going to create a Micronaut app written in Groovy.

2. 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

3. Solution

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

4. Writing the App

Create an app using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide --build=maven --lang=groovy
If you don’t specify the --build argument, Gradle is used as a build tool.
If you don’t specify the --lang argument, Java is used as a language.

The previous command creates a Micronaut app with the default package example.micronaut in a folder named micronautguide.

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:

pom.xml
<dependency>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-management</artifactId>
    <scope>compile</scope>
</dependency>

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

Create a test which verifies it:

src/test/groovy/example/micronaut/HealthSpec.groovy
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import javax.inject.Inject
import spock.lang.Specification

@MicronautTest (1)
class HealthSpec extends Specification {

    @Inject
    @Client("/")
    HttpClient client (2)

    void "/health responds 200"()  {
        when:
        Map m = client.toBlocking().retrieve(HttpRequest.GET("/health"), Map) (3)

        then:
        noExceptionThrown()
        m
        m.containsKey("status")
        m["status"] == "UP"
    }
}
1 Annotate the class with @MicronatTest to let Micronaut starts the embedded server and inject the beans. More info: https://micronaut-projects.github.io/micronaut-test/latest/guide/index.html.
2 Inject the HttpClient bean in the application context.
3 Creating HTTP Requests is easy thanks to Micronaut’s fluid API.

5. Testing the Application

To run the tests:

$ ./mvnw test

6. Deploy to Elastic Beanstalk

Create an executable jar including all dependencies:

$ ./mvnw package

The next screenshots illustrate the steps necessary to deploy a Micronaut app to AWS Elastic Beanstalk:

elasticbeanstalk1
elasticbeanstalk2
elasticbeanstalk3
  • Select Java Platform.

  • Click Upload button

elasticbeanstalk4

Upload your JAR.

elasticbeanstalk5

Before you create the environment 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

7. Next steps

Explore more features with Micronaut Guides.

Learn more about AWS Elastic Beanstalk

8. Help with Micronaut

Object Computing, Inc. (OCI) sponsored the creation of this Guide. A variety of consulting and support services are available.