apache - AWS Elastic Beanstalk instance crashing after deploying Next.js app - Stack Overflow

admin2025-05-02  1

I'm running a Next.js application on an AWS Elastic Beanstalk environment using Apache and Node.js 22 on 64-bit Amazon Linux 2023. Most of the time, after running eb deploy, the instance crashes.

Issue

After deployment, the Elastic Beanstalk environment health transitions from Info to Severe, with the following message in the AWS console:

Environment health has transitioned from Info to Severe.
ELB processes are not healthy on all instances.
Application update in progress on 1 instance. 0 out of 1 instance completed (running for 5 minutes).
None of the instances are sending data.
ELB health is failing or not available for all instances.

When this happens (not with every deploy but most of them), the instance becomes unusable:

  • I can't connect to the instance via SSH.
  • Logs cannot be retrieved from the AWS console.
  • My only option is to terminate the instance and let Elastic Beanstalk create a new one.

Context

  1. Application-Specific Behavior
  • The app checks the Host header to determine the configuration dynamically.
  • The application relies on custom configurations for deployment and proxying.
  1. Custom Deployment Script I added a .ebextensions/build.config file to ensure the application builds and starts correctly during deployment:
container_commands:
  01_install_dependencies:
    command: "npm install"
    cwd: "/var/app/staging"
  02_build_application:
    command: "npm run build"
    cwd: "/var/app/staging"
  03_copy_build_to_current:
    command: "cp -R .next /var/app/current/.next"
    cwd: "/var/app/staging"

This was introduced to address previous issues with the application failing to start properly after deployment.

  1. Proxy Configuration I have the following configuration in .ebextensions/httpd-proxy.config to handle domain redirection, SSL, and backend proxying:
files:
  "/etc/httpd/conf.d/proxy.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      <VirtualHost *:80>
        ProxyPreserveHost On
        ServerName myServerName
        ServerAlias \
          "*.myUrls" \
        ProxyTimeout 60
        ProxyPass / http://127.0.0.1:8080/ connectiontimeout=10 timeout=180
        ProxyPassReverse / http://127.0.0.1:8080/
        ErrorLog /var/log/httpd/error.log
        CustomLog /var/log/httpd/access.log combined
        LogLevel warn
        <Location "/api/health">
          SetHandler proxy:fcgi://127.0.0.1:8080
        </Location>
      </VirtualHost>

Troubleshooting Steps Taken

  1. Checked Logs:
  • When the instance crashes, logs cannot be retrieved via the AWS console or through SSH.
  • I suspect the issue may be related to either:
  • Timeouts or health check failures during the deployment process.
  • Configuration mismatches with the ProxyPass setup or the .ebextensions script.
  1. Custom Configuration:
  • I've tried tweaking the ProxyTimeout, connectiontimeout, and timeout values, but the issue persists.
  • The app runs fine in local development and in staging environments without Elastic Beanstalk.

Questions

  1. Why does the instance become completely unresponsive (unable to SSH or retrieve logs) after deployment? Is there something wrong with my configurations or deployment scripts?
  2. How can I debug the health check failures and improve the deployment reliability?
  3. Is there a better way to handle custom proxy configurations and deployment scripts in Elastic Beanstalk for Next.js applications? Any guidance would be greatly appreciated! Thanks in advance.

Edit

I've removed the build.config and added a .platform/hooks/postdeploy/postdeploy.sh file with

#!/bin/bash
npm install --production=false

It made the deploy way more stable but it just crash again for the first time in a week.

转载请注明原文地址:http://anycun.com/QandA/1746123459a91998.html