// Copyright Abridged, Inc. 2021,2024. All Rights Reserved. // Node module: @collabland/aws // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT import type {AwsCredentialIdentity} from '@aws-sdk/types'; import {getEnvVar, isProduction, setEnvVar} from '@collabland/common'; import {NodeHttpHandler} from '@smithy/node-http-handler'; import {AdaptiveRetryStrategy} from '@smithy/util-retry'; import {HttpsProxyAgent} from 'hpagent'; import https from 'https'; import {URL} from 'url'; /** * Get the AWS account id * @returns */ export function getAWSAccountId() { // 220623082201 is the production account, we default to that one to avoid // making changes in the production environment return getEnvVar('AWS_ACCOUNT', '220623082201'); } /** * Get the AWS region * @returns */ export function getAWSRegion() { return getEnvVar('AWS_REGION', isProduction() ? 'us-east-1' : 'us-west-1'); } /** * Get the AWS region for SNS * @returns */ export function getAWSSNSRegion(defaultRegion?: string) { let region = getEnvVar('AWS_SNS_REGION'); if (region != null) return region; // Infer the default region from SNS topic const topic = getEnvVar('COLLABLAND_JOB_TOPIC'); if (topic?.startsWith('arn:aws:sns:')) { // arn:aws:sns:us-west-1:352853905257:collabland-jobs-source-fuse-dev region = topic.split(':')[3]; } return region ?? defaultRegion ?? 'us-east-1'; } /** * Get the AWS region for SQS * @returns */ export function getAWSSQSRegion(defaultRegion?: string) { let region = getEnvVar('AWS_SQS_REGION'); if (region != null) return region; // Infer the default region from SQS url const url = getEnvVar('COLLABLAND_JOB_QUEUE'); if (url?.startsWith('https://sqs.')) { // https://sqs.us-west-1.amazonaws.com/352853905257/collabland-jobs-source-fuse-dev region = new URL(url).hostname.split('.')[1]; } return region ?? defaultRegion ?? getAWSRegion(); } export function getAWSDynamoDBRegion() { return getEnvVar( 'AWS_DYNAMODB_REGION', isProduction() ? 'us-east-2' : getAWSRegion(), ); } export function getAWSKMSRegion() { return getEnvVar('AWS_KMS_REGION', 'us-west-1'); } export function getAWSOpenSearchRegion() { return getEnvVar('AWS_OPENSEARCH_REGION', getAWSRegion()); } export function getAWSECSRegion() { return getEnvVar('AWS_ECS_REGION', 'us-east-1'); } /** * Set AWS environment variables for credentials * @param credentials */ export function setAwsCredentialsToEnv(credentials: AwsCredentialIdentity) { setEnvVar('AWS_ACCESS_KEY_ID', credentials.accessKeyId, true); setEnvVar('AWS_SECRET_ACCESS_KEY', credentials.secretAccessKey, true); setEnvVar('AWS_SESSION_TOKEN', credentials.sessionToken, true); setEnvVar( 'AWS_CREDENTIAL_EXPIRATION', credentials.expiration?.toISOString(), true, ); } export function getHttpHandler(maxSockets = 1000) { const httpsProxy = getEnvVar('HTTPS_PROXY') ?? getEnvVar('https_proxy'); let agent: https.Agent; if (httpsProxy != null) { agent = new HttpsProxyAgent({ proxy: httpsProxy, keepAlive: true, maxSockets, }); } else { agent = new https.Agent({ keepAlive: true, maxSockets, // AWS SDK default to 50 }); } return new NodeHttpHandler({ httpsAgent: agent, requestTimeout: 30000, // 30 seconds socketTimeout: 300000, // 5 mins connectionTimeout: 30000, // 30 seconds }); } export function getRetryStrategy(maxRetries = 10) { return new AdaptiveRetryStrategy(async () => maxRetries); } export function getDefaultConfig() { return { region: getAWSRegion(), apiVersion: 'latest', maxAttempts: 10, requestHandler: getHttpHandler(), retryStrategy: getRetryStrategy(), }; }