Skip to content

Commit 7f5edee

Browse files
authored
ensure typed shim files contain only the function name (#1108)
* ensure typed shim files contain only the function name * change check to onDelete * refactor FunctionEnvironmentTypeGenerator * small nit
1 parent f999897 commit 7f5edee

File tree

7 files changed

+42
-22
lines changed

7 files changed

+42
-22
lines changed

.changeset/smart-crews-serve.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 typed shim files contain only the function name

packages/backend-function/src/factory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
FunctionOutput,
2727
functionOutputKey,
2828
} from '@aws-amplify/backend-output-schemas';
29+
import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.js';
2930

3031
/**
3132
* Entry point for defining a function in the Amplify ecosystem
@@ -292,7 +293,8 @@ class AmplifyFunction
292293
this.functionEnvironmentTranslator = new FunctionEnvironmentTranslator(
293294
functionLambda,
294295
props.environment,
295-
backendSecretResolver
296+
backendSecretResolver,
297+
new FunctionEnvironmentTypeGenerator(id)
296298
);
297299

298300
this.resources = {

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import assert from 'node:assert';
1212
import { ParameterPathConversions } from '@aws-amplify/platform-core';
1313
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
1414
import { Template } from 'aws-cdk-lib/assertions';
15+
import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.js';
1516

1617
const testStack = {} as Construct;
1718

@@ -21,6 +22,8 @@ const testBackendIdentifier: BackendIdentifier = {
2122
type: 'branch',
2223
};
2324

25+
const testLambdaName = 'testFunction';
26+
2427
class TestBackendSecretResolver implements BackendSecretResolver {
2528
resolveSecret = (backendSecret: BackendSecret): SecretValue => {
2629
return backendSecret.resolve(testStack, testBackendIdentifier);
@@ -62,7 +65,8 @@ void describe('FunctionEnvironmentTranslator', () => {
6265
new FunctionEnvironmentTranslator(
6366
testLambda,
6467
functionEnvProp,
65-
backendResolver
68+
backendResolver,
69+
new FunctionEnvironmentTypeGenerator(testLambdaName)
6670
);
6771

6872
const template = Template.fromStack(Stack.of(testLambda));
@@ -88,7 +92,8 @@ void describe('FunctionEnvironmentTranslator', () => {
8892
new FunctionEnvironmentTranslator(
8993
testLambda,
9094
functionEnvProp,
91-
backendResolver
95+
backendResolver,
96+
new FunctionEnvironmentTypeGenerator(testLambdaName)
9297
);
9398

9499
const template = Template.fromStack(Stack.of(testLambda));
@@ -121,7 +126,8 @@ void describe('FunctionEnvironmentTranslator', () => {
121126
new FunctionEnvironmentTranslator(
122127
getTestLambda(),
123128
functionEnvProp,
124-
backendResolver
129+
backendResolver,
130+
new FunctionEnvironmentTypeGenerator(testLambdaName)
125131
)
126132
);
127133
});
@@ -136,7 +142,8 @@ void describe('FunctionEnvironmentTranslator', () => {
136142
new FunctionEnvironmentTranslator(
137143
testLambda,
138144
functionEnvProp,
139-
backendResolver
145+
backendResolver,
146+
new FunctionEnvironmentTypeGenerator(testLambdaName)
140147
);
141148

142149
const template = Template.fromStack(Stack.of(testLambda));
@@ -154,7 +161,8 @@ void describe('FunctionEnvironmentTranslator', () => {
154161
new FunctionEnvironmentTranslator(
155162
testLambda,
156163
functionEnvProp,
157-
backendResolver
164+
backendResolver,
165+
new FunctionEnvironmentTypeGenerator(testLambdaName)
158166
);
159167

160168
const template = Template.fromStack(Stack.of(testLambda));
@@ -216,7 +224,7 @@ void describe('FunctionEnvironmentTranslator', () => {
216224
});
217225

218226
const getTestLambda = () =>
219-
new Function(new Stack(new App()), 'testFunction', {
227+
new Function(new Stack(new App()), testLambdaName, {
220228
code: Code.fromInline('test code'),
221229
runtime: Runtime.NODEJS_20_X,
222230
handler: 'handler',

packages/backend-function/src/function_env_translator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export class FunctionEnvironmentTranslator {
2626
constructor(
2727
private readonly lambda: NodejsFunction, // we need to use a specific type here so that we have all the method goodies
2828
private readonly functionEnvironmentProp: Required<FunctionProps>['environment'],
29-
private readonly backendSecretResolver: BackendSecretResolver
29+
private readonly backendSecretResolver: BackendSecretResolver,
30+
private readonly functionEnvironmentTypeGenerator: FunctionEnvironmentTypeGenerator
3031
) {
3132
for (const [key, value] of Object.entries(this.functionEnvironmentProp)) {
3233
if (key === this.amplifySsmEnvConfigKey) {
@@ -86,10 +87,9 @@ export class FunctionEnvironmentTranslator {
8687
// Using CDK validation mechanism as a way to generate a typed process.env shim file at the end of synthesis
8788
this.lambda.node.addValidation({
8889
validate: (): string[] => {
89-
new FunctionEnvironmentTypeGenerator(
90-
this.lambda.node.id,
90+
this.functionEnvironmentTypeGenerator.generateTypedProcessEnvShim(
9191
this.amplifyBackendEnvVarNames
92-
).generateTypedProcessEnvShim();
92+
);
9393
return [];
9494
},
9595
});

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void describe('FunctionEnvironmentTypeGenerator', () => {
1919
new FunctionEnvironmentTypeGenerator('testFunction');
2020
const sampleStaticEnv = '_HANDLER: string;';
2121

22-
functionEnvironmentTypeGenerator.generateTypedProcessEnvShim();
22+
functionEnvironmentTypeGenerator.generateTypedProcessEnvShim([]);
2323

2424
// assert type definition file path
2525
assert.equal(
@@ -46,10 +46,10 @@ void describe('FunctionEnvironmentTypeGenerator', () => {
4646
};
4747
});
4848
const functionEnvironmentTypeGenerator =
49-
new FunctionEnvironmentTypeGenerator('testFunction', ['TEST_ENV']);
49+
new FunctionEnvironmentTypeGenerator('testFunction');
5050
const sampleStaticEnv = 'TEST_ENV: string;';
5151

52-
functionEnvironmentTypeGenerator.generateTypedProcessEnvShim();
52+
functionEnvironmentTypeGenerator.generateTypedProcessEnvShim(['TEST_ENV']);
5353

5454
// assert type definition file path
5555
assert.equal(
@@ -69,10 +69,10 @@ void describe('FunctionEnvironmentTypeGenerator', () => {
6969
void it('generated type definition file has valid syntax', async () => {
7070
const targetDirectory = await fsp.mkdtemp('func_env_type_gen_test');
7171
const functionEnvironmentTypeGenerator =
72-
new FunctionEnvironmentTypeGenerator('testFunction', ['TEST_ENV']);
72+
new FunctionEnvironmentTypeGenerator('testFunction');
7373
const filePath = `${process.cwd()}/.amplify/function-env/testFunction.ts`;
7474

75-
functionEnvironmentTypeGenerator.generateTypedProcessEnvShim();
75+
functionEnvironmentTypeGenerator.generateTypedProcessEnvShim(['TEST_ENV']);
7676

7777
// import to validate syntax of type definition file
7878
await import(pathToFileURL(filePath).toString());

packages/backend-function/src/function_env_type_generator.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ export class FunctionEnvironmentTypeGenerator {
1212
/**
1313
* Initialize typed process.env shim file name and location
1414
*/
15-
constructor(
16-
private readonly functionName: string,
17-
private readonly amplifyBackendEnvVars: string[] = []
18-
) {
15+
constructor(private readonly functionName: string) {
1916
this.typeDefFilePath = `${process.cwd()}/.amplify/function-env/${
2017
this.functionName
2118
}.ts`;
@@ -24,7 +21,7 @@ export class FunctionEnvironmentTypeGenerator {
2421
/**
2522
* Generate a typed process.env shim
2623
*/
27-
generateTypedProcessEnvShim() {
24+
generateTypedProcessEnvShim(amplifyBackendEnvVars: string[]) {
2825
const lambdaEnvVarTypeName = 'LambdaProvidedEnvVars';
2926
const amplifyBackendEnvVarTypeName = 'AmplifyBackendEnvVars';
3027

@@ -57,7 +54,7 @@ export class FunctionEnvironmentTypeGenerator {
5754
`/** Amplify backend environment variables available at runtime, this includes environment variables defined in \`defineFunction\` and by cross resource mechanisms */`
5855
);
5956
declarations.push(`type ${amplifyBackendEnvVarTypeName} = {`);
60-
this.amplifyBackendEnvVars.forEach((envName) => {
57+
amplifyBackendEnvVars.forEach((envName) => {
6158
const declaration = `${envName}: string;`;
6259

6360
declarations.push(declaration);

packages/integration-tests/src/test-project-setup/data_storage_auth_with_triggers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ class DataStorageAuthWithTriggerTestProject extends TestProjectBase {
236236
backendId,
237237
'AWS::IAM::Role'
238238
);
239+
240+
// ensure typed shim files are generated by checking for onDelete's typed shim
241+
const typedShimStats = await fs.stat(
242+
path.join(this.projectDirPath, '.amplify', 'function-env', 'onDelete.ts')
243+
);
244+
245+
assert.ok(typedShimStats.isFile());
239246
}
240247

241248
private setUpDeployEnvironment = async (

0 commit comments

Comments
 (0)