Deploying Laravel Application On AWS ElasticBeanstalk from Git using CodePipeline
Tags: aws, codepipeline, elasticbeanstalk, laravel
Published: 2022-10-06 15:48:59

So you have built your awesome laravel application, and now you want to deploy it on AWS Elasticbeanstalk .

Here are some steps that could help you to successfully deploy your laravel application on elastic beanstalk from GIT using CodePipeline.

This article is for someone who is familiar with the aws environment but struggling deploying Laravel application on elastickbeanstalk from Git using CodePiepline

**Step 1 :**
Create a  “**.ebextensions**” folder in you Laravel Application Root directory  as shown in the image below
 
![Step 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8la9c1hnwcv2cnkq4jj4.png)

Inside the “**.ebextensions**” folder , create an “**environment.config**”  file , it could be any name of your choice , copy and paste the content below in the config file you created.

```
option_settings:
  aws:elasticbeanstalk:container:php:phpini:
    document_root: /public
  aws:elasticbeanstalk:application:environment:
    APP_NAME: Laravel
    APP_ENV: local
    APP_KEY: “your_app_key”
    APP_DEBUG: true
    APP_URL: http://localhost
    LOG_CHANNEL: stack
    LOG_DEPRECATIONS_CHANNEL: null
    LOG_LEVEL: debug
    DB_CONNECTION: mysql
    DB_HOST: "${RDS_HOSTNAME}"
    DB_PORT: "${RDS_HOSTNAME}"
    DB_DATABASE: "${RDS_DB_NAME}"
    DB_USERNAME: "${RDS_USERNAME}"
    DB_PASSWORD: "${RDS_PASSWORD}"
    BROADCAST_DRIVER: log
    CACHE_DRIVER: file
    FILESYSTEM_DRIVER: local
    QUEUE_CONNECTION: sync
    SESSION_DRIVER: file
    SESSION_LIFETIME: 120
    MEMCACHED_HOST: 127.0.0.1
    REDIS_HOST: 127.0.0.1
    REDIS_PASSWORD: null
    REDIS_PORT: 6379
    MAIL_MAILER: smtp
    MAIL_HOST: mailhog
    MAIL_PORT: 1025
    MAIL_USERNAME: null
    MAIL_PASSWORD: null
    MAIL_ENCRYPTION: null
    MAIL_FROM_ADDRESS: null
    MAIL_FROM_NAME="${APP_NAME}"
    AWS_ACCESS_KEY_ID: ''
    AWS_SECRET_ACCESS_KEY: ''
    AWS_DEFAULT_REGION: us-east-1
    AWS_BUCKET: ''
    AWS_USE_PATH_STYLE_ENDPOINT: false
    PUSHER_APP_ID: ''
    PUSHER_APP_KEY: ''
    PUSHER_APP_SECRET: ''
    PUSHER_APP_CLUSTER: mt1
    MIX_PUSHER_APP_KEY: "${PUSHER_APP_KEY}"
    MIX_PUSHER_APP_CLUSTER: "${PUSHER_APP_CLUSTER}"
container_commands:
  01-no_dev:
    command: "composer.phar install --optimize-autoloader"
  02-artisan_optmize:
    command: "php artisan optimize"
files:
    "/etc/cron.d/schedule_run":
        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * webapp php /var/www/html/artisan schedule:run --env=production >> /dev/null 2>&1
    "/etc/php.d/99max_size.ini" :
        mode: "000755"
        owner: root
        group: root
        content: |
          upload_max_filesize = 128M
          post_max_size = 128M 
```

Create a second config file , could be anyname different from the name of the first config file. Copy and paste the content below in the newly created config file.
```
commands:
    setvars:
        command: /opt/elasticbeanstalk/bin/get-config optionsettings | \jq '."aws:elasticbeanstalk:application:environment"' | \jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/local.sh

```
The command in this config file exposes the ElasticBeanstalk environment variables to the EC2 instance created by the Elasticbeanstalk instance so that your laravel application can have access to the environment variables

**Step 2:**
Create a **.platform** folder in the root of your laravel application , following this structure in the image below
 
![Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjztjdrpedzsg3pwkjwr.png)
Copy and paste the content below in the **laravel.conf** file 
```
location / {
         try_files $uri $uri/ /index.php?$query_string;
         gzip_static on;
     }
 client_max_body_size 128M;

```
**Step 3 :**
Head over to https://console.aws.amazon.com, create an elastic beanstalk instance , select “**Web server enviroment**” as show in the picture below. 
 

![Image 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlwesz23fqboqlpicx68.png)

![Image 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bwfju3y1qs10ektlpz9.png)

**Step 4 :** After you are done creating your elasticbeanstalk instance , go [here](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_PHP.rds.html) to see how to attach an rds database to your elasticbeanstalk instance . This will expose the following environment variables to your laravel application
  ```
    RDS_HOSTNAME
    RDS_HOSTNAME
    RDS_DB_NAME
    RDS_USERNAME
    RDS_PASSWORD
```
You can either change your laravel config to read this environment variable name directly in your “**config/database.php**” or you reference it as shown in the environment.config file you created in step 1.

**Step 4 :**

• Create a code pipeline on AWS.
• Choose Source Provider as “**Github (Version 2)**” , you can skip the build stage.
• On the Deploy stage  choose “AWS Elasticbeanstalk” as your deploy provider, you would then select the earlier created application from the list of application names available
• Once you are done filling all the necessary information, review and release changes 

![Image 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4axoo19m1kuwibcjay5.png)
 
**Step 5:** Your Pipeline would run through all the stages and automatically deploy your laravel application to your elasticbeanstalk environment