UNPKG

@gp_jcisneros/aws-utils

Version:

AWS SDK utilities for GreenPay microservices

107 lines (91 loc) 3.71 kB
const { LambdaClient, InvokeCommand } = require('@aws-sdk/client-lambda'); const { AWSError, IntegrationError } = require('@gp_jcisneros/errors'); /** * Utilities for AWS Lambda operations */ class LambdaUtils { constructor(region = process.env.AWS_REGION || 'us-east-1') { this.client = new LambdaClient({ region }); } /** * Invoke a Lambda function * @param {string} functionName - Name of the Lambda function * @param {Object} payload - Payload to send to the function * @param {string} invocationType - 'RequestResponse' or 'Event' (default: 'RequestResponse') * @returns {Promise<Object>} - Response from the Lambda function */ async invokeLambda(functionName, payload, invocationType = 'RequestResponse') { try { const command = new InvokeCommand({ FunctionName: functionName, InvocationType: invocationType, Payload: JSON.stringify(payload) }); const response = await this.client.send(command); if (invocationType === 'RequestResponse') { const responsePayload = JSON.parse(Buffer.from(response.Payload).toString()); // Check if Lambda returned an error if (responsePayload.errorMessage) { throw new IntegrationError(`Lambda execution failed: ${responsePayload.errorMessage}`, 'LAMBDA_EXECUTION_ERROR', 'lambda', { functionName, errorMessage: responsePayload.errorMessage }); } return responsePayload; } return { statusCode: response.StatusCode, requestId: response.ResponseMetadata?.RequestId }; } catch (error) { // Si ya es un error personalizado, lo relanzamos if (error instanceof AWSError || error instanceof IntegrationError) { throw error; } throw new AWSError(`Lambda invocation failed for ${functionName}: ${error.message}`, 'LAMBDA', 'INVOCATION_ERROR'); } } /** * Invoke a Lambda function asynchronously * @param {string} functionName - Name of the Lambda function * @param {Object} payload - Payload to send to the function * @returns {Promise<Object>} - Response metadata */ async invokeLambdaAsync(functionName, payload) { return this.invokeLambda(functionName, payload, 'Event'); } /** * Get Lambda function configuration * @param {string} functionName - Name of the Lambda function * @returns {Promise<Object>} - Function configuration */ async getFunctionConfiguration(functionName) { try { const { GetFunctionConfigurationCommand } = require('@aws-sdk/client-lambda'); const command = new GetFunctionConfigurationCommand({ FunctionName: functionName }); return await this.client.send(command); } catch (error) { // Si ya es un error personalizado, lo relanzamos if (error instanceof AWSError || error instanceof IntegrationError) { throw error; } throw new AWSError(`Failed to get function configuration for ${functionName}: ${error.message}`, 'LAMBDA', 'CONFIG_ERROR'); } } // Static methods for convenience static async invokeLambda(functionName, payload, invocationType = 'RequestResponse') { const utils = new LambdaUtils(); return utils.invokeLambda(functionName, payload, invocationType); } static async invokeLambdaAsync(functionName, payload) { const utils = new LambdaUtils(); return utils.invokeLambdaAsync(functionName, payload); } static async getFunctionConfiguration(functionName) { const utils = new LambdaUtils(); return utils.getFunctionConfiguration(functionName); } } module.exports = { LambdaUtils };