How do I perform Laravel Artisan migrations on AWS Elastic Beanstalk?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How Do I Perform Laravel Artisan Migrations on AWS Elastic Beanstalk?

Deploying a Laravel application to a managed platform like AWS Elastic Beanstalk (EB) often presents a specific challenge: ensuring that database schema migrations are executed correctly and idempotently across all environments. When you have separate staging and production databases managed by AWS RDS, running migrations on the live server requires more than just running the command locally.

As a senior developer, I’ve seen teams struggle with this deployment step. Running php artisan migrate directly in a standard web request context can lead to race conditions or unexpected failures if not handled correctly within the Elastic Beanstalk lifecycle. This post will walk you through the robust method for executing Laravel migrations reliably on your AWS EB instances.

Understanding the Migration Challenge in Cloud Environments

In a local Vagrant setup, running migration commands is straightforward because you control the entire environment—the PHP process and the database connection are tightly coupled. However, on Elastic Beanstalk, the deployment process involves uploading code, which triggers a new application instance. We need a mechanism to ensure that the migrations run once upon deployment, rather than attempting to run them on every web request.

The core principle is treating migrations as a distinct, prerequisite step in your Continuous Integration/Continuous Deployment (CI/CD) pipeline, separate from the web request handling.

The Recommended Approach: Using a Deployment Script

The most reliable way to handle this is to introduce an explicit deployment script that runs Artisan commands before or during the application launch phase on the server. This ensures that the database structure is correctly initialized before any user traffic hits your application.

Step 1: Preparing the Environment

Ensure your Elastic Beanstalk environment is configured to access the necessary environment variables (like those pointing to your RDS credentials) via the .ebextensions directory or environment settings. Your configuration, as seen in your example with app/config/staging/database.php, correctly separates these details based on the deployed environment.

Step 2: Creating the Deployment Command Script

Since Elastic Beanstalk manages the underlying EC2 instances, we need to instruct the application runtime (PHP) to execute the migration command when the environment starts up or during a specific deployment phase.

A common technique is to define a custom entry point or use a shell script executed by the deployment process. When deploying via git aws.push, you typically push your code and configuration files, but you might need an additional step in your pipeline (like AWS CodePipeline or a custom script) that executes the migration command directly on the deployed instance.

Here is how you structure the execution command:

#!/bin/bash

# Navigate to the application root directory
cd /var/www/your-app-directory

# Ensure Composer dependencies are installed (good practice for any Laravel deployment)
composer install --no-dev --optimize-autoloader

# Execute the migration command using the full path to the PHP binary
# This ensures you are running the command within the context of the EB environment.
/usr/bin/php artisan migrate --force

Step 3: Integrating into Elastic Beanstalk Deployment

For a seamless experience, integrate this step into your deployment workflow. If you are using a CI/CD tool (like GitHub Actions or Jenkins), the process looks like this:

  1. Build Stage: Compile dependencies (composer install).
  2. Deployment Stage: Deploy the code bundle to Elastic Beanstalk.
  3. Post-Deployment Hook (Crucial Step): After the deployment is complete, use an AWS CLI command or a custom script triggered by EB to SSH into the newly deployed instance and execute your migration script defined above.

This approach adheres to Laravel best practices for migrations, which you can find detailed guidance on from Laravel documentation. By isolating the migration step outside of the standard web request flow, you ensure data integrity regardless of how many requests are hitting the server simultaneously.

Conclusion

Executing Laravel Artisan migrations on AWS Elastic Beanstalk is fundamentally about shifting the execution context from your local machine to the deployed cloud instance in a controlled manner. Relying on a pre-deployment script executed via an external pipeline ensures that your staging and production databases remain synchronized, avoiding the pitfalls of running database operations within live HTTP requests. By treating migrations as infrastructure setup rather than runtime logic, you build more resilient and predictable deployments.