8.1 Cloud Configuration

Applications that are built for the Cloud often need adapt to running in a Cloud environment, read and share configuration in a distributed manner and externalize configuration to the environment where necessary.

Micronaut’s Environment concept is by default Cloud platform aware and will make a best effort to detect the underlying active environment.

You can then use the Requires annotation to conditionally load bean definitions.

The following table summarizes the constants provided by the Environment interface and provides an example:

Table 1. Micronaut Environment Detection
ConstantDescriptionRequires ExampleEnvironment name

ANDROID

The application is running as an Android application

@Requires(env = Environment.ANDROID)

android

TEST

The application is running within a JUnit or Spock test

@Requires(env = Environment.TEST)

test

CLOUD

The application is running in a Cloud environment (present for all other cloud platform types)

@Requires(env = Environment.CLOUD)

cloud

AMAZON_EC2

Running on Amazon EC2

@Requires(env = Environment.AMAZON_EC2)

ec2

GOOGLE_COMPUTE

Running on Google Compute

@Requires(env = Environment.GOOGLE_COMPUTE)

gcp

KUBERNETES

Running on Kubernetes

@Requires(env = Environment.KUBERNETES)

k8s

HEROKU

Running on Heroku

@Requires(env = Environment.HEROKU)

heroku

CLOUD_FOUNDRY

Running on Cloud Foundry

@Requires(env = Environment.CLOUD_FOUNDRY)

pcf

AZURE

Running on Microsoft Azure

@Requires(env = Environment.AZURE)

azure

IBM

Running on IBM Cloud

@Requires(env = Environment.IBM)

ibm

DIGITAL_OCEAN

Running on Digital Ocean

@Requires(env = Environment.DIGITAL_OCEAN)

digitalocean

ORACLE_CLOUD

Running on Oracle Cloud

@Requires(env = Environment.ORACLE_CLOUD)

oraclecloud

Note that it may be the case that you have multiple active environment names since you may run Kubernetes on AWS for example.

In addition, using the value of the constants defined in the table above you can create environment specific configuration files. For example if you create a src/main/resources/application-gcp.yml file then that configuration will only be loaded when running on Google Compute.

Any configuration property in the Environment can also be set via an environment variable. For example, setting the CONSUL_CLIENT_HOST environment variable will override the host property in ConsulConfiguration.