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.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF