Your First Serverless Function: A Practical Guide to AWS Lambda and CloudWatch


Now, let's talk about the code that runs on your cloud infrastructure—or better yet, what if you didn't have to worry about the infrastructure at all?
Welcome to the world of serverless.
For decades, running an application meant managing a server. You were the owner—responsible for patching the OS, managing network access (like IP addresses), scaling up for traffic spikes, and scaling down to save money. But what if you could just provide your code and let AWS handle everything else?
That’s the promise of AWS Lambda.
What is AWS Lambda? Your Code, No Servers.
AWS Lambda is a serverless, event-driven compute service. That's a fancy way of saying it lets you run code without provisioning or managing servers. You simply upload your code, and Lambda runs it in response to an event (like an HTTP request or a file upload).
As my notes highlight, the primary difference between traditional and serverless architecture lies in ownership and abstraction.
Server Architecture (e.g., EC2) | Serverless Architecture (AWS Lambda) |
You manage the server (OS, patches) | AWS manages everything. |
You configure scaling | It auto-scales up and down instantly, from zero to thousands of requests. |
You pay for it while it's running | You pay only for the compute time you consume—down to the millisecond. |
You are the owner; you see all the details. | You don't know the underlying details, and you don't need to |
With Lambda, you focus on writing your function, and AWS handles the rest.
What is AWS CloudWatch? Your Eyes and Ears in the Cloud.
So, your code is running somewhere in the cloud, but how do you know what it's doing? How do you debug it when it fails? How do you know how often it's being used?
This is where AWS CloudWatch comes in.
As you rightly noted, CloudWatch is the "gatekeeper" for monitoring, alerting, reporting, and logging in AWS. For Lambda, it is absolutely essential. If Lambda is the engine, CloudWatch is the dashboard.
It gives us three fundamental things for our Lambda functions:
CloudWatch Logs: Every print() statement in your function and all system output ends up here. This is your primary tool for debugging.
CloudWatch Metrics: Lambda automatically sends performance data to CloudWatch. You can track invocations, errors, duration, and more without writing any code. You can even create Custom Metrics for more specific tracking.
CloudWatch Alarms: You can take action based on metrics. For example: "Send me an email if my function has more than 5 errors in one minute."
A Hands-On Tutorial: Your First Lambda Function
Let's build a "Hello World" function and see how it connects to CloudWatch. For this tutorial, we'll use the AWS Management Console to see things visually.
Step 1: Create a Lambda Function
Navigate to the AWS Lambda service in the console.
Click Create function.
Select Author from scratch.
Function name: my-first-lambda
Runtime: Select Python 3.11 (or another recent version).
Leave the rest as default and click Create function.
You now have a live Lambda function!
Step 2: Add a Log to Your Code
In the Code source editor, you'll see some default code in a file named lambda_function.py. Let's modify it to add a custom log message.
Replace the code with this:
import json
def lambda_handler(event, context):
# Log the incoming event to CloudWatch for debugging
print(f"EVENT RECEIVED: {json.dumps(event, indent=2)}")
# Prepare a response
response = {
"statusCode": 200,
"body": json.dumps("Hello from your first Lambda function!"),
}
# Log a success message
print("SUCCESS: Function executed correctly.")
return response
Click the Deploy button above the editor to save your changes.
Step 3: Test Your Function
Go to the Test tab.
Event name: MyFirstTest
Leave the JSON template as it is and click the orange Test button.
You should see a green "Succeeded" status.
Step 4: Find Your Logs in CloudWatch
Now, let's see where our print() statements went.
Navigate to the AWS CloudWatch service.
In the left menu, click Log groups.
You will see a log group named /aws/lambda/my-first-lambda. Click on it.
Inside, you'll see one or more "Log streams." Click the latest one.
Success! You will now see the output from your test.
As you can see, our custom print statements—EVENT RECEIVED and SUCCESS—are clearly visible. CloudWatch also automatically adds helpful information like the START, END, and REPORT lines, which show the unique request ID and the billed duration for the execution.
Step 5: Check Your Metrics
Let's see the performance data AWS collected automatically.
In CloudWatch, go to Metrics > All metrics.
In the list of namespaces, click on Lambda.
Click on By Function Name.
Find the Invocations metric for my-first-lambda and check the box next to it.
You'll see a graph showing a data point for the test invocation you just ran. You didn't have to configure anything to get this data!
The single dot on the graph represents the one time we invoked our function using the "Test" button. As your function gets more traffic, this graph will give you a real-time view of its usage.
Conclusion
You've just built and monitored your first serverless application!
You learned that AWS Lambda lets you run code without managing servers, and AWS CloudWatch gives you the essential observability (logs and metrics) to understand what your code is doing. This powerful pair is the heart of modern, scalable, and cost-effective cloud applications.
In our next article, we'll take this a step further. Now that we know how to build a function manually, how can we deploy it automatically using the CloudFormation skills we learned in our first post? Stay tuned!
Thanks for reading! If you found this helpful, please clap, follow, and join me for the next part of our AWS journey.
Subscribe to my newsletter
Read articles from Md Sharjil Alam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Md Sharjil Alam
Md Sharjil Alam
🚀 DevOps & Cloud Engineer | AWS | CI/CD | Terraform | Docker | Golang | Kubernetes I'm a DevOps & Cloud Engineer passionate about automating infrastructure and building reliable, scalable cloud systems. I bring hands-on experience with AWS services, CI/CD pipelines, and Infrastructure as Code to streamline software delivery and enhance operational efficiency. From writing backend logic in Golang to provisioning cloud infra with Terraform, and deploying Dockerized apps using Jenkins, I’ve worked across the stack to integrate development and operations seamlessly. 🔧 Core Skills: DevOps: Jenkins, GitHub Actions, Docker, Ansible, Terraform Cloud: AWS (EC2, S3, IAM, Lambda, Route 53, CloudWatch) IaC & Automation: Terraform, Ansible, Shell scripting Containerization & Orchestration: Docker, Kubernetes Backend Development: Golang, REST APIs, MySQL, MongoDB Frontend (for full-stack apps): ReactJS, JavaScript, Tailwind CSS Tools: Git, GitHub, Linux, VS Code 🛠️ Project Highlights: ⚙️ Built automated CI/CD pipelines with Jenkins, Docker, and GitHub Actions ☁️ Deployed and managed staging/production environments on AWS 🔧 Provisioned cloud infrastructure using Terraform and Ansible 🧠 Wrote backend APIs in Go and connected to full-stack apps 📊 Set up IAM roles, monitoring (CloudWatch), and cloud security best practices 📚 I share learnings and tutorials on Hashnode. 📩 Let’s connect: mdsharjil32@gmail.com