# Use Environment Variables to Customize Microservices

This tutorial describes how to use environment variables to customize the behavior of microservices. Using environment variables as a configuration mechanism allows the same Linux image to run in development and production environments. This is especially useful when managing sensitive information via Kubernetes Secrets.

The entando.json file makes environment variables available to a microservice in a docker-based bundle. There are two options:

  1. Inject the variables using a key-value pair
  2. Inject a reference to an existing Kubernetes Secret

This tutorial will demonstrate both of these options. The microservice will receive one environment value directly as plain text in the pod YAML and the other indirectly through a referenced Secret.

# Prerequisites

# Add Environment Variables to the Microservice

  1. To determine YOUR-BUNDLE-ID, run the following command. Supply the full bundle URL, remembering to update the placeholders with your Docker organization and your bundle name.
ent ecr get-bundle-id docker://registry.hub.docker.com/YOUR-ORG/YOUR-BUNDLE
  1. Create a Secret named YOUR-BUNDLE-ID-my-secret with key-value pair mySecretKey=mySecretValue. Replace YOUR-BUNDLE-ID with the output of the previous step.
ent kubectl create secret generic YOUR-BUNDLE-ID-my-secret --from-literal=mySecretKey=mySecretValue -n entando
  1. Insert the following env section into the microservice in entando.json, remembering to replace YOUR-BUNDLE-ID. By convention, environment variables are all caps and K8s resource names are hyphenated.
"env": [
  { "name": "SIMPLE_VAR",
    "value": "mySimpleValue" 
  },
  { "name": "SECRET_VAR",
    "secretKeyRef": {
        "name": "YOUR-BUNDLE-ID-my-secret", 
        "key": "mySecretKey"
      }
  }
]
  1. Build and deploy the updated bundle

# Verify the Environment Variables

  1. Retrieve your microservice pod name. Use ent kubectl get pods or your cluster management tool to find the name.

    The pod name begins with 'pn', followed by two generated alpha-numeric strings, your docker organization name, microservice name, etc.
    E.g. YOUR-MICROSERVICE-POD-NAME = pn-ccfcefa6-615bc3ba-dockerorg-conference-ms-deploymentvpsgj

  2. Shell into the pod, using the name from above:

ent kubectl exec -it YOUR-MICROSERVICE-POD-NAME -- /bin/bash
  1. Confirm the environment variables are present:
echo SIMPLE_VAR=$SIMPLE_VAR, SECRET_VAR=$SECRET_VAR

Expected output:

SIMPLE_VAR=mySimpleValue, SECRET_VAR=mySecretValue

Congratulations!

You have learned how to use environment variables with microsevices on the Entando Platform!