Creating Your First Lambda Function

Creating Your First Lambda Function

So you want to build your first serverless function? Let's do it. No fluff, no detours — just you, AWS SAM, and a working Lambda function running on your machine in minutes. We're not even going to deploy to AWS yet. Let's just get it working locally first. What You'll Need Two things: AWS SAM CLI installed — SAM (Serverless Application Model) is the tool we'll use to create, build, and test our function. Follow the install guide for your operating system. Docker or Finch installed — SAM uses a container runtime to simulate Lambda locally. You have two options: Docker Desktop — The most common option. Install it and make sure it's running. Finch — A lightweight open source alternative from AWS. Install it with brew install finch. Verify your setup Check that SAM is installed: sam --version Enter fullscreen mode Exit fullscreen mode If you chose Finch, initialize and start the VM: finch vm init finch vm start Enter fullscreen mode Exit fullscreen mode SAM automatically detects Finch when Docker isn't running — no extra configuration needed. If you have both installed, SAM uses Docker by default. To make Finch the default on macOS, run: sudo /usr/libexec/PlistBuddy -c "Add :DefaultContainerRuntime string finch" /Library/Preferences/com.amazon.samcli.plist Enter fullscreen mode Exit fullscreen mode Verify your container runtime is working: docker --version # or: finch --version Enter fullscreen mode Exit fullscreen mode That's it. No AWS account needed yet. We're staying local for now. Step 1: Initialize Your Project Open your terminal and run: sam init Enter fullscreen mode Exit fullscreen mode SAM is going to walk you through a few questions. For this blog, I'm using the TypeScript template, but you can choose any runtime and template you'd like — Python, Java, Go, whatever you're comfortable with. The prompts, project structure, and file names will vary depending on what you pick. Here's what I chose: Template source — Choose 1 - AWS Quick Start Templates Quick start template — Choose 1 - Hello World Example Use the most popular runtime and package type? — N Runtime — Choose 12 - nodejs24.x Package type — Choose Zip Starter template — Choose 2 — Hello World Example TypeScript Enable X-Ray tracing? — N (we'll keep it simple for now) Enable CloudWatch Application Insights? — N Enable Lambda Insights? — N Project name — Hit Enter to accept the default, or name it whatever you like SAM just scaffolded an entire serverless project for you. Let's look at what we got: sam-app/ ├── template.yaml # The blueprint for your infrastructure ├── samconfig.toml # SAM deployment configuration ├── hello-world/ # Your function code lives here │ ├── app.ts # Your actual Lambda function │ ├── package.json # Dependencies │ └── tests/ # Unit tests │ └── unit/ │ └── test-handler.test.ts └── events/ └── event.json # A sample test event Enter fullscreen mode Exit fullscreen mode The two files that matter most: template.yaml — This describes your Lambda function and any AWS resources it needs. Think of it as the blueprint. app.ts — This is your actual function code. The thing that runs when your Lambda gets invoked. Understanding the template Let's look at the key part of template.yaml — the function resource: Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: hello-world/ Handler: app.lambdaHandler Runtime: nodejs24.x Architectures: - x86_64 Events: HelloWorld: Type: Api Properties: Path: /hello Method: get Metadata: BuildMethod: esbuild BuildProperties: Minify: true Target: "es2020" Sourcemap: true EntryPoints: - app.ts Enter fullscreen mode Exit fullscreen mode Here's what's going on: Type: AWS::Serverless::Function — This tells SAM to create a Lambda function. SAM handles all the underlying CloudFormation resources for you. CodeUri: hello-world/ — Points to the folder where your function code lives. Handler: app.lambdaHandler — Tells Lambda which file and function to run. In this case, the lambdaHandler export in app.ts. Runtime: nodejs24.x — The Node.js version your function runs on. The magic is in the Events section. By adding an event with Type: Api, SAM automatically creates an API Gateway for you — no extra configuration needed. You didn't define an API Gateway resource anywhere, but SAM sees that your function wants to respond to HTTP requests at GET /hello and wires it all up behind the scenes. One line, and you've got a fully managed API endpoint in front of your Lambda. The Metadata section tells SAM how to build your TypeScript code. Instead of running tsc and bundling manually, SAM uses esbuild — a JavaScript/TypeScript bundler. It compiles your TypeScript, minifies the output, generates sourcemaps for debugging, and packages it all up. You don't need to install esbuild yourself — SAM handles it during sam build. Step 2: Build It cd sam-app sam build Enter fullscreen mode Exit fullscreen mode SAM grabs your code, installs any dependencies, and packages everything up. You'll see a .aws-sam folder appear — that's your build output. You don't need to touch it. Don't have the runtime installed on your machine (e.g., no Node.js)? No problem — just add --use-container and SAM will build inside a Docker/Finch container instead: sam build --use-container Enter fullscreen mode Exit fullscreen mode SAM will automatically pull down a container image that matches your function's runtime, build your code inside it, and output the packaged result to .aws-sam just like a normal build. You get the exact same Lambda environment without installing anything on your machine. Step 3: Test It Locally Here's the fun part. Run your Lambda function right on your machine: sam local invoke Enter fullscreen mode Exit fullscreen mode You should see a response like: {"statusCode": 200, "body": "{\"message\": \"hello world\"}"} Enter fullscreen mode Exit fullscreen mode That's your Lambda function running locally. No AWS account, no deployment, no charges. Just your code doing its thing. Make It Yours Let's make a quick change so you can see the full loop — edit, build, test. Open hello-world/app.ts and find this line: message: 'hello world', Enter fullscreen mode Exit fullscreen mode Change it to: message: 'hello Lambda', Enter fullscreen mode Exit fullscreen mode Now rebuild and invoke again: sam build sam local invoke Enter fullscreen mode Exit fullscreen mode {"statusCode": 200, "body": "{\"message\": \"hello Lambda\"}"} Enter fullscreen mode Exit fullscreen mode That's the workflow. Change your code, build, test — all local, all instant. Want to test it as an API instead? Run: sam local start-api Enter fullscreen mode Exit fullscreen mode This spins up a local API Gateway. Open your browser and hit http://127.0.0.1:3000/hello and you'll see: {"message": "hello Lambda"} Enter fullscreen mode Exit fullscreen mode You now have a fully functional serverless API running on your laptop. What Just Happened? In about 5 minutes, you: Scaffolded a serverless project with a single command Learned how the SAM template automatically wires up a Lambda function and API Gateway Built your TypeScript code with esbuild — no manual setup needed Ran a Lambda function locally Made a code change and saw it reflected instantly Spun up a local API endpoint and hit it from the browser All without touching AWS. All free. All on your machine. Ready to Deploy? Let's Go Live. You've got it working locally. Now let's put it in the cloud so the rest of the world can use it. For this part, you'll need a couple more things set up. Additional Requirements An AWS account — Head to aws.amazon.com/free and sign up. New customers get up to $200 in credits and a Free Plan that lets you explore AWS for up to 6 months at no cost. On top of that, Lambda has its own always-free tier — 1 million requests and 400,000 GB-seconds of compute per month. AWS CLI installed and configured — This is how your machine talks to your AWS account. First, install it from the AWS CLI install guide. Then follow the Setting up the AWS CLI guide to configure your credentials. For most new users, you'll want the IAM user with short-term credentials option — it's the recommended approach for getting started securely. Once you're set up, verify it's working: aws sts get-caller-identity Enter fullscreen mode Exit fullscreen mode If you see your account ID, you're connected. Step 4: Deploy It From your project directory, run: sam deploy --guided Enter fullscreen mode Exit fullscreen mode The --guided flag walks you through the deployment settings the first time. Here's what to expect: Stack Name — Give it a name (like my-sam-app) AWS Region — Pick your closest region (e.g., us-east-1) Confirm changes before deploy — Y Allow SAM CLI IAM role creation — Y (SAM needs to create a role for your function) Disable rollback — N Save arguments to config file — Y (so you don't have to answer these again) SAM will show you a changeset — a preview of what it's about to create. Type Y to confirm, and watch it go. When it's done, you'll see an Outputs section with a URL. That's your live API endpoint. Copy it, paste it in your browser, and... {"message": "hello Lambda"} Enter fullscreen mode Exit fullscreen mode 🎉 Your Lambda function is live on AWS. Clean Up Don't want to leave resources hanging around? Tear it all down with: sam delete Enter fullscreen mode Exit fullscreen mode SAM removes everything it created. Clean slate. What Just Happened? In about 10 minutes, you: Scaffolded a serverless project with a single command Learned how the SAM template automatically wires up a Lambda function and API Gateway Built your TypeScript code with esbuild — no manual setup needed Tested your function locally and made a code change Spun up a local API and hit it from the browser Deployed it to AWS with a real, live URL Cleaned it all up with one command SAM handled all the heavy lifting — the CloudFormation stack, the IAM roles, the API Gateway, the packaging. You just answered a few questions. What's Next? You've got the basics down. From here you can: Add more functions to your template.yaml Connect your Lambda to DynamoDB, S3, or other AWS services Set up environment variables and custom permissions Explore event-driven architectures with SNS, SQS, and EventBridge Want to keep learning? Check out Serverless Land — the go-to hub for all things Lambda and serverless with patterns, tutorials, and best practices. Or dive into the Sessions with SAM YouTube playlist that goes deeper into everything AWS SAM can do. But that's for next time. For now, celebrate — you just went serverless. 🚀

Original Source

Read the full article at Dev →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.