|  | 
|  | 1 | +#!/usr/bin/python | 
|  | 2 | +# -*- coding: utf-8 -*- | 
|  | 3 | +# lambdaupdate.py | 
|  | 4 | +# It is an example that handles Lambda functions on AWS. | 
|  | 5 | +# It uses Client API (low-level) of Boto3. | 
|  | 6 | +# Update a Lambda function. | 
|  | 7 | +# You must provide 1 parameter: | 
|  | 8 | +# FUNCTION_NAME = Lambda function name | 
|  | 9 | +# FUNCTION_FILE = The path to the .py file where the code of the Lambda function is located | 
|  | 10 | +# FUNCTION_ROLE = The role ARN that has Lambda permissions | 
|  | 11 | +# FUNCTION_HANDLER = The fully qualifed method name (Ex: lambda_function.lambda_handler) | 
|  | 12 | + | 
|  | 13 | +import sys | 
|  | 14 | +import io | 
|  | 15 | +import zipfile | 
|  | 16 | +import boto3 | 
|  | 17 | +import botocore | 
|  | 18 | + | 
|  | 19 | +def create_lambda_deployment_package(function_file_name): | 
|  | 20 | + # Creates a Lambda deployment package in ZIP format in an in-memory buffer. | 
|  | 21 | + # This buffer can be passed directly to AWS Lambda when creating the function. | 
|  | 22 | + # Parameters: | 
|  | 23 | + # function_file_name (string): The name of the file that contains the Lambda handler function. | 
|  | 24 | + # Return: | 
|  | 25 | + # The deployment package. | 
|  | 26 | + | 
|  | 27 | + buffer = io.BytesIO() | 
|  | 28 | + with zipfile.ZipFile(buffer, 'w') as zipped: | 
|  | 29 | + zipped.write(function_file_name) | 
|  | 30 | + buffer.seek(0) | 
|  | 31 | + return buffer.read() | 
|  | 32 | + | 
|  | 33 | + | 
|  | 34 | +def main(): | 
|  | 35 | + | 
|  | 36 | + REGION = 'eu-west-1' # AWS region | 
|  | 37 | + | 
|  | 38 | + # Make a list of command line arguments, omitting the [0] element | 
|  | 39 | + # which is the script itself. | 
|  | 40 | + args = sys.argv[1:] | 
|  | 41 | + if len(args) < 4: | 
|  | 42 | + print('Not enough parameters.\n'\ | 
|  | 43 | + 'Proper Usage is: python lambdaupdate.py '\ | 
|  | 44 | + '<FUNCTION_NAME> <FUNCTION_FILE<> <FUNCTION_ROLE> <FUNCTION_HANDLER>') | 
|  | 45 | + sys.exit(1) | 
|  | 46 | + | 
|  | 47 | + function_name = args[0] | 
|  | 48 | + function_file = args[1] | 
|  | 49 | + function_role = args[2] | 
|  | 50 | + function_handler = args[3] | 
|  | 51 | + | 
|  | 52 | + print('Lambda function name: ' + function_name) | 
|  | 53 | + print('Lambda function file: ' + function_file) | 
|  | 54 | + print('Lambda function role: ' + function_role) | 
|  | 55 | + print('Lambda function handler: ' + function_handler) | 
|  | 56 | + | 
|  | 57 | + # Create a Lambda Client | 
|  | 58 | + lambda_client = boto3.client('lambda', region_name=REGION) | 
|  | 59 | + | 
|  | 60 | + # Create Lambda function | 
|  | 61 | + try: | 
|  | 62 | + print('Updating function ...') | 
|  | 63 | +  | 
|  | 64 | + deployment_package = create_lambda_deployment_package(function_file) | 
|  | 65 | + | 
|  | 66 | + # Update Lambda function configuration | 
|  | 67 | + response = lambda_client.update_function_configuration( | 
|  | 68 | + FunctionName=function_name, | 
|  | 69 | + Description='Updated by the Lambda Python API', | 
|  | 70 | + Runtime='python3.8', | 
|  | 71 | + Role=function_role, | 
|  | 72 | + Handler=function_handler) | 
|  | 73 | + | 
|  | 74 | + print('Response (Conf.):') | 
|  | 75 | + print(response) | 
|  | 76 | + function_arn_conf = response['FunctionArn'] | 
|  | 77 | + | 
|  | 78 | + # Update Lambda function code | 
|  | 79 | + response = lambda_client.update_function_code( | 
|  | 80 | + FunctionName=function_name, | 
|  | 81 | + ZipFile=deployment_package, | 
|  | 82 | + Publish=True) | 
|  | 83 | + | 
|  | 84 | + print('Response (Code):') | 
|  | 85 | + print(response) | 
|  | 86 | + function_arn_code = response['FunctionArn'] | 
|  | 87 | + | 
|  | 88 | + print('\nUpdated function "' + function_name + '"') | 
|  | 89 | + print('The function ARN (Conf.) is ' + function_arn_conf) | 
|  | 90 | + print('The function ARN (Code) is ' + function_arn_code) | 
|  | 91 | + | 
|  | 92 | + except botocore.exceptions.ClientError as e: | 
|  | 93 | + if e.response['Error']['Code'] == "AccessDeniedException": | 
|  | 94 | + print("Error: Access Denied!!") | 
|  | 95 | + elif e.response['Error']['Code'] == "ValidationException": | 
|  | 96 | + print("Error: Name Not Valid!!") | 
|  | 97 | + else: | 
|  | 98 | + raise | 
|  | 99 | + | 
|  | 100 | + return | 
|  | 101 | + | 
|  | 102 | + | 
|  | 103 | +# This is the standard boilerplate that calls the main() function. | 
|  | 104 | +if __name__ == '__main__': | 
|  | 105 | + main() | 
0 commit comments