Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added lambdaRegion option to config options #175

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' });
});
Loading