8.1 Cloud Configuration

Applications built for the Cloud often need to 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 makes 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 in 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 you can have multiple environments active, for example when running in Kubernetes on AWS.

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, it is only 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 overrides the host property in ConsulConfiguration.