求助:将Python+AWS CLI的Lambda下载脚本转为Node.js实现
Convert AWS Lambda Function Download Script to Node.js
Got it, let's turn that bash-based approach into a Node.js solution. We'll use the AWS SDK for JavaScript v3 (the latest, modular version) along with built-in Node.js modules for file handling, plus node-fetch to download the Lambda code packages.
Prerequisites
- Node.js 16+ installed
- AWS credentials configured (via
~/.aws/credentials, environment variables, or IAM roles if running on AWS) - Install required dependencies:
npm install @aws-sdk/client-lambda node-fetch
Complete Node.js Script
Create a file named download-lambdas.js with this code:
const { LambdaClient, ListFunctionsCommand, GetFunctionCommand } = require("@aws-sdk/client-lambda"); const fetch = require('node-fetch'); const fs = require('fs').promises; const path = require('path'); // Initialize Lambda client (uses default AWS credentials chain) const lambdaClient = new LambdaClient({ region: 'your-aws-region' }); // Replace with your region, e.g., 'us-east-1' async function downloadLambdaFunctions() { // Create output directory if it doesn't exist const outputDir = path.join(__dirname, 'lambda_functions'); await fs.mkdir(outputDir, { recursive: true }); try { // Get list of all Lambda functions const listCommand = new ListFunctionsCommand({}); const listResponse = await lambdaClient.send(listCommand); const functions = listResponse.Functions || []; if (functions.length === 0) { console.log('No Lambda functions found in this region.'); return; } console.log(`Found ${functions.length} Lambda functions. Starting download...`); // Download each function concurrently (adjust concurrency if needed to avoid rate limits) await Promise.all(functions.map(async (lambdaFunc) => { const functionName = lambdaFunc.FunctionName; try { // Get the pre-signed URL for the Lambda code const getCommand = new GetFunctionCommand({ FunctionName: functionName }); const getResponse = await lambdaClient.send(getCommand); const codeUrl = getResponse.Code.Location; // Download the code zip file const response = await fetch(codeUrl); if (!response.ok) { throw new Error(`Failed to download ${functionName}: HTTP ${response.status}`); } // Save the zip to the output directory const outputPath = path.join(outputDir, `${functionName}.zip`); const fileStream = fs.createWriteStream(outputPath); await new Promise((resolve, reject) => { response.body.pipe(fileStream); response.body.on('error', reject); fileStream.on('finish', resolve); }); console.log(`Successfully downloaded: ${functionName}`); } catch (err) { console.error(`Failed to download ${functionName}:`, err.message); } })); console.log('\nCompleted downloading all Lambda functions!'); } catch (err) { console.error('Error fetching Lambda function list:', err.message); } } // Run the download function downloadLambdaFunctions();
Key Details & Customizations
- Region: Replace
'your-aws-region'with your actual AWS region (e.g.,'us-west-2'). - Concurrency: The script uses
Promise.allto download functions concurrently. If you hit AWS rate limits, you can modify this to process functions in batches or add a small delay between requests. - Credentials: The Lambda client uses the default AWS credential chain, so it will pick up credentials from environment variables (
AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY),~/.aws/credentials, or IAM roles if running on an AWS resource like EC2/EKS. - Error Handling: The script includes basic error handling to log failures for individual functions without stopping the entire download process.
How to Run
- Save the script as
download-lambdas.js - Install dependencies (as listed in prerequisites)
- Replace the region with your own
- Run the script:
node download-lambdas.js
Your Lambda function zip files will be saved in a lambda_functions directory in the same folder as the script.
内容的提问来源于stack exchange,提问作者Dlindau
相关产品推荐
相关产品推荐

