
Demystifying AWS Lambda: Building Your First Serverless Function
What if you could run code in the cloud without managing a single server? That's AWS Lambda. In this beginner guide, you'll create your first function and trigger it through API Gateway.
āļø What is Serverless Computing?
Serverless doesn't mean there are no servers. It means you don't manage them. AWS handles provisioning, scaling, and maintenance, and you focus only on your code. You pay for the requests and the compute time your code actually uses, which makes it cost-effective. Lambda also has a free tier, so experimenting costs little or nothing.
š What is AWS Lambda?
AWS Lambda is a serverless, event-driven compute service. Something happens (an event, such as an API call or a file upload), and Lambda runs your code in response. You never provision or manage infrastructure.
š ļø Create Your First Function
Open the AWS Management Console and search for Lambda.
Click Create function and choose Author from scratch.
Give it a meaningful name, like my-first-function.
Choose a runtime, such as Python or Node.js. AWS automatically creates an IAM execution role so your function has permission to run and write logs.
Click Create function.
Click Create function and choose Author from scratch.
Give it a meaningful name, like my-first-function.
Choose a runtime, such as Python or Node.js. AWS automatically creates an IAM execution role so your function has permission to run and write logs.
Click Create function.
š» Write the Code
In the built-in code editor, use this Python handler:
1
2
3
4
5
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": "Hello from Lambda! š"
}event is the input that triggered the function.
context has runtime information.
The returned value is the response.
context has runtime information.
The returned value is the response.
š¢ Deploy and Test
Click Deploy to save your code.
Click Test, create a test event (the default is fine), and run it.
You'll see a 200 response with your message. š
Click Test, create a test event (the default is fine), and run it.
You'll see a 200 response with your message. š
Logs are available in Amazon CloudWatch via the Monitor tab.
š Trigger It with API Gateway
Right now you can only run the function from the console. Let's make it reachable from the internet:
In your function, click Add trigger.
Choose API Gateway ā Create a new API ā HTTP API.
Set security to Open (fine for learning) and click Add.
Copy the API endpoint URL and open it in your browser.
Choose API Gateway ā Create a new API ā HTTP API.
Set security to Open (fine for learning) and click Add.
Copy the API endpoint URL and open it in your browser.
You'll see your "Hello from Lambda!" message. Your function is now live:
User ā API Gateway ā Lambda ā Response
ā ļø "Open" means anyone with the URL can call it. Delete the API when you're done experimenting.
š® What's Next?
Lambda handles compute. Tomorrow I'll explore Amazon S3 for storage, and then connect the pieces into a real serverless workflow.
š¬ Your turn: Create your own function and change the message in the response. Did the browser output change after you redeployed?
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article