Bobcares

Letsencrypt aws elastic beanstalk | Configuration steps

by | Nov 6, 2022

We will check how to set up Letsencrypt aws elastic beanstalk. At Bobcares, AWS Support Services provides solutions to your AWS queries.

Letsencrypt aws elastic beanstalk

 

Elastic Beanstalk is a Platform as a Service that allows users to deploy and scale web apps easily.

 

Having Let’s Encrypt on AWS EB can be a bit tricky. From this article, we will explain how to configure your environment correctly for one or several domains/subdomains.

Letsencrypt aws elastic beanstalk

.ebextensions folder

 

Ebextensions folder holds the configuration of the AWS EB application. This folder contains .config files. Note that this is NOT inside your Docker image. You would require to create a .ebextensions folder, in the root folder of your git repository.

 
➜  webapp git:(master) ✗ ls -la
drwxr-xr-x  20 user  admin    640 Apr 10 01:17 .
drwxrwxr-x  12 user  admin    384 Mar 30 22:48 ..
drwxr-xr-x   4 user  admin    128 Jul  1  2019 .ebextensions
drwxr-xr-x  15 user  admin    480 Apr 29 14:19 .git
-rw-r--r--@  1 user  admin   4314 Jul 23  2019 Dockerfile
drwxr-xr-x  28 user  admin    896 Mar 24 15:16 laravel
 

For this example, we will create a new file name: AWS_letsencrypt_config.config.

 
➜  webapp git:(master) ✗ ls -la .ebextensions
total 24
drwxr-xr-x   4 user  admin   128 Jul  1  2019 .
drwxr-xr-x  20 user  admin   640 Apr 10 01:17 ..
-rw-r--r--@  1 user  admin  4710 Mar 27 19:23 AWS_letsencrypt_config.config
 

We have taken one domain and two subdomains in this example:

 
  • domain
  • admin.domain.com
  • test.domain.com
 

Step 1: Configure the security groups

 

This step will allow traffic on SSL port 443. If you have an existing instance then open port 443 on your docker image.

 
Resources:
  sslSecurityGroupIngress: 
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: {"Fn::GetAtt" : ["AWSEBSecurityGroup", "GroupId"]}
      IpProtocol: tcp
      ToPort: 443
      FromPort: 443
      CidrIp: 0.0.0.0/0
 

Step 2: Configure Nginx

 

Now we will create two configuration files.

 

The first file (000_http_redirect_custom.conf). This will tell the Nginx server to listen to any requests that come on default HTTP port 80 and then redirect them to HTTPS.

 
files:
  /etc/nginx/conf.d/000_http_redirect_custom.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      server {
        listen 80;
        return 301 https://$host$request_uri;
      }
 

Next, you need to create another file. The second file “https_custom.pre”, is about the SSL configuration. Further, this is helpful for the file location of certificates and proxy.

 
/etc/nginx/conf.d/https_custom.pre:
    mode: "000644"
    owner: root
    group: root
    content: |
      # HTTPS server
      server {
        listen       443 default ssl;
        server_name  localhost;
        error_page  497 https://$host$request_uri;
        
        ssl_certificate      /etc/letsencrypt/live/ebcert/fullchain.pem;
        ssl_certificate_key  /etc/letsencrypt/live/ebcert/privkey.pem;

        ssl_session_timeout  5m;
        ssl_protocols  TLSv1.1 TLSv1.2;
        ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
        ssl_prefer_server_ciphers   on;

        location / {
          proxy_pass http://docker;
          proxy_http_version 1.1;

          proxy_set_header Connection "";
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }
 

Step 3: Container configuration

 

packages: package option grabs Extra Packages for Enterprise Linux (EPEL) for our certbot.

 

container_commands: the given code is divided into 6 steps :

 
  1. 00_create_dir : create a path for certbot.
  2. 10_installcertbotandchmod : install certbot-auto and give permission.
  3. 20_getcert : generate a certificate.
  4. 30_link : create a symbolic link between certificate and the live configuration directory.
  5. 40_config : certificates are created. so now we can rename the .pre to .config.
  6. 50_cronjobsetrenewal : create a cron job.
 

Now set the EMAIL variable of your environment configuration from your AWS Console.

 
packages: 
  yum:
    epel-release: []

container_commands:
  00_create_dir:
    command: "mkdir -p /opt/certbot"
  10_installcertbotandchmod:
    command: "wget https://dl.eff.org/certbot-auto -O /opt/certbot/certbot-auto;chmod a+x /opt/certbot/certbot-auto"
  20_getcert:
    command: "sudo /opt/certbot/certbot-auto certonly --standalone --debug --non-interactive --email ${EMAIL} --agree-tos --domains domain.com \
    -d admin.domaiun.com \
    -d test.domaiun.com \
    --expand --renew-with-new-domains --pre-hook \"service nginx stop\""
  30_link:
    command: "sudo ln -sf /etc/letsencrypt/live/domain.com /etc/letsencrypt/live/ebcert"
  40_config:
    command: "mv /etc/nginx/conf.d/https_custom.pre /etc/nginx/conf.d/https_custom.conf"
  50_cronjobsetrenewal:
    command: '(crontab -l ; echo ''0 6 * * * root /opt/certbot/certbot-auto renew --standalone --pre-hook "service nginx stop" --post-hook "service nginx start" --force-renew'') | crontab -'
 

That’s it! Save your changes and deploy it: eb deploy.

 

[Looking for a solution to another query? We are just a click away.]

 

Conclusion

To sum up,From this Letsencrypt aws elastic beanstalk article. You have learned how to configure environment for one or multiple domains or subdomains.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.