sst / ion

SST v3
https://sst.dev
MIT License
2.13k stars 250 forks source link

NextJS Deployment Error: ENOENT: no such file or directory, open 'resource.enc' #1278

Closed mattwarfield closed 4 days ago

mattwarfield commented 4 days ago

Description

When deploying a NextJS app to AWS using SST 3.2.11 with OpenNext, I'm encountering an error. The app works fine locally, but fails after deployment when accessing a page that references an S3 bucket.

Error Message

Error: ENOENT: no such file or directory, open 'resource.enc'

Steps to Reproduce

  1. Set up AWS accounts per https://sst.dev/docs/aws-accounts
  2. Create new monorepo project using SST monorepo-template
  3. Successfully deploy and test monorepo's original hello world API endpoint with S3 bucket (dev & prod stages)
  4. Add serverless NextJS frontend to monorepo and deploy
  5. Add reference to S3 bucket in NextJS frontend
    • Dev stage: works
    • Prod API endpoint: works
    • Prod NextJS frontend fails to build with the error message above

Environment

Code Snippet

// packages/frontend/src/app/page.tsx

import { Resource } from 'sst'

export default function Home() {
  const bucketName = Resource.MyBucket.name
  return (
    // ... rest of the component
  )
}

sst.config.ts

// @ts-nocheck
/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
  app(input) {
    return {
      name: 'helloworld',
      removal: input?.stage === 'production' ? 'retain' : 'remove',
      home: 'aws',
      providers: {
        aws: {
          profile:
            input.stage === 'production'
              ? 'helloworld-production'
              : 'helloworld-dev',
        },
      },
    }
  },
  async run() {
    await import('./infra/storage')
    await import('./infra/web')
    const api = await import('./infra/api')

    return {
      api: api.myApi.url,
    }
  },
})

Full Error Stack Trace

ERROR    ⨯ Error: ENOENT: no such file or directory, open 'resource.enc'
    at Object.openSync (node:fs:573:18)
    at readFileSync (node:fs:452:35)
    at 3138 (/var/task/packages/frontend/.next/server/app/page.js:1:32533)
    at t (/var/task/packages/frontend/.next/server/webpack-runtime.js:1:127)
    at 4455 (/var/task/packages/frontend/.next/server/app/page.js:1:718)
    at t (/var/task/packages/frontend/.next/server/webpack-runtime.js:1:127)
    at r (/var/task/packages/frontend/.next/server/app/page.js:1:33723)
    at /var/task/packages/frontend/.next/server/app/page.js:1:33754
    at t.X (/var/task/packages/frontend/.next/server/webpack-runtime.js:1:1191)
    at /var/task/packages/frontend/.next/server/app/page.js:1:33736 {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'resource.enc'
}

Additional Information

Questions

  1. What might be causing this 'resource.enc' file error?
  2. How can I properly reference the S3 bucket in a deployed NextJS app in a SST monorepo?
  3. Are there any known issues with SST 3.2.11 and NextJS 14.2.15 integration?
jacksors commented 4 days ago

Currently getting the same issue with 3.2.19 and next 14.2.15.

Rolling SST back to 3.1.78 seems to resolve it, I think the issue stems from https://github.com/sst/ion/commit/3cf7254d7260f58c2d75971f3df1197ecb2efac9

mattwarfield commented 4 days ago

Thanks @jacksors! Appreciate you taking a look.

I can confirm that rolling back to SST version 3.1.78 resolved the issue for me as well. The app now deploys and functions correctly without the 'resource.enc' error.

sommeeeer commented 4 days ago

can confirm this is happening all versions above 3.1.78. Another workaround (that is not pretty, since its jsonfied) is to use process.env.SST_RESOURCE_MySecret as the secrets/links are injected into the environment on top of the server-index handler file.

A better workaround is just removing the environment variable called SST_KEY_FILE on the default server function

thdxr commented 4 days ago

sorry about that we have a fix in v3.2.20 that should remove the env but will do a more correct fix later today