Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I confirm that env variables are being added to Hosting Lambdas (Web Compute)? #3345

Closed
4 tasks done
foobarnes opened this issue Mar 3, 2023 · 4 comments
Closed
4 tasks done
Labels
archived This issue has been locked. compute investigating question Further information is requested

Comments

@foobarnes
Copy link

Before opening, please confirm:

App Id

d263pg34gxs8oh

AWS Region

us-east-1

Amplify Hosting feature

Environment variables

Question

I am using NextAuth. It requires an env variable NEXTAUTH_URL to be set.

NextAuth makes available several auth-related api routes with a catchall/decomp. route. api/auth/[...nextauth].js

I am logging process.env.NEXTAUTH_URL at the bottom of this route generator/config, and it seems to be correctly set at the time of NextAuth's initialization.

Immediately after, however, it appears to be unavailable from NextAuth's perspective. I receive the [next-auth][warn][NEXTAUTH_URL] warning stating Environment variable NEXTAUTH_URL missing. Please set it in your .env file. I'm not sure why that would be the case.

How can I confirm that my env variables have been applied to my SSR Lambdas? I'm using Amplify Hosting in web compute mode. These instructions for web dynamic deployments used to be the steps I'd follow to find my lambda IDs.

api/auth/[...nextauth].js

import NextAuth from 'next-auth';
// ... imports

export const authOptions = {
  debug: process.env.NODE_ENV !== 'production',
  providers: [
    TwitterProvider({
      clientId: process.env.TWITTER_CLIENT_ID,  // Correctly set
      clientSecret: process.env.TWITTER_CLIENT_SECRET,  // Correctly set
      version: '2.0'
    }),
    // ...more providers here
  ],
  secret: process.env.NEXTAUTH_SECRET  // Correctly set
  // ... other config here
};

console.log("Auth URL: ", process.env.NEXTAUTH_URL);   // Correctly logs (see CloudWatch logs below)

export default NextAuth(authOptions);

CloudWatch Logs

image

Amplify Console Env Variables

image

next.config.js

/** @type {import('next').NextConfig} */

const nextConfig = {
  reactStrictMode: true,
  async redirects() {
    // ...
  },
  async rewrites() {
    // ...
  },
  compiler: {
    // ...
  },
  env: {
    // ...
    NEXT_PUBLIC_NEXTAUTH_URL: process.env.NEXTAUTH_URL,
    NEXTAUTH_URL: process.env.NEXTAUTH_URL,
    NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
    // ...
  },
  swcMinify: true,
};

module.exports = () => {
  const plugins = [withSvgr, withBundleAnalyzer, withTM];

  return plugins.reduce((acc, plugin) => plugin(acc), {
    ...nextConfig,
  });
};

amplify.yml

version: 1
applications:
  - backend:
      phases:
        build:
          commands:
            - cd ..
            - amplifyPush --simple
            - cd web
    frontend:
      phases:
        preBuild:
          commands:
            - yarn
        build:
          commands:
            - yarn build
            - echo "NEXTAUTH_URL=$NEXTAUTH_URL" >> .env
            - echo "NEXTAUTH_SECRET=$NEXTAUTH_SECRET" >> .env
            - echo "NEXTAUTH_URL=$NEXTAUTH_URL" >> .env.production
            - echo "NEXTAUTH_SECRET=$NEXTAUTH_SECRET" >> .env.production
      artifacts:
        baseDirectory: .next
        files:
          - '**/*'
      # cache:
      #   paths:
      #     - 'node_modules/**/*' # Cache `node_modules` for faster `yarn` or `npm i`
      #     - '.next/cache/**/*' # Cache Next.js for faster application rebuilds
    appRoot: web

Questions:

  • How can I confirm that my env variables have been applied to my SSR Lambdas now that I'm using Hosting Compute?

I appreciate any sanity checking as well. This has been driving me crazy. 🥲

@foobarnes foobarnes added the question Further information is requested label Mar 3, 2023
@foobarnes foobarnes changed the title How can I confirm that env variables are being added to Hosting Compute? How can I confirm that env variables are being added to Hosting Lambdas (Web Compute)? Mar 3, 2023
@ghost ghost added the compute label Mar 7, 2023
@ghost
Copy link

ghost commented Mar 7, 2023

Hi @foobarnes 👋🏽 thanks for providing this detailed information. You mentioned in your CloudWatch logs that the Lambda function is reporting the correct environment variable. Where are you running into the Environment variable NEXTAUTH_URL missing, could you provide a screenshot of that as well?

I'm continuing to investigate this issue and will update with more information shortly.

@ghost ghost added the investigating label Mar 7, 2023
@ghost
Copy link

ghost commented Jun 2, 2023

According to our documentation:

To make specific environment variables accessible to Next.js, you can modify the Amplify build specification file to set them in the environment files that Next.js recognizes. Amplify needs to be able to load these environment variables before it builds the application. The following build specification example demonstrates how to add environment variables in the build commands section.

You are following the correct steps for making those variables accessible at runtime. Feel free to create a new issue if you run into issues with this behavior.

@ghost ghost closed this as completed Jun 2, 2023
@github-actions
Copy link

github-actions bot commented Jun 2, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions
Copy link

github-actions bot commented Jun 2, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot added the archived This issue has been locked. label Jun 2, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 2, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived This issue has been locked. compute investigating question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant