Skip to content

Commit 318335d

Browse files
authored
ensure resource access env vars are added to typed shim files (#1091)
1 parent 7857f0a commit 318335d

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed

.changeset/modern-terms-stare.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/integration-tests': patch
3+
'@aws-amplify/backend-function': patch
4+
---
5+
6+
Ensure resource access env vars are added to function typed shim files

packages/backend-function/src/function_env_type_generator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void describe('FunctionEnvironmentTypeGenerator', () => {
3636
mock.restoreAll();
3737
});
3838

39-
void it('generates a type definition file with dynamic environment variables', () => {
39+
void it('generates a type definition file with Amplify backend environment variables', () => {
4040
const fdCloseMock = mock.fn();
4141
const fsOpenSyncMock = mock.method(fs, 'openSync');
4242
const fsWriteFileSyncMock = mock.method(fs, 'writeFileSync', () => null);

packages/backend-function/src/function_env_type_generator.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export class FunctionEnvironmentTypeGenerator {
3636
}
3737

3838
// Add Lambda runtime environment variables to the typed shim
39+
declarations.push(
40+
`/** Lambda runtime environment variables, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime */`
41+
);
3942
declarations.push(`type ${lambdaEnvVarTypeName} = {`);
4043
for (const key in staticEnvironmentVariables) {
4144
const comment = `/** ${staticEnvironmentVariables[key]} */`;
@@ -50,6 +53,9 @@ export class FunctionEnvironmentTypeGenerator {
5053
* 1. Defined by the customer passing env vars to the environment parameter for defineFunction
5154
* 2. Defined by resource access mechanisms
5255
*/
56+
declarations.push(
57+
`/** Amplify backend environment variables available at runtime, this includes environment variables defined in \`defineFunction\` and by cross resource mechanisms */`
58+
);
5359
declarations.push(`type ${amplifyBackendEnvVarTypeName} = {`);
5460
this.amplifyBackendEnvVars.forEach((envName) => {
5561
const declaration = `${envName}: string;`;
@@ -58,11 +64,9 @@ export class FunctionEnvironmentTypeGenerator {
5864
});
5965
declarations.push(`};${EOL}`);
6066

61-
const content =
62-
`/** Lambda runtime environment variables, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime */${EOL}` +
63-
`export const env = process.env as ${lambdaEnvVarTypeName} & ${amplifyBackendEnvVarTypeName};${EOL}${EOL}${declarations.join(
64-
EOL
65-
)}`;
67+
const content = `export const env = process.env as ${lambdaEnvVarTypeName} & ${amplifyBackendEnvVarTypeName};${EOL}${EOL}${declarations.join(
68+
EOL
69+
)}`;
6670

6771
fs.writeFileSync(this.typeDefFilePath, content);
6872
}

packages/integration-tests/src/test-projects/data-storage-auth-with-triggers-ts/.amplify/function-env/defaultNodeFunction.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* This file is here to make Typescript happy for initial type checking and will be overwritten when tests run
33
*/
4-
/** Lambda runtime environment variables, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime */
5-
export const env = process.env as LambdaProvidedEnvVars & DefinedEnvVars;
4+
export const env = process.env as LambdaProvidedEnvVars & AmplifyBackendEnvVars;
65

6+
/** Lambda runtime environment variables, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime */
77
type LambdaProvidedEnvVars = {
88
/** The handler location configured on the function. */
99
_HANDLER: string;
@@ -81,7 +81,9 @@ type LambdaProvidedEnvVars = {
8181
TZ: string;
8282
};
8383

84-
type DefinedEnvVars = {
84+
/** Amplify backend environment variables available at runtime, this includes environment variables defined in `defineFunction` and by cross resource mechanisms */
85+
type AmplifyBackendEnvVars = {
8586
TEST_SECRET: string;
8687
TEST_SHARED_SECRET: string;
88+
testName_BUCKET_NAME: string;
8789
};

packages/integration-tests/src/test-projects/data-storage-auth-with-triggers-ts/amplify/func-src/response_generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Amplify.configure(
1111
{
1212
Storage: {
1313
S3: {
14-
bucket: process.env.testName_BUCKET_NAME,
14+
bucket: env.testName_BUCKET_NAME,
1515
region: env.AWS_REGION,
1616
},
1717
},

0 commit comments

Comments
 (0)