使用AWS EB CLI部署时如何排除指定目录以保留文件?
/public/images During Elastic Beanstalk Deployments with EB CLI Hey there, I’ve tackled this exact problem before—so let’s get you sorted out. The short answer is yes, Elastic Beanstalk absolutely supports preserving specific directories during deployments, and there are a couple of reliable ways to do it. Let’s break down the best approaches, plus some alternative deployment strategies if you want to explore other options.
Option 1: Mount Amazon EFS to /public/images (Recommended)
This is the most robust solution for persistent user-uploaded content, as it decouples your static files from the application deployment lifecycle. EFS provides scalable, shared storage that stays intact even when your EB instances are replaced or updated.
Steps to Set Up:
- Create an Amazon EFS file system in the same VPC as your Elastic Beanstalk environment. Ensure the EFS security group allows inbound NFS traffic (port 2049) from your EB environment’s security group.
- Add an
.ebextensionsconfig file to your project root (e.g.,.ebextensions/efs-mount.config) with the following content:
Replacecommands: 01_create_images_dir: command: mkdir -p /var/app/current/public/images ignoreErrors: true 02_mount_efs: command: | mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport YOUR_EFS_FILE_SYSTEM_ID.efs.YOUR_REGION.amazonaws.com:/ /var/app/current/public/images 03_update_fstab: command: echo "YOUR_EFS_FILE_SYSTEM_ID.efs.YOUR_REGION.amazonaws.com:/ /var/app/current/public/images nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport 0 0" >> /etc/fstabYOUR_EFS_FILE_SYSTEM_IDandYOUR_REGIONwith your actual EFS details. - Deploy your app with
eb deployas usual. From now on,/public/imageswill point to the EFS mount, so all uploaded images will persist across deployments.
Option 2: Backup/Restore with EB Deployment Hooks (Temporary Fix)
If you need a quick solution without setting up EFS, you can use EB’s deployment hooks to backup the /public/images directory before deployment and restore it afterward. Note: This only works if your EB instances aren’t replaced (e.g., during scaling or environment updates), so it’s not ideal for long-term use.
Steps to Set Up:
Create an .ebextensions/preserve-images.config file with:
files: # Pre-deploy hook: Backup existing images to /tmp "/opt/elasticbeanstalk/hooks/appdeploy/pre/01_backup_images.sh": mode: "000755" owner: root group: root content: | #!/bin/bash mkdir -p /tmp/images_backup cp -r /var/app/current/public/images/* /tmp/images_backup/ 2>/dev/null || true # Post-deploy hook: Restore images to the new deployment "/opt/elasticbeanstalk/hooks/appdeploy/post/01_restore_images.sh": mode: "000755" owner: root group: root content: | #!/bin/bash mkdir -p /var/app/current/public/images cp -r /tmp/images_backup/* /var/app/current/public/images/ 2>/dev/null || true
Deploy with eb deploy—the hooks will automatically handle backing up and restoring your images.
Alternative Deployment Strategies
If you’re open to moving beyond EB CLI, here are some other AWS-native options:
- AWS CodePipeline + CodeDeploy: CodeDeploy gives you granular control over which files are replaced during deployment. You can define an
appspec.ymlto exclude/public/imagesor run custom scripts to preserve the directory. - Containerized Deployment (ECS/EKS): Package your PHP app into a Docker container, then mount EFS or S3 to the
/public/imagespath. Containers make deployments consistent, and persistent storage keeps your images safe. - Direct EC2 Deployment: Use
rsyncto push code to your EC2 instances with an exclusion rule:rsync -av --exclude='public/images' ./ ec2-user@your-instance-ip:/var/www/html/ - S3 + CloudFront for Static Assets: For user-uploaded images, consider storing them directly in S3 instead of on your EB instance. Use CloudFront to serve the images with low latency—this is cheaper, more scalable, and eliminates deployment-related file loss entirely.
Hope one of these solutions works for you—feel free to ask if you need help with any specific setup step!
内容的提问来源于stack exchange,提问作者Dexter Bengil

