Created
January 14, 2022 14:31
-
-
Save alxfordy/4616bf4af6e7b1b1687cb68f6774d194 to your computer and use it in GitHub Desktop.
AWS Lambda Bulk Export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Ensure you ahve already run `aws configure` and set up your access/secret keys then run this on your desktop | |
""" | |
import os | |
import sys | |
from urllib.request import urlopen | |
import zipfile | |
from io import BytesIO | |
import json | |
import boto3 | |
def get_lambda_functions_code_url(): | |
client = boto3.client("lambda") | |
lambda_functions = [lambda_func for lambda_func in client.list_functions().get("Functions")] | |
functions_code_url = [] | |
for lambda_func in lambda_functions: | |
fn_code = client.get_function(FunctionName=lambda_func.get("FunctionName")) | |
functions_code_url.append({ | |
"FunctionName": lambda_func.get("FunctionName"), | |
"FunctionCodeLocation": fn_code.get("Code").get("Location") | |
}) | |
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(f"There are {len(fc)} lambda functions") | |
for item in fc: | |
print(f"Download {item.get('FunctionName')}") | |
download_lambda_function_code(item.get("FunctionName"), item.get("FunctionCodeLocation"), dest) | |
else: | |
print("Destination folder doesn't exist") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment