The EIP Cloud Services Module is a comprehensive Node.js package that provides seamless integration with various cloud services, including Redis, AWS S3, CDN, AWS Lambda, and MySQL. This module is designed to simplify the complexities of interacting with these cloud services, offering a range of functionalities from data caching and storage to content delivery and serverless computing.
To use this module, first install it in your Node.js project. Then, import the required services as follows:
const { redis, cdn, mysql } = require('eip-cloud-services');
Here is an example configuration for the EIP Cloud Services Module. Replace the placeholder values with your actual configuration details.
const packageJson = require('../package.json');
module.exports = {
cdn: {
['cdnName']: { // e.g. "myCdn"
['envName1']: { //e.g. "production"
type: 'google',
urlMapName: 'YOUR_GCP_URL_MAP_NAME',
projectId: 'YOUR_GCP_PROJECT_NAME',
},
['envName2']: { // e.g. "staging"
type: 'amazon',
distributionId: 'YOUR_DISTRIBUTION_ID',
}
},
},
redis: { // For regular redis connections
port: 'YOUR_REDIS_PORT',
host: 'YOUR_REDIS_HOST',
prefix: 'YOUR_REDIS_PREFIX',
},
redis: { // For redis cluster instances
prefix: 'YOUR_REDIS_PREFIX',
clusterEnabled: true,
cluster: [
{
port: 'YOUR_REDIS_PORT',
host: 'YOUR_REDIS_HOST',
},
{
port: 'YOUR_REDIS_PORT_2',
host: 'YOUR_REDIS_HOST_2',
},
...
]
},
s3: {
Bucket: 'YOUR_S3_BUCKET',
logs: "verbose", // "verbose" or "outputs", any other value will not log. verbose will log all activity. outputs will only log when there is a file updated.
logsFunction: ( message ) => { ... } // Optional, if nothing is provided console.log will be used.
},
mysql: {
connectionLimit: 10, // Max connections
host: 'my-database.domain.com',
user: 'user',
password: 'password',
database: 'my-database', // defaults to undefined
multipleStatements: false // defaults to true if not set
}
};
This module provides an interface for invoking AWS Lambda functions. It simplifies the process of triggering Lambda functions from your Node.js application, with support for both synchronous and asynchronous invocations.
Ensure this module is included in your Node.js project. Import the Lambda component as follows:
const lambda = require('eip-cloud-services/lambda');
You can invoke a Lambda function by specifying the function name and the event payload. The module supports both waiting for the function execution to complete and fire-and-forget invocations.
const response = await lambda.invokeLambda('yourFunctionName', { key: 'value' }, true);
console.log(response);
await lambda.invokeLambda('yourFunctionName', { key: 'value' }, false);
-
functionName
: The name of the Lambda function to invoke. -
eventPayload
: The payload to pass to the function. This can be any valid JSON object. -
waitForExecution
(optional): Set totrue
to wait for the execution to start and receive a response. -
context
(optional): The client context for the function execution. -
invocationType
(optional): Defaults to 'Event'. Can be set to 'RequestResponse' for synchronous execution. -
logType
(optional): The type of logging for the function.
Configure the module with your AWS credentials and Lambda settings.
The module includes error handling to manage issues related to Lambda invocation, such as network errors or configuration problems.
Logging is provided to track the start and completion of Lambda invocations, aiding in debugging and monitoring.
This MySQL module provides a simple and efficient interface for interacting with MySQL databases. It includes functionalities to manage database connections, execute queries, and handle connection pooling.
Ensure this module is included in your Node.js project. Import the MySQL component as follows:
const mysql = require('eip-cloud-services/mysql');
You can execute queries using the query
method. This method automatically handles connections and releases them after query execution.
const results = await mysql.query('SELECT * FROM your_table');
console.log(results);
It's not required for you to get connections manually, you can use the methods directly without having to setup connections. Getting a connection should only be used if you need to parse the connection to a third party module for example.
const pool = mysql.getPool();
const connection = await mysql.getConnection();
To gracefully close all connections in the pool:
await mysql.kill();
Configure your MySQL connection settings (like host, user, password, etc.) in the config
directory.
The module is designed to handle errors gracefully, providing clear error messages for database connection issues and query errors.
- The module uses connection pooling for efficient database interaction.
- The pool is automatically created and managed by the module.
This module provides functionalities to manage and interact with Content Delivery Networks (CDNs) like Amazon CloudFront and Google Cloud CDN. It includes features for creating invalidations, thereby ensuring that the latest content is served to end-users.
Ensure this module is included in your Node.js project. Import the CDN component as follows:
const cdn = require('eip-cloud-services/cdn');
To invalidate cached content in a CDN, use the createInvalidation
method. This method supports invalidating content in both Amazon CloudFront and Google Cloud CDN.
await cdn.createInvalidation('amazon', 'path/to/your/file.jpg', 'production');
await cdn.createInvalidation('google', 'path/to/your/file.jpg', 'production');
You can also invalidate multiple paths by passing an array of keys:
await cdn.createInvalidation('amazon', ['file1.jpg', 'file2.jpg'], 'staging');
Configure the module with your CDN settings in the config
directory. The configuration should include details like project ID, distribution ID, and URL map names for the respective CDNs.
The module is equipped to handle errors gracefully, including validation of input parameters and handling CDN-specific errors.
- Supports both Amazon CloudFront and Google Cloud CDN.
- Validates the key argument to ensure proper formatting.
- Constructs invalidation paths based on the provided keys.
- Initializes Google Auth if Google CDN is used.
- Sends invalidation commands to the CDN client based on the type of CDN and environment.
This module provides an interface for interacting with AWS S3, offering functionalities like object retrieval, storage, deletion, and more. It supports advanced features such as encryption and cache control directives, making it a versatile tool for managing S3 operations efficiently.
Ensure this module is included in your Node.js project. Import the S3 component as follows:
const s3 = require('eip-cloud-services/s3');
const exists = await s3.exists('myObjectKey');
console.log(exists); // true if exists, false otherwise
const object = await s3.get('myObjectKey');
console.log(object);
await s3.set('myObjectKey', 'myObjectData', { /* options */ });
await s3.del('myObjectKey');
await s3.copy('sourceObjectKey', 'destinationObjectKey');
await s3.move('sourceObjectKey', 'destinationObjectKey');
The module provides detailed control over cache behavior and supports encryption for stored objects, allowing you to optimize performance and security.
Configure the module with your AWS credentials and preferred settings in the config
directory.
The module includes comprehensive error handling, ensuring robust interaction with AWS S3 even in complex scenarios.
This Redis module provides a robust interface for interacting with Redis, supporting both standard Redis operations and advanced features like Redis Clusters. The module is designed to manage multiple Redis client instances efficiently, allowing seamless operation with different Redis configurations.
To use this Redis module, first ensure that it is included in your Node.js project. You can import the module as follows:
const redis = require('eip-cloud-services/redis');
You can perform standard Redis operations like setting, getting, deleting keys, etc. Here are some examples:
await redis.set('myKey', 'myValue');
const value = await redis.get('myKey');
console.log(value); // Outputs: myValue
await redis.del('myKey');
If your Redis setup includes a cluster, the module automatically configures the client for cluster operations based on your configuration.
redis.subscribe('myChannel', (message) => {
console.log(`Received: ${message}`);
});
await redis.publish('myChannel', 'Hello, World!');
The module internally manages multiple Redis clients for different purposes (like separate clients for pub/sub). However, this is abstracted away from the standard use of the module.
The module is designed to gracefully handle Redis errors, including connection issues and cluster errors.