title: elasticsearch can't resolve environment variables in elasticsearch.yml
body: <p>I have the following two settings in my <code>elasticsearch.yml</code> file. They are the only ones that pull from environment variables.</p>
  
  <pre><code>cloud.aws.access_key: ${AWS_ACCESS_KEY_ID}
  cloud.aws.secret_key: ${AWS_SECRET_KEY}
  </code></pre>
  
  <p>When I restart elasticsearch to load these from the environment, I get an error that it can't resolve them. I've tested it and it will not resolve either, so this error applies to both (it just fails on the bottom one first)</p>
  
  <pre><code>- IllegalArgumentException[Could not resolve placeholder 'AWS_SECRET_KEY']
    java.lang.IllegalArgumentException: Could not resolve placeholder 'AWS_SECRET_KEY'
    at org.elasticsearch.common.property.PropertyPlaceholder.parseStringValue(PropertyPlaceholder.java:124)
    at org.elasticsearch.common.property.PropertyPlaceholder.replacePlaceholders(PropertyPlaceholder.java:81)
    at org.elasticsearch.common.settings.ImmutableSettings$Builder.replacePropertyPlaceholders(ImmutableSettings.java:1060)
    at org.elasticsearch.node.internal.InternalSettingsPreparer.prepareSettings(InternalSettingsPreparer.java:101)
    at org.elasticsearch.bootstrap.Bootstrap.initialSettings(Bootstrap.java:106)
    at org.elasticsearch.bootstrap.Bootstrap.main(Bootstrap.java:177)
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:32)
  </code></pre>
  
  <p>I did some investigating through elasticsearch's code repository on github and discovered this bit of code that pulls from the environment variables.</p>
  
  <p><a href="https://github.com/elasticsearch/elasticsearch/blob/04b412b5972a08a79989d82cc9d17842fe3c3e5a/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java#L1033-1047" rel="nofollow noreferrer"><code>ImmutableSettings.java#resolvePlaceholder</code> from elasticsearch@github</a></p>
  
  <p>Namely. the lines inside that function that <em>should</em> be pulling from the environment variables are these one:</p>
  
  <p><a href="https://github.com/elasticsearch/elasticsearch/blob/04b412b5972a08a79989d82cc9d17842fe3c3e5a/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java#L1042-1045" rel="nofollow noreferrer">Code from <code>resolvePlaceholder</code> that pulls out environment variables</a></p>
  
  <p>However, after <code>resolvePlaceholder</code> is run from inside function <a href="https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/property/PropertyPlaceholder.java#L107-125" rel="nofollow noreferrer"><code>PropertyPlaceholder#parseStringValue</code></a>, the <code>System.getenv</code> call must be returning null as that is the only way for that error to be thrown.</p>
  
  <p>I wrote a simple test program that is essentially a copy of <code>ImmutableSettings.java#resolvePlaceholder</code> to test that  <code>System.getenv</code> was pulling out the environment variables correctly on my system. This in fact returns the values I expect.</p>
  
  <pre><code>public class Cool {
    public static void main(String[] args) {
      System.out.println(resolvePlaceholder(args[0]));
    }
    public static String resolvePlaceholder(String placeholderName) {
      if (placeholderName.startsWith("env.")) {
        // explicit env var prefix
        System.out.println("1: placeholderName.startsWith(\"env.\")");
        return System.getenv(placeholderName.substring("env.".length()));
      }
      String value = System.getProperty(placeholderName);
      if (value != null) {
        System.out.println("2: System.getProperty");
        return value;
      }
      value = System.getenv(placeholderName);
      if (value != null) {
        System.out.println("3: System.getenv");
        return value;
      }
      return "Map should've had it";
    }
  }
  </code></pre>
  
  <p>When run, this is the output, showing we are getting the set environment variables (keys hidden for obvious reasons):</p>
  
  <pre><code>[ec2-user@ip-172-31-34-195 ~]$ java Cool AWS_SECRET_KEY
  3: System.getenv
  XXXXXXXXXXXXXXXXXX
  [ec2-user@ip-172-31-34-195 ~]$ java Cool AWS_ACCESS_KEY_ID
  3: System.getenv
  XXXXXXXXXXXXXXXXXX
  </code></pre>
  
  <p>What is it about elasticsearch that isn't able to parse my environment variables from <code>elasticsearch.yml</code>? I've done quite a bit of digging at this point but I'm sure there is a simple solution around the corner. Any help would be very much appreciated.</p>