Skip to content

Commit

Permalink
feat: added lambdaRegion option to config options
Browse files Browse the repository at this point in the history
this will let the consumer decide which region to invoke on the target function
if trying network request across regions, the user must currently pass the entire Lambda class in just to configure this one variable
  • Loading branch information
alice-byb committed Aug 16, 2024
1 parent 4477b67 commit 9921578
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/adapters/lambda-invocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ import { AbortController } from '@aws-sdk/abort-controller';
const lambdaInvocationAdapter: AlphaAdapter = async (config) => {
const LambdaClass = config.Lambda || Lambda;
const lambdaOptions: LambdaClientConfig = {
region: config.lambdaRegion,
endpoint: config.lambdaEndpoint || process.env.LAMBDA_ENDPOINT,
};

if (config.lambdaRegion) {
// Allow the region to be controlled via config
lambdaOptions.region = config.lambdaRegion;
}

if (config.timeout) {
// Set some low level HTTP client timeout options
// so that the system level resources will be
Expand Down
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AxiosPromise, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
import type { Lambda } from '@aws-sdk/client-lambda';
import type { Lambda, LambdaClientConfig } from '@aws-sdk/client-lambda';
import type { Context, Handler } from 'aws-lambda';
import { SignatureV4CryptoInit, SignatureV4Init } from '@aws-sdk/signature-v4';

Expand All @@ -26,6 +26,10 @@ export interface AlphaOptions<D = any> extends AxiosRequestConfig<D> {
* (Optional) The AWS endpoint to use when invoking the target Lambda function.
*/
lambdaEndpoint?: string;
/**
* (Optional) The AWS region to use when invoking the target Lambda function.
*/
lambdaRegion?: LambdaClientConfig['region'];
Lambda?: typeof Lambda;
}

Expand Down
21 changes: 21 additions & 0 deletions test/lambda-invocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,24 @@ test('lambdaEndpoint config option is provided to the Lambda client', async () =

expect(FakeLambda).toHaveBeenCalledWith({ endpoint: 'http://test-endpoint' });
});

test('lambdaRegion config option is provided to the Lambda client', async () => {
const alpha = new Alpha('lambda://test-function', {
lambdaRegion: 'ap-southeast-2',
Lambda: FakeLambda,
});
createResponse(mockLambda, {
StatusCode: 200,
Payload: {
body: 'test',
statusCode: 200,
},
});

const response = await alpha.get('/test');

expect(response.data).toBe('test');
expect(response.status).toBe(200);

expect(FakeLambda).toHaveBeenCalledWith({ region: 'ap-southeast-2' });
});

0 comments on commit 9921578

Please sign in to comment.