下载已上传的 Lambda 函数

Download an already uploaded Lambda function

我使用 "upload .zip" 在 AWS (Python) 中创建了一个 lambda 函数 我丢失了那些文件,我需要做一些更改,有什么方法可以下载那个 .zip 文件吗?

是!

导航到您的 lambda 函数设置,在右上角您将有一个名为“Actions”的按钮。在下拉菜单 select“export”中,在弹出窗口中单击“下载部署包”,该函数将下载到 .zip 文件中。

右上角的操作按钮

上面 CTA 的弹出窗口(点击此处的“下载部署包”)

更新:通过 sambhaji-sawant 向脚本添加了 link。修正了错别字,根据评论改进了答案和脚本!

您可以使用 aws-cli 下载任何 lambda 的 zip。

首先,您需要将 URL 获取到 lambda zip $ aws lambda get-function --function-name $functionName --query 'Code.Location'

然后您需要使用 wget/curl 从 URL 下载 zip。 $ wget -O myfunction.zip URL_from_step_1

此外,您可以使用

列出您 AWS 账户上的所有函数

$ aws lambda list-functions

我制作了一个简单的 bash 脚本来从您的 AWS 帐户并行下载所有 lambda 函数。你可以看到 here :)

注意:在使用上述命令(或任何 aws-cli 命令)之前,您需要使用 aws configure

设置 aws-cli

Full guide here

您可以使用 shell 脚本可用 here

如果你想下载给定区域的所有功能,这里是我的解决方法。 我创建了一个简单的节点脚本来下载功能。在 运行 脚本之前安装所有必需的 npm 包并将 AWS CLI 设置为您想要的区域。

let download = require('download-file');
let extract = require('extract-zip');
let cmd = require('node-cmd');

let downloadFile = async function (dir, filename, url) {
    let options = {
        directory: dir,
        filename: filename
    }
    return new Promise((success, failure) => {
        download(url, options, function (err) {
            if (err) {
                failure(err)
            } else {
                success('done');
            }
        })
    })
}

let extractZip = async function (source, target) {
    return new Promise((success, failure) => {
        extract(source, { dir: target }, function (err) {
            if (err) {
                failure(err)
            } else {
                success('done');
            }
        })
    })
}

let getAllFunctionList = async function () {
    return new Promise((success, failure) => {
        cmd.get(
            'aws lambda list-functions',
            function (err, data, stderr) {
                if (err || stderr) {
                    failure(err || stderr)
                } else {
                    success(data)
                }
            }
        );
    })
}

let getFunctionDescription = async function (name) {
    return new Promise((success, failure) => {
        cmd.get(
            `aws lambda get-function --function-name ${name}`,
            function (err, data, stderr) {
                if (err || stderr) {
                    failure(err || stderr)
                } else {
                    success(data)
                }
            }
        );
    })
}

let init = async function () {
    try {
        let { Functions: getAllFunctionListResult } = JSON.parse(await getAllFunctionList());
        let getFunctionDescriptionResult, downloadFileResult, extractZipResult;
        getAllFunctionListResult.map(async (f) => {
            var { Code: { Location: getFunctionDescriptionResult } } = JSON.parse(await getFunctionDescription(f.FunctionName));
            downloadFileResult = await downloadFile('./functions', `${f.FunctionName}.zip`, getFunctionDescriptionResult)
            extractZipResult = await extractZip(`./functions/${f.FunctionName}.zip`, `/Users/malhar/Desktop/get-lambda-functions/final/${f.FunctionName}`)
            console.log('done', f.FunctionName);
        })
    } catch (e) {
        console.log('error', e);
    }
}


init()

您可以找到一个 python 脚本来下载所有 lambda 函数 here

import os
import sys
from urllib.request import urlopen
import zipfile
from io import BytesIO

import boto3

def get_lambda_functions_code_url():

client = boto3.client("lambda")

lambda_functions = [n["FunctionName"] for n in client.list_functions()["Functions"]]

functions_code_url = []

for fn_name in lambda_functions:
    fn_code = client.get_function(FunctionName=fn_name)["Code"]
    fn_code["FunctionName"] = fn_name
    functions_code_url.append(fn_code)

return functions_code_url


def download_lambda_function_code(fn_name, fn_code_link, dir_path):

    function_path = os.path.join(dir_path, fn_name)
    if not os.path.exists(function_path):
        os.mkdir(function_path)

    with urlopen(fn_code_link) as lambda_extract:
        with zipfile.ZipFile(BytesIO(lambda_extract.read())) as zfile:
            zfile.extractall(function_path)


if __name__ == "__main__":
    inp = sys.argv[1:]
    print("Destination folder {}".format(inp))
    if inp and os.path.exists(inp[0]):
        dest = os.path.abspath(inp[0])
        fc = get_lambda_functions_code_url()
        print("There are {} lambda functions".format(len(fc)))
        for i, f in enumerate(fc):
            print("Downloading Lambda function {} {}".format(i+1, f["FunctionName"]))
            download_lambda_function_code(f["FunctionName"], f["Location"], dest)
    else:
        print("Destination folder doesn't exist")

这是我使用的bash脚本,它下载了默认区域中的所有功能:

download_code () {
    local OUTPUT=
    OUTPUT=`sed -e 's/,$//' -e 's/^"//'  -e 's/"$//g'  <<<"$OUTPUT"`
    url=$(aws lambda get-function --function-name get-marvel-movies-from-opensearch --query 'Code.Location' )
    wget $url -O $OUTPUT.zip
}

FUNCTION_LIST=$(aws lambda list-functions --query Functions[*].FunctionName)
for run in $FUNCTION_LIST
do
    download_code $run
done

echo "Finished!!!!"