Skip to content

Instantly share code, notes, and snippets.

@seantcanavan
Created September 12, 2023 15:53
Show Gist options
  • Save seantcanavan/a1dd6f055efd5a58a375361b3e51311d to your computer and use it in GitHub Desktop.
Save seantcanavan/a1dd6f055efd5a58a375361b3e51311d to your computer and use it in GitHub Desktop.
Print Deprecated Go Lambda Functions to CSV
import subprocess
import csv
# Define the list of AWS regions as of my last update in September 2021.
aws_regions = [
"us-east-1", "us-east-2", "us-west-1", "us-west-2",
"af-south-1", "ap-east-1", "ap-south-1", "ap-northeast-2",
"ap-southeast-1", "ap-southeast-2", "ap-northeast-1",
"ca-central-1", "eu-central-1", "eu-west-1", "eu-west-2",
"eu-south-1", "eu-west-3", "eu-north-1", "me-south-1",
"sa-east-1"
]
# Open a CSV file for writing
with open("lambda_functions.csv", "w", newline="") as csvfile:
# Create a CSV writer object
csvwriter = csv.writer(csvfile)
# Write the header
csvwriter.writerow(["Region", "FunctionARN"])
# Loop through each AWS region and run the AWS CLI command
for region in aws_regions:
print(f"Processing region: {region}")
# Run the AWS CLI command
cmd = [
"aws", "lambda", "list-functions",
"--function-version", "ALL",
"--region", region,
"--output", "text",
"--query", "Functions[?Runtime=='go1.x'].FunctionArn"
]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Check for errors
if result.stderr:
print(f"Error: {result.stderr}")
continue
# Skip if the output is just a single double-quote
if result.stdout.strip() == '"':
continue
# Parse the stdout and write to CSV
function_arns = result.stdout.strip()
# Split the function ARNs by 'arn:aws:lambda'
function_arns = function_arns.split('arn:aws:lambda')
# Remove any empty strings, and strip each element
function_arns = [f'arn:aws:lambda{arn.strip()}' for arn in function_arns if arn.strip()]
for arn in function_arns:
csvwriter.writerow([region, arn])
print("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment