UNPKG

serverless-tag-resources

Version:

Datamart: Tag all AWS resources with dual legacy + datamart:* tag support

60 lines (50 loc) 1.51 kB
"use strict"; /** * AWS SDK v3 client factory. * Lazily creates and caches clients per service+region. */ const clientCache = new Map(); /** * Get or create an AWS SDK v3 client. * * @param {Function} ClientClass - SDK v3 client constructor (e.g. CloudFormationClient) * @param {object} config - { region, credentials } * @returns {object} cached client instance */ function getClient(ClientClass, config) { const key = `${ClientClass.name}:${config.region}`; if (!clientCache.has(key)) { clientCache.set(key, new ClientClass(config)); } return clientCache.get(key); } /** * Clear client cache (useful for testing). */ function clearClients() { clientCache.clear(); } /** * Extract SDK v3 compatible config from Serverless provider. * * @param {object} awsProvider - serverless.getProvider('aws') * @returns {object} { region, credentials } */ function configFromProvider(awsProvider) { const region = awsProvider.getRegion(); const creds = awsProvider.getCredentials(); // Serverless v3 provides credentials in different formats // depending on the provider. Normalize for SDK v3. const config = { region }; if (creds.credentials) { config.credentials = creds.credentials; } else if (creds.accessKeyId) { config.credentials = { accessKeyId: creds.accessKeyId, secretAccessKey: creds.secretAccessKey, sessionToken: creds.sessionToken, }; } return config; } module.exports = { getClient, clearClients, configFromProvider };