I'm looking for an explanation of the Amplify Gen 2 secret function.
As far as i can make out from the documentation:
https://docs.amplify.aws/react/deploy-and-host/fullstack-branching/secrets-and-vars/
Secrets can be set in the Amplify Web console per branch or shared for all branches
Secrets that are being used in a sandbox environment need to be set via npx ampx sandbox terminal command.
Once secrets are set they can be accessed via the function:
secret("NAME OF SECRET");
but only in the backend files in the amplify folder.
Specifically the resource.ts files like this:
export const processOrderHandler = defineFunction({
// optionally specify a name for the Function (defaults to directory name)
name: "process-order-handler",
// optionally specify a path to your handler (defaults to "./handler.ts")
entry: "./handler.ts",
// add environment variables
environment: {
STRIPE_SECRET_KEY: secret("STRIPE_SECRET_KEY"), // Secrets must be set for each branch via Amplify web console and via Terminal for Sandbox environments.
TEST_SECRET: secret("TEST_SECRET")
},
});
Then the secrets can be accessed via the
import { env } from "$amplify/env/process-order-handler";
env.STRIPE_SECRET_KEY;
env.TEST_SECRET;
My question is how can i use a similar approach to the above to access a secret in NextJS SSR functions.
I have server code in locations like:
/app/api/route.js
and some server side functions in standard pages:
/app/contact/page.js
I can't access the secret() function in any NextJS code without running errors. And even if i manage to get it working without errors, I can only get back a BackendSecret type when calling this function.
I want to maintain the same approach to all secrets handling and store them all via SSM if possible.
NextJS docs suggest the .env file with NEXT_PUBLIC_ for public variables and all others are treated as server only and secure.
But this isn't safe using git tracking.