如何防止AWS CodePipeline部署Laravel应用时删除存储数据
Hey there, let's break down why your stored data keeps getting wiped on deployments and how to fix this for good.
The Root Cause
Elastic Beanstalk deploys new application code to a fresh directory on your EC2 instances, then switches traffic to this new deployment. Once the switch is complete, the old app directory gets cleaned up. If you're storing dynamic data (like user uploads, cache files, or generated content) inside Laravel's default storage directory (which lives in your app's root folder), this data gets deleted every time you push a new master branch—because the entire app directory gets replaced with the fresh deployment package.
Actionable Solutions
1. Use Persistent EBS Storage (Single Instance Environments)
If you're running a single EC2 instance environment, mount a persistent EBS volume to a non-app directory and reconfigure Laravel to use this path:
- First, add an EBS volume to your Elastic Beanstalk environment via the AWS Console (or automate it with
.ebextensions). Mount it to a path like/var/app/storage-persistent. - Update your Laravel
config/filesystems.phpto point your storage disks to this persistent path. For example, the public disk:'public' => [ 'driver' => 'local', 'root' => '/var/app/storage-persistent/app/public', 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ], - Set proper permissions for the web server user with an
.ebextensionsconfig file (create.ebextensions/storage-permissions.config):container_commands: 01_assign_ownership: command: "chown -R webapp:webapp /var/app/storage-persistent" 02_create_required_dirs: command: "mkdir -p /var/app/storage-persistent/app/public /var/app/storage-persistent/framework/cache"
2. Use S3 for User Uploads & Static Content
For multi-instance (load-balanced) environments, local EBS storage won't work (each instance has its own volume). Instead, configure Laravel to use Amazon S3 for storing user-uploaded files:
- Update
config/filesystems.phpto enable the S3 disk (ensure you have the AWS SDK installed via Composer):'s3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), ], - Set your default storage disk to
s3in.env:FILESYSTEM_DISK=s3 - This way, all uploaded files live in S3, which persists across deployments and is accessible by all your EC2 instances.
3. Offload Cache & Session Data
If your lost data includes cache or session data, don't store these locally. Use external services instead:
- Redis: Configure Laravel to use Redis for cache and sessions (set up an ElastiCache Redis cluster in AWS). Update
config/cache.phpandconfig/session.phpto set'driver' => 'redis'. - Database: Alternatively, store sessions in your database by setting
'driver' => 'database'inconfig/session.php. This ensures session data survives deployments and instance replacements.
Key Takeaway
Never store dynamic, persistent data inside your application's root directory when using Elastic Beanstalk—this directory is ephemeral and gets replaced on every deployment. Always use external persistent storage (EBS, EFS, S3) or managed services (Redis, RDS) for data that needs to stick around.
内容的提问来源于stack exchange,提问作者Hilal Najem

