Modify A Lambda Function

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’.

Modify A Lambda Function

Step 2: Click on the function name.

Modify A Lambda Function

Step 3: From the ‘Actions’ click ‘Publish new version’.

Modify A Lambda Function

Step 4: Provide a version description and click ‘Publish’.

Modify A Lambda Function

Lambda function version 1 successfully created.

Modify A Lambda Function

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’.

Modify A Lambda Function

Note: Currently we are in myfunction version1.

Step 6: Provide name, description and version details and click ‘Create’.

Modify A Lambda Function

Function alias successfully created.

Modify A Lambda Function

Next, we are going to modify the function definition to demonstrate logging and context. Select the latest version.

Modify A Lambda Function

Step 7: Navigate to function code section and replace function code with below code. Click ‘Save’ once done.

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.

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.

Modify A Lambda Function

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

Leave a Reply

Close Menu