Skip to content

Instantly share code, notes, and snippets.

@ankur0101
Created July 31, 2023 14:34
Show Gist options
  • Save ankur0101/82c1f68b668f656a3f4b9ac833198dc9 to your computer and use it in GitHub Desktop.
Save ankur0101/82c1f68b668f656a3f4b9ac833198dc9 to your computer and use it in GitHub Desktop.
A python snippet to retrieve all lambda functions to store under git
import boto3
import os
import sys
import requests
from pathlib import Path
import zipfile
fn_client = boto3.client("lambda")
dir_name = 'lambda_functions'
Path("./"+str(dir_name)).mkdir(parents=True, exist_ok=True)
for func in fn_client.list_functions()['Functions']:
print(func['FunctionName'])
tmp_dir = './'+str(dir_name)+'/'
arn = func['FunctionArn']
arn_parts = arn.split(':')
func_name = arn_parts[6]
func_details = fn_client.get_function(FunctionName=arn)
zip_file = tmp_dir + func_name + '.zip'
if 'Location' not in func_details['Code']:
continue
url = func_details['Code']['Location']
r = requests.get(url)
with open(zip_file, "wb") as code:
code.write(r.content)
with zipfile.ZipFile(zip_file,"r") as zip_ref:
depl_package_dir = "./"+str(dir_name)+'/'+func_name+'/'+func['Runtime']
Path(depl_package_dir).mkdir(parents=True, exist_ok=True)
zip_ref.extractall(depl_package_dir)
os.remove(zip_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment