Skip to content

Instantly share code, notes, and snippets.

@filipeandre
Last active June 17, 2024 17:27
Show Gist options
  • Save filipeandre/13984799b5d93cfdb1458450bc24cc74 to your computer and use it in GitHub Desktop.
Save filipeandre/13984799b5d93cfdb1458450bc24cc74 to your computer and use it in GitHub Desktop.
Remove the AWS_LAMBDA_EXEC_WRAPPER env variable and sls-sdk-* layers from all aws lambdas of current region and account
import boto3
def list_and_update_lambdas():
client = boto3.client('lambda')
paginator = client.get_paginator('list_functions')
response_iterator = paginator.paginate()
lambdas_updated_count = 0
for page in response_iterator:
for function in page['Functions']:
function_name = function['FunctionName']
try:
response = client.get_function_configuration(FunctionName=function_name)
layers = response.get('Layers', [])
env_vars = response.get('Environment', {}).get('Variables', {})
# Filter out layers that start with 'sls-sdk-'
updated_layers = [layer['Arn'] for layer in layers if not layer['Arn'].startswith('sls-sdk-')]
update_needed = len(updated_layers) != len(layers)
if 'AWS_LAMBDA_EXEC_WRAPPER' in env_vars:
del env_vars['AWS_LAMBDA_EXEC_WRAPPER']
update_needed = True
if update_needed:
client.update_function_configuration(
FunctionName=function_name,
Layers=updated_layers,
Environment={'Variables': env_vars}
)
lambdas_updated_count += 1
print(f"Updated {function_name}: Removed sls-sdk-* layers and/or AWS_LAMBDA_EXEC_WRAPPER")
except Exception as e:
print(f"Error updating configuration for {function_name}: {e}")
if lambdas_updated_count == 0:
print("No Lambda functions were updated.")
else:
print(f"Updated {lambdas_updated_count} Lambda functions.")
if __name__ == "__main__":
list_and_update_lambdas()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment