Modify A Lambda Function
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have created our 1st Lambda function using python in AWS console.
https://cloudaffaire.com/create-a-lambda-function/
In this blog post, we are going to modify this lambda function. We will also create a version and alias of the lambda function.
Modify A Lambda Function:
Step 1: Login to AWS console and navigate to ‘Lambda’.
Step 2: Click on the function name.
Step 3: From the ‘Actions’ click ‘Publish new version’.
Step 4: Provide a version description and click ‘Publish’.
Lambda function version 1 successfully created.
Note: Only latest version of the function is editable.
Next, we will create an alias of this version.
Step 5: From ‘Actions’ click ‘Create alias’.
Note: Currently we are in myfunction version1.
Step 6: Provide name, description and version details and click ‘Create’.
Function alias successfully created.
Next, we are going to modify the function definition to demonstrate logging and context. Select the latest version.
Step 7: Navigate to function code section and replace function code with below code. Click ‘Save’ once done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import json import time import logging logger = logging.getLogger() logger.setLevel(logging.INFO) #Three modes INFO, DEBUG and ERROR def lambda_handler(event, context): print('Hello world from print') logger.info('Hellow world! from logger') print("Function name:", context.function_name) print("Function vrsion:", context.function_version) print("Function ARN:", context.invoked_function_arn) print("Log stream name:", context.log_stream_name) print("Log group name:", context.log_group_name) print("Request ID:", context.aws_request_id) print("Mem. limits(MB):", context.memory_limit_in_mb) time.sleep(1) print("Time remaining (MS):", context.get_remaining_time_in_millis()) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } |
Note: We are writing a bunch of logs of function context using print and logger statement.
Next, we will invoke this lambda function manually using AWS CLI.
Step 8: Login to EC2 instance and execute the below command.
1 |
aws lambda invoke --function-name myfunction --log-type Tail outputfile.txt |
Note: You can also execute the previous version of the function by providing the version details. #aws lambda invoke –function-name myfunction:1 –log-type Tail outputfile.txt
You can view lambda logs under CloudWatch Logs section.
Hope you have enjoyed this article. In the next blog post, we will modify this function and add a trigger to this function.
To get more details on Lambda, please refer below AWS documentation
https://docs.aws.amazon.com/lambda/index.html