tezda-ip-whitelist-cli
Version:
CLI tool to update IP whitelist via AWS Lambda
57 lines (56 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.invokeLambda = exports.createLambdaClient = void 0;
const client_lambda_1 = require("@aws-sdk/client-lambda");
/**
* Creates a LambdaClient with the specified region and profile.
*
* @param region - AWS region.
* @param profile - AWS profile name.
* @returns Configured LambdaClient instance.
*/
const createLambdaClient = (region, profile) => {
const { fromIni } = require('@aws-sdk/credential-providers');
const clientConfig = { region };
if (profile) {
clientConfig.credentials = fromIni({ profile });
}
return new client_lambda_1.LambdaClient(clientConfig);
};
exports.createLambdaClient = createLambdaClient;
/**
* Invokes an AWS Lambda function with the given payload.
*
* @param lambdaClient - An instance of LambdaClient.
* @param functionName - The name of the Lambda function to invoke.
* @param payload - The payload to send to the Lambda function.
* @param invocationType - The type of invocation (default is 'RequestResponse').
* @returns The response from the Lambda function.
*/
const invokeLambda = async (lambdaClient, functionName, payload, invocationType = 'RequestResponse') => {
const params = {
FunctionName: functionName,
Payload: Buffer.from(JSON.stringify(payload)),
InvocationType: invocationType,
};
const command = new client_lambda_1.InvokeCommand(params);
try {
const response = await lambdaClient.send(command);
if (!response.Payload) {
throw new Error('No payload returned from Lambda function.');
}
const payloadString = Buffer.from(response.Payload).toString('utf-8');
const parsedPayload = JSON.parse(payloadString);
if (response.FunctionError) {
console.error(`Lambda function error: ${response.FunctionError}`, parsedPayload);
throw new Error(`Lambda function error: ${response.FunctionError}`);
}
console.log('Lambda response:', parsedPayload);
return parsedPayload;
}
catch (error) {
console.error('Error invoking Lambda function:', error.message || error);
throw error;
}
};
exports.invokeLambda = invokeLambda;