Skip to content

Instantly share code, notes, and snippets.

@r0lodex
Created June 17, 2020 23:52
Show Gist options
  • Save r0lodex/50947d8d98fabea1051d74a7f697ff38 to your computer and use it in GitHub Desktop.
Save r0lodex/50947d8d98fabea1051d74a7f697ff38 to your computer and use it in GitHub Desktop.
AWS Elastic Beanstalk Notes

AWS Elastic Beanstalk Notes

I've been playing around with Beanstalk, swimming in their scattered docs to make things work the way I wanted them to work. This note is written 18th June 2020, just so you know it works at this time of writing.

Platform: PHP

Configuring NGINX

  • Add .platfrom/nginx in your applications folder
  • nginx.conf will overwrite Beanstalk's default Nginx configuration
  • By default, the config will include conf.d/xxx.conf
  • Here's the default configuration of nginx.conf
#Elastic Beanstalk Nginx Configuration File

user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    32145;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;
        access_log    /var/log/nginx/access.log main;

        client_header_timeout 60;
        client_body_timeout   60;
        keepalive_timeout     60;
        gzip                  off;
        gzip_comp_level       4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }
}

HTTP to HTTPS redirect

  • You'll need to have the SSL certificates ready and set in your Beanstalk environment. This should be fairly easy to setup.
  • Use ACM for the cert, and configure your environment's Application Load Balancer to have a 443 listener.
  • Previously I use Nginx to redirect traffic to HTTPS, but that'll go through ALB first.
  • Instead, my colleague @od3n adviced to let ALB do the work, so I figured that could be done via .ebextensions.
  • Furthermore we can track changes from there.
...

files: ...
option_settings: ...

...

Resources:
  AWSEBV2LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      LoadBalancerArn:
        Ref: AWSEBV2LoadBalancer
      Port: 80
      Protocol: HTTP
      DefaultActions:
        - Type: redirect
          RedirectConfig:
            Host: "#{host}"
            Path: "/#{path}"
            Port: "443"
            Protocol: "HTTPS"
            Query: "#{query}"
            StatusCode: "HTTP_301"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment