OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Access Elastic Beanstalk environment properties in NGINX configs running on AWS Linux 2

  • Thread starter Thread starter Getter Jetter
  • Start date Start date
G

Getter Jetter

Guest
I had this working before on AWS Linux AMI but no luck with AWS Linux 2.

I need to access my environment properties from the Nginx configuration file during the EB application deployment. It's a Single instance Node Server.

I did it like this with the AWS Linux AMI and it worked without a problem:

.ebextensions/00_options.config

Code:
option_settings:
   aws:elasticbeanstalk:application:environment:
      DOMAIN: socket.example.com
      MASTER_DOMAIN: https://example.com
      etc..

.ebextensions/10_proxy.config

Code:
... some configs ...

files:

  /etc/nginx/conf.d/proxy.conf:
    mode: "000644"
    owner: root
    group: root
    content: |

      upstream nodejs {
          server 127.0.0.1:8081;
          keepalive 256;
      }

      map $http_origin $cors_header {
          hostnames;
          default "";
          `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "MASTER_DOMAIN"}}` "$http_origin";
      }

      server {

          listen 80;
          listen 8080;

          server_name `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "DOMAIN"}}`;

          location ~ /.well-known {
              allow all;
              root /usr/share/nginx/html;
          }
          location / {
              return 301 https://$host$request_uri;
          }
      }
      
      etc..


.... some more configs ....

I'm not including most of the configs above because they're not relevant.

So when I did this before, everything worked as expected. The config file inserted my properties and created the file in the /etc/nginx/conf.d/proxy.conf folder.



Now with AWS Linux 2 the specs have changed and we have to add our Nginx configuration files in the .platform/nginx/conf.d folder located in our application bundle root folder.

Here the reference ( see Reverse proxy configuration)

So I created a proxy.conf file in the location mentioned above with the content that was previously inserted in /etc/nginx/conf.d/proxy.conf.



.platform/nginx/conf.d/proxy.conf

Code:
upstream nodejs {
    server 127.0.0.1:8081;
    keepalive 256;
}
    
map $http_origin $cors_header {
    hostnames;
    default "";
   `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "MASTER_DOMAIN"}}` "$http_origin";
}

etc...

And then the problems began..

This first trial throwed unexpected "{" in /var/proxy/staging/nginx/conf.d/proxy.conf:11 at me.

And after that I tried a lot of things. Tried it with ${MASTER_DOMAIN} and messed around with the new EB AWS Linux 2 hooks (see link above Platform hooks). 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).



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 .platform/nginx/conf.d/proxy.conf file because I started to think that there is no way to include them directly with the new specs.

.ebextensions/10_proxy.config

Code:
... some configs ....

files:

  /var/proxy/staging/custom_folder/proxy.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
    
    etc...

.platform/nginx/conf.d/proxy.conf

Code:
include custom_folder/proxy.conf;

With this idea in mind I did a lot of nonsense, I created hooks for creating (mkdir) 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 prebuild, postdeploy files but this is another issue.

And a lot more of trying and failing...



But then I've read (also from the link above):

"If you configure your proxy to send traffic to multiple application processes, you can configure several environment properties, and use their values in both proxy configuration and your application code."

And hope came back.. Does this mean I actually CAN directly add environmental variables into the Nginx configs located in the .platform directory? ... I don't know.. Do you?



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.

Also my mind isn't very clear anymore after 14 hours of battling this issue. I need a break.

If you did it to the end thank you for your time and help would be greatly appreciated.
<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: "000644"
owner: root
group: root
content: |

upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}

map $http_origin $cors_header {
hostnames;
default "";
`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "MASTER_DOMAIN"}}` "$http_origin";
}

server {

listen 80;
listen 8080;

server_name `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "DOMAIN"}}`;

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 "";
`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "MASTER_DOMAIN"}}` "$http_origin";
}

etc...
</code></pre>
<p>And then the problems began..</p>
<p>This first trial throwed <code>unexpected "{" 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: "000644"
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>"<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>"</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>
Continue reading...
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top