amplify-backend icon indicating copy to clipboard operation
amplify-backend copied to clipboard

Using secret() in lambda causes "window is not defined"

Open estateservicinghub opened this issue 7 months ago • 3 comments

Environment information

System:
  OS: Linux 6.8 Linux Mint 22 (Wilma)
  CPU: (12) x64 AMD Ryzen 5 7600X 6-Core Processor
  Memory: 40.65 GB / 61.88 GB
  Shell: /bin/bash
Binaries:
  Node: 22.14.0 - ~/.nvm/versions/node/v22.14.0/bin/node
  Yarn: undefined - undefined
  npm: 10.9.2 - ~/.nvm/versions/node/v22.14.0/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.1
  @aws-amplify/backend: 1.5.0
  @aws-amplify/backend-auth: 1.2.0
  @aws-amplify/backend-cli: 1.2.9
  @aws-amplify/backend-data: 1.1.4
  @aws-amplify/backend-deployer: 1.1.5
  @aws-amplify/backend-function: 1.7.0
  @aws-amplify/backend-output-schemas: 1.3.0
  @aws-amplify/backend-output-storage: 1.1.2
  @aws-amplify/backend-secret: 1.1.4
  @aws-amplify/backend-storage: 1.2.1
  @aws-amplify/cli-core: 1.1.3
  @aws-amplify/client-config: 1.4.0
  @aws-amplify/deployed-backend-client: 1.4.2
  @aws-amplify/form-generator: 1.0.3
  @aws-amplify/model-generator: 1.0.8
  @aws-amplify/platform-core: 1.1.0
  @aws-amplify/plugin-types: 1.3.0
  @aws-amplify/sandbox: 1.2.3
  @aws-amplify/schema-generator: 1.2.4
  aws-amplify: 6.14.2
  aws-cdk: 2.163.1
  aws-cdk-lib: 2.163.1
  typescript: 5.6.3
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Describe the bug

I created the sample lambda function per https://docs.amplify.aws/javascript/build-a-backend/add-aws-services/rest-api/set-up-rest-api/, with some modification, but just returning the string per the example worked fine.

Then I modified it to return the value of a secret, using secret() per https://docs.amplify.aws/javascript/deploy-and-host/sandbox-environments/features/. This is failing with 'window is not defined' in the logs.

Reproduction steps

The contents of functions/my-function/handler.ts is:

import type { APIGatewayProxyHandler } from "aws-lambda" ;
import { secret } from "@aws-amplify/backend";

export const handler: APIGatewayProxyHandler = async (event) => {
  console.log("event", event);

  return {
    statusCode: 200,
    // Modify the CORS settings below to match your specific requirements
    headers: {
      "Access-Control-Allow-Origin": "*", // Restrict this to domains you trust
      "Access-Control-Allow-Headers": "*", // Specify only the headers you need to allow
    },
    body: JSON.stringify( secret( "TESTING" ) ),
  };
};``

This resulted in a CORS error on the console, and the following in the function log:

[proto-rest-api] 5:21:49 PM 2025-04-18T00:21:49.988Z undefined ERROR Uncaught Exception {"errorType":"ReferenceError","errorMessage":"window is not defined","stack":["ReferenceError: window is not defined"," at t.default (file:///var/task/index.mjs:2640:4646)"," at Object.<anonymous> (file:///var/task/index.mjs:2636:4610)"," at r (file:///var/task/index.mjs:2636:3398)"," at Object.<anonymous> (file:///var/task/index.mjs:2636:3553)"," at r (file:///var/task/index.mjs:2636:3398)"," at file:///var/task/index.mjs:2636:3476"," at file:///var/task/index.mjs:2636:3481"," at file:///var/task/index.mjs:2636:3148"," at file:///var/task/index.mjs:2636:3261"," at file:///var/task/index.mjs:2:501"]}

estateservicinghub avatar Apr 18 '25 00:04 estateservicinghub