You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS EB CLI部署时如何排除指定目录以保留文件?

How to Preserve /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.


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:

  1. 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.
  2. Add an .ebextensions config file to your project root (e.g., .ebextensions/efs-mount.config) with the following content:
    commands:
      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/fstab
    
    Replace YOUR_EFS_FILE_SYSTEM_ID and YOUR_REGION with your actual EFS details.
  3. Deploy your app with eb deploy as usual. From now on, /public/images will 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.yml to exclude /public/images or 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/images path. Containers make deployments consistent, and persistent storage keeps your images safe.
  • Direct EC2 Deployment: Use rsync to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:45:41