Amplify NextJS build failing sporadically with "[ERROR]: !!! Build failed"

0

We have been using Amplify to host our NextJS client app for the past 6 months now. Our app has been increasing in complexity and subsequently I can see build times increased from previous 6 mins to now around 14 mins. The repo structure is a monorepo managed by turborepo where the NextJS app imports from common packages and other configs.

However, recently we have noticed builds failing randomly with no particular reason for why it failed. Sharing the updated error logs below.

2025-04-08T07:41:30.480Z [INFO]: @company/web-ui:build:    Creating an optimized production build ...
2025-04-08T07:45:42.604Z [WARNING]: x internal errors encountered: external process killed a task
2025-04-08T07:45:42.757Z [ERROR]: !!! Build failed
2025-04-08T07:45:42.757Z [INFO]: Please read more about Amplify Hosting's support for SSR frameworks to find if your build failure is related to an unsupported feature: https://docs.aws.amazon.com/amplify/latest/userguide/amplify-ssr-framework-support.html. You may also find this troubleshooting guide useful: https://docs.aws.amazon.com/amplify/latest/userguide/troubleshooting-ssr-deployment.html
2025-04-08T07:45:42.757Z [ERROR]: !!! Error: Command failed with exit code 1

The warning is coming from turborepo saying that something external is killing the build task. My hunch is that this is a RAM or a compute issue but cannot pinpoint the exact issue. Local builds work fine. We also run cypress tests on Github Actions and building the project there works without issues as well.

I have tried using swapon to increase swap memory but looks like that does not seem to work as well.

Potential Similar Issue: https://github.com/aws-amplify/amplify-hosting/issues/654#issuecomment-2653694219

Build Script

version: 1
applications:
  - frontend:
      phases:
        preBuild:
          commands:
            - sudo fallocate -l 10G /swapfile
            - sudo chmod 600 /swapfile
            - sudo mkswap /swapfile
            - sudo swapon /swapfile || (sudo dmesg | tail -20)
            - sudo swapon -s
            - pwd
            - npm install
        build:
          commands: 
            - npx turbo run build --filter=@outmarket/web-ui...
      artifacts:
        baseDirectory: .next
        files:
          - "**/*"
      cache:
        paths:
          - .next/cache/**/*
          - .npm/**/*
          - node_modules/**/*
    appRoot: apps/product-frontend/apps/web-ui

Thanks in advance.

asked a month ago103 views
1 Answer
0

The issue you're experiencing with your Amplify NextJS build failing sporadically is likely related to memory constraints during the build process. As your app has grown in complexity and build times have increased, it's possible that the build is hitting memory limits, causing it to fail unexpectedly.

The error message "external process killed a task" from turborepo suggests that the build process is being terminated by an external factor, which is consistent with running out of memory.

Here are some steps you can try to address this issue:

  1. Increase memory allocation:

    • Add an environment variable to increase Node.js memory allocation: export NODE_OPTIONS="--max-old-space-size=8192" (or a higher value)
    • Add this to your build commands in the amplify.yml file.
  2. Optimize your build process:

    • Review your dependencies and remove any unnecessary ones.
    • Consider using techniques like code splitting to reduce the bundle size.
    • Optimize your use of turborepo, ensuring you're only building what's necessary.
  3. Adjust Amplify build settings:

    • In the Amplify Console, go to App settings > Build settings.
    • Try increasing the compute capacity for your builds.
  4. Cache optimization:

    • Ensure you're caching effectively. Your current cache paths look good, but double-check that all necessary directories are included.
  5. Monitor build logs:

    • Keep a close eye on your build logs to identify any specific steps where memory usage spikes.
  6. Consider breaking down your build:

    • If possible, break your monorepo build into smaller, separate builds to reduce overall memory usage.

If these steps don't resolve the issue, you may need to reach out to AWS Support for further assistance, as there could be underlying issues with the build environment that are beyond your control.

Remember, local builds and GitHub Actions builds may have different resource allocations compared to the Amplify environment, which is why you're not seeing the same issues there.
Sources
Build container ran out of memory | AWS re:Post
on amplify react app deploy issue | AWS re:Post

profile picture
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions