I am trying to deploy several services using AWS Elastic Beanstalk
with Docker running on Amazon Linux 2
platform.
Since, there are two services in my docker-compose.yml
file:
version: '3.8'
services:
beanstalk-flask:
image: "anotheruserdocker/beanstalk-flask"
ports:
- "5000:5000"
tasks:
image: "xxxxx.dkr.ecr.us-east-1.amazonaws.com/xxx:xxx"
ports:
- "8080:8080"
I need to change nginx
service configuration in order to proxy traffic to specific service.
I was following the documentation, in which it was noted that you can override the default nginx.conf
with your own and in order to do it, you need to place your config file in the application source bundle
, like so .platform/nginx/nginx.conf
.
I have also included this include conf.d/elasticbeanstalk/*.conf;
line in order to override it.
nginx.conf file:
# Elastic Beanstalk Nginx Configuration File
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
worker_rlimit_nofile 32633;
include conf.d/elasticbeanstalk/*.conf;
upstream service_1 {
server 172.17.0.1:8080;
keepalive 256;
}
upstream serivce_2 {
server 172.17.0.1:5000;
keepalive 256;
}
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include conf.d/*.conf;
map $http_upgrade $connection_upgrade {
default "upgrade";
}
server {
listen 80 default_server;
gzip on;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
access_log /var/log/nginx/access.log main;
location / {
proxy_pass http://service_1;
proxy_http_version 1.1;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /api {
proxy_pass http://service_2;
proxy_http_version 1.1;
}
# Include the Elastic Beanstalk generated locations
include conf.d/elasticbeanstalk/*.conf;
}
}
Once I’m uploading the application source bundle
that looks like this:
docker-compose.yml
.platform/nginx/nginx.conf
the configuration doesn’t change.
Am I missing something, is it a bug, or are there any other ways to change/modify the default nginx
configuration?
Also, I have noticed that upon booting nginx.service
isn’t in running state, is it possible to start this service upon boot?
Thank you.
2
Answers
Found a possible solution.
During the creation of
AWS Elastic Beanstalk
environment (if you are using Load Balanced deployment type), you can add processes which Load Balancer will register(?).Once I've added the processes (that run on
8080
and5000
ports), I created additionallistener
for the Application Load Balancer that listens to traffic on port5000
(I only did this for this port, because by defaultAWS Elastic Beanstalk
environment creates a listener that forwards traffic to the target group of EC2 instance that was running on the specified8080
port) and forwards it to the target group of the process that runs it on this port.Interestingly enough, I don't really know how this worked, I've connected to the EC2 instance and noticed that
nginx.service
was ininactive
state.Probably I don't understand clearly how this works behind the scenes, any clarifications would be much appreciated.
Thank you!
P.S.: Once I get enough reputation points, I'll attach some screenshots of the steps taken.
The Nginx service is not configured at all. With AWS Elastic Beanstalk with Docker running on Amazon Linux 2 + docker compose they assume you run Nginx in a container.
It’s documented here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker.container.console.html#docker-software-config