Skip to content

Commit e008bb3

Browse files
author
Kamil Sobol
authored
lazy load ssm (#1522)
* lazy load ssm * lazy load ssm * fix test * fix test
1 parent 9e24bcc commit e008bb3

File tree

8 files changed

+47
-6
lines changed

8 files changed

+47
-6
lines changed

.changeset/afraid-cameras-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-function': patch
3+
---
4+
5+
fix: lazy load ssm client in backend function banner

packages/backend-function/src/lambda-shims/resolve_ssm_params.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
* This code loads environment values from SSM and places them in their corresponding environment variables.
33
* If there are no SSM environment values for this function, this is a noop.
44
*/
5-
import { SSM } from '@aws-sdk/client-ssm';
5+
import type { SSM } from '@aws-sdk/client-ssm';
66
import type { SsmEnvVars } from '../function_env_translator.js';
77

88
/**
99
* Reads SSM environment context from a known Amplify environment variable,
1010
* fetches values from SSM and places those values in the corresponding environment variables
1111
*/
12-
export const internalAmplifyFunctionResolveSsmParams = async (
13-
client = new SSM()
14-
) => {
12+
export const internalAmplifyFunctionResolveSsmParams = async (client?: SSM) => {
1513
const envPathObject: SsmEnvVars = JSON.parse(
1614
process.env.AMPLIFY_SSM_ENV_CONFIG ?? '{}'
1715
);
@@ -21,8 +19,16 @@ export const internalAmplifyFunctionResolveSsmParams = async (
2119
return;
2220
}
2321

22+
let actualSsmClient: SSM;
23+
if (client) {
24+
actualSsmClient = client;
25+
} else {
26+
const ssmSdk = await import('@aws-sdk/client-ssm');
27+
actualSsmClient = new ssmSdk.SSM();
28+
}
29+
2430
const resolveSecrets = async (paths: string[]) => {
25-
const response = await client.getParameters({
31+
const response = await actualSsmClient.getParameters({
2632
Names: paths,
2733
WithDecryption: true,
2834
});

packages/integration-tests/src/test-in-memory/data_storage_auth_with_triggers.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void it('data storage auth with triggers', () => {
5252
assertExpectedLogicalIds(templates.defaultNodeFunc, 'AWS::Lambda::Function', [
5353
'defaultNodeFunctionlambda5C194062',
5454
'echoFunclambdaE17DCA46',
55+
'funcWithSsmlambda6A8824A1',
5556
'handler2lambda1B9C7EFF',
5657
'node16Functionlambda97ECC775',
5758
'onUploadlambdaA252C959',

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,15 @@ class DataStorageAuthWithTriggerTestProject extends TestProjectBase {
194194
(name) => name.includes('node16Function')
195195
);
196196

197+
const funcWithSsm = await this.resourceFinder.findByBackendIdentifier(
198+
backendId,
199+
'AWS::Lambda::Function',
200+
(name) => name.includes('funcWithSsm')
201+
);
202+
197203
assert.equal(defaultNodeLambda.length, 1);
198204
assert.equal(node16Lambda.length, 1);
205+
assert.equal(funcWithSsm.length, 1);
199206

200207
const expectedResponse = {
201208
s3TestContent: 'this is some test content',
@@ -206,6 +213,7 @@ class DataStorageAuthWithTriggerTestProject extends TestProjectBase {
206213

207214
await this.checkLambdaResponse(defaultNodeLambda[0], expectedResponse);
208215
await this.checkLambdaResponse(node16Lambda[0], expectedResponse);
216+
await this.checkLambdaResponse(funcWithSsm[0], 'It is working');
209217

210218
const bucketName = await this.resourceFinder.findByBackendIdentifier(
211219
backendId,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { SSM } from '@aws-sdk/client-ssm';
2+
3+
/**
4+
* This function asserts that customer can import and use SSM client.
5+
* I.e. asserts that the banner we inject doesn't prevent that use case.
6+
*/
7+
export const handler = async () => {
8+
new SSM();
9+
return 'It is working';
10+
};

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ export const onUpload = defineFunction({
3434
name: 'onUpload',
3535
entry: './func-src/handler.ts',
3636
});
37+
38+
export const funcWithSsm = defineFunction({
39+
name: 'funcWithSsm',
40+
entry: './func-src/handler_with_ssm.ts',
41+
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { data } from './data/resource.js';
2-
import { defaultNodeFunc, node16Func } from './function.js';
2+
import { defaultNodeFunc, funcWithSsm, node16Func } from './function.js';
33
import { storage } from './storage/resource.js';
44
import { auth } from './auth/resource.js';
55

@@ -9,4 +9,5 @@ export const dataStorageAuthWithTriggers = {
99
defaultNodeFunc,
1010
data,
1111
node16Func,
12+
funcWithSsm,
1213
};

packages/integration-tests/src/test-projects/data-storage-auth-with-triggers-ts/hotswap-update-files/function.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ export const onUpload = defineFunction({
3636
name: 'onUpload',
3737
entry: './func-src/handler.ts',
3838
});
39+
40+
export const funcWithSsm = defineFunction({
41+
name: 'funcWithSsm',
42+
entry: './func-src/handler_with_ssm.ts',
43+
});

0 commit comments

Comments
 (0)