title: Access Elastic Beanstalk environment properties in NGINX configs running on AWS Linux 2
body: <p>I had this working before on AWS Linux AMI but no luck with AWS Linux 2.</p>
  <p>I need to access my environment properties from the Nginx configuration file during the EB application deployment. It's a Single instance Node Server.</p>
  <p>I did it like this with the AWS Linux AMI and it worked without a problem:</p>
  <p>.ebextensions/00_options.config</p>
  <pre><code>option_settings:
     aws:elasticbeanstalk:application:environment:
        DOMAIN: socket.example.com
        MASTER_DOMAIN: https://example.com
        etc..
  </code></pre>
  <p>.ebextensions/10_proxy.config</p>
  <pre><code>... some configs ...
  
  files:
  
    /etc/nginx/conf.d/proxy.conf:
      mode: &quot;000644&quot;
      owner: root
      group: root
      content: |
  
        upstream nodejs {
            server 127.0.0.1:8081;
            keepalive 256;
        }
  
        map $http_origin $cors_header {
            hostnames;
            default &quot;&quot;;
            `{&quot;Fn::GetOptionSetting&quot;: {&quot;Namespace&quot;: &quot;aws:elasticbeanstalk:application:environment&quot;, &quot;OptionName&quot;: &quot;MASTER_DOMAIN&quot;}}` &quot;$http_origin&quot;;
        }
  
        server {
  
            listen 80;
            listen 8080;
  
            server_name `{&quot;Fn::GetOptionSetting&quot;: {&quot;Namespace&quot;: &quot;aws:elasticbeanstalk:application:environment&quot;, &quot;OptionName&quot;: &quot;DOMAIN&quot;}}`;
  
            location ~ /.well-known {
                allow all;
                root /usr/share/nginx/html;
            }
            location / {
                return 301 https://$host$request_uri;
            }
        }
        
        etc..
  
  
  .... some more configs ....
        
  </code></pre>
  <p>I'm not including most of the configs above because they're not relevant.</p>
  <p>So when I did this before, everything worked as expected. The config file inserted my properties and created the file in the <code>/etc/nginx/conf.d/proxy.conf</code> folder.</p>
  <hr />
  <p>Now with AWS Linux 2 the specs have changed and we have to add our Nginx configuration files in the <code>.platform/nginx/conf.d</code> folder located in our application bundle root folder.</p>
  <p><a href="https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html" rel="noreferrer">Here the reference</a> ( see <strong>Reverse proxy configuration</strong>)</p>
  <p>So I created a <code>proxy.conf</code> file in the location mentioned above with the content that was previously inserted in <code>/etc/nginx/conf.d/proxy.conf</code>.</p>
  <hr />
  <p>.platform/nginx/conf.d/proxy.conf</p>
  <pre><code>upstream nodejs {
      server 127.0.0.1:8081;
      keepalive 256;
  }
      
  map $http_origin $cors_header {
      hostnames;
      default &quot;&quot;;
     `{&quot;Fn::GetOptionSetting&quot;: {&quot;Namespace&quot;: &quot;aws:elasticbeanstalk:application:environment&quot;, &quot;OptionName&quot;: &quot;MASTER_DOMAIN&quot;}}` &quot;$http_origin&quot;;
  }
  
  etc...
  </code></pre>
  <p>And then the problems began..</p>
  <p>This first trial throwed <code>unexpected &quot;{&quot; in /var/proxy/staging/nginx/conf.d/proxy.conf:11</code> at me.</p>
  <p>And after that I tried a lot of things. Tried it with <code>${MASTER_DOMAIN}</code> and messed around with the new EB AWS Linux 2 <code>hooks</code> (see link above <strong>Platform hooks</strong>). All for no avail it seems like you can't access the properties from the Nginx configs. I've read an article or a documentation from Nginx mentioning something similar today but I can't find it anymore (did a lot of googling).</p>
  <hr />
  <p>I also tried to create a config file like I did with the working version which purpose was to save a temp file somewhere with the included properties and then include this file in the needed <code>.platform/nginx/conf.d/proxy.conf</code> file because I started to think that there is no way to include them directly with the new specs.</p>
  <p>.ebextensions/10_proxy.config</p>
  <pre><code>... some configs ....
  
  files:
  
    /var/proxy/staging/custom_folder/proxy.conf:
      mode: &quot;000644&quot;
      owner: root
      group: root
      content: |
      
      etc...
  </code></pre>
  <p>.platform/nginx/conf.d/proxy.conf</p>
  <pre><code>include custom_folder/proxy.conf;
  </code></pre>
  <p>With this idea in mind I did a lot of nonsense, I created <code>hooks</code> for creating (<code>mkdir</code>) directories in which I tried to temporarily save the file which leaded to new permission errors. I wasn't able to give the proper permissions to <code>prebuild</code>, <code>postdeploy</code> files but this is another issue.</p>
  <p>And a lot more of trying and failing...</p>
  <hr />
  <p>But then I've read (also from the link above):</p>
  <p>&quot;<em>If you configure your proxy to send traffic to multiple application processes, you can configure several <strong>environment properties</strong>, and use their values in both <strong>proxy configuration</strong> and your application code.</em>&quot;</p>
  <p>And hope came back.. Does this mean I actually <strong>CAN</strong> directly add environmental variables into the Nginx configs located in the <code>.platform</code> directory? ... I don't know.. Do you?</p>
  <hr />
  <p>I could continue to describe all the things I tried all night long so I will stop here. I hope you get the issue. If not ask me and I will do my best to make all this understandable.</p>
  <p>Also my mind isn't very clear anymore after 14 hours of battling this issue. I need a break.</p>
  <p>If you did it to the end thank you for your time and help would be greatly appreciated.</p>