Creating Lambda Function on AWS Management Console for Cloud and DevOps Engineers

Creating Lambda Function on AWS Management Console for Cloud and DevOps Engineers

Learning path for the AWS Cloud Practitioner exam

📝Introduction

In this blog post, we will cover a simple and intro hands-on lab, where you will author a Lambda function using the Python programming language in the AWS console. You will also review logs generated by CloudWatch.

📝Log in to the AWS Management Console

Using your credentials, make sure you're using the right Region. In my case, I chose us-east-1.

📝Create Lambda Function

Navigate to the Lambda tab and click Create Function.

Make sure the Author from scratch option at the top is selected, and then use the following settings:

Function name: Type <your_function_name>

Runtime: Select the latest version of Python(3.10).

Expand the Change default execution role section, verify that Create a new role with basic Lambda permissions is selected and then click Create function.

📝Setting and Executing Lambda Function

Once the function has been created, scroll down to the Code tab.

Under Code source, select lambda_function.py.

Replace the existing sample code with the following:

import json

def lambda_handler(event, context):
    message = 'Hello {} {}! Keep being awesome!'.format(event['first_name'], event['last_name'])  

    #print to CloudWatch logs
    print(message)

    return {
        'message' : message
    }

Click Deploy.

Now, select Test > Configure test event.

Add your Event name and in the Event JSON box, replace the sample code with the following:

{
  "first_name": "Your First Name Here",
  "last_name": "Your Last Name Here"
}

Update the code to use your first and last name then click Format JSON to standardize the code in JSON format.

Click Save and Test and check the result printout.

📝Verify CloudWatch Captured Function Logs

Click on the Monitor tab and the View CloudWatch logs button and a new screen will launch to check if it has captured the Function logs.

Under Log Streams, click the most recent log stream.

Review the log. You should see a similar printout as you did in the execution results.

Congratulations — you've completed this hands-on lab covering the basics of creating a Lambda Function from the AWS Management Console.

Thank you for reading. I hope you were able to understand and learn something helpful from my blog.

Please follow me on Hashnode and on LinkedIn franciscojblsouza