Skip to content

Commit 62edafe

Browse files
authored
add explainer for function env file generation (#7957)
1 parent 8b682d1 commit 62edafe

File tree

1 file changed

+43
-1
lines changed
  • src/pages/[platform]/build-a-backend/functions/environment-variables-and-secrets

1 file changed

+43
-1
lines changed

src/pages/[platform]/build-a-backend/functions/environment-variables-and-secrets/index.mdx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Within your function handler, you can access environment variables using the nor
7272

7373
```ts title="amplify/functions/say-hello/handler.ts"
7474
// highlight-next-line
75-
import { env } from '$amplify/env/say-hello'; // the import is '$amplify/env/<function name>'
75+
import { env } from '$amplify/env/say-hello'; // the import is '$amplify/env/<function-name>'
7676

7777
export const handler = async (event) => {
7878
// the env object has intellisense for all environment variables that are available to the function
@@ -100,6 +100,48 @@ If you did not, you will need to manually configure your project. Within your `a
100100

101101
</Accordion>
102102

103+
### Generated env files
104+
105+
When you configure your function with environment variables or secrets, Amplify's backend tooling generates a file using the function's `name` in `.amplify/generated` with references to your environment variables and secrets, as well as [environment variables predefined by the Lambda runtime](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime). This provides a type-safe experience for working with environment variables that does not require typing `process.env` manually.
106+
107+
<Callout info>
108+
109+
**Note:** generated files are created before deployments when executing `ampx sandbox` or `ampx pipeline-deploy`
110+
111+
</Callout>
112+
113+
For example, if you have a function with the following definition:
114+
115+
```ts title="amplify/functions/say-hello/resource.ts"
116+
import { defineFunction } from "@aws-amplify/backend";
117+
118+
export const sayHello = defineFunction({
119+
name: "say-hello",
120+
environment: {
121+
NAME: "World",
122+
},
123+
});
124+
```
125+
126+
Upon starting your next deployment, Amplify will create a file at the following location:
127+
128+
```
129+
.amplify/generated/env/say-hello.ts
130+
```
131+
132+
Using the TypeScript path alias, `$amplify`, you can import the file in your function's handler:
133+
134+
```ts title="amplify/functions/say-hello/handler.ts"
135+
import { env } from "$amplify/env/say-hello"
136+
137+
export const handler = async (event) => {
138+
// the env object has intellisense for all environment variables that are available to the function
139+
return `Hello, ${env.NAME}!`;
140+
};
141+
```
142+
143+
Encountering issues with this file? [Visit the troubleshooting guide for `Cannot find module $amplify/env/<function-name>`](/[platform]/build-a-backend/troubleshooting/cannot-find-module-amplify-env/)
144+
103145
## Secrets
104146

105147
Sometimes it is necessary to provide a secret value to a function. For example, it may need a database password or an API key to perform some business use case. Environment variables should NOT be used for this because environment variable values are included in plaintext in the function configuration. Instead, secret access can be used.

0 commit comments

Comments
 (0)