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

add `addEnvironment` for custom function

Open ykethan opened this issue 10 months ago • 2 comments

Environment information

System:
  OS: macOS 15.2
  CPU: (8) arm64 Apple M1
  Memory: 172.80 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.18.1 - ~/.nvm/versions/node/v20.18.1/bin/node
  Yarn: 1.22.22 - /opt/homebrew/bin/yarn
  npm: 10.8.2 - ~/.nvm/versions/node/v20.18.1/bin/npm
  pnpm: 9.15.2 - ~/Library/pnpm/pnpm
NPM Packages:
  @aws-amplify/auth-construct: 1.6.0
  @aws-amplify/backend: 1.14.0
  @aws-amplify/backend-auth: 1.5.0
  @aws-amplify/backend-cli: 1.4.8
  @aws-amplify/backend-data: 1.4.0
  @aws-amplify/backend-deployer: 1.1.15
  @aws-amplify/backend-function: 1.12.1
  @aws-amplify/backend-output-schemas: 1.4.0
  @aws-amplify/backend-output-storage: 1.1.4
  @aws-amplify/backend-secret: 1.1.5
  @aws-amplify/backend-storage: 1.2.4
  @aws-amplify/cli-core: 1.2.3
  @aws-amplify/client-config: 1.5.5
  @aws-amplify/deployed-backend-client: 1.5.0
  @aws-amplify/form-generator: 1.0.3
  @aws-amplify/model-generator: 1.0.12
  @aws-amplify/platform-core: 1.6.0
  @aws-amplify/plugin-types: 1.8.0
  @aws-amplify/sandbox: 1.2.10
  @aws-amplify/schema-generator: 1.2.7
  aws-amplify: 6.12.1
  aws-cdk: 2.176.0
  aws-cdk-lib: 2.176.0
  typescript: 5.7.3
No AWS environment variables
No CDK environment variables

Describe the feature

currently to add environment variables such as GraphQL API endpoint you need to access the environment property using

const env = {
  variables: {
    APPSYNC_API_ENDPOINT: backend.data.graphqlUrl,
  },
};
backend.customAPIFunction.resources.cfnResources.cfnFunction.environment = env;

but adding this would override any env variables directly set on the function, to add the GraphQL url or reference other resources you need to override the property

// backend.ts
const cfnFn = backend.customAPIFunction.resources.cfnResources.cfnFunction;

cfnFn.addPropertyOverride("Environment.Variables", {
  // @ts-expect-error
  ...((cfnFn.environment?.variables || {}) as Record<string, string>),
  KEY3: "value3", // Add new vars
  KEY4: "value4",
});


Use case

keep existing environment variables present on the custom function created in the defineFunction and extend them using addEnvironment on backend.customAPIFunction.addEnvironment

ykethan avatar Jan 31 '25 23:01 ykethan