惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
L
LangChain Blog
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
G
Google Developers Blog
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Creating Your First Lambda Function
Eric D Johns · 2026-05-15 · via DEV Community

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:

  1. 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.
  2. 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:

  1. Template source — Choose 1 - AWS Quick Start Templates
  2. Quick start template — Choose 1 - Hello World Example
  3. Use the most popular runtime and package type?N
  4. Runtime — Choose 12 - nodejs24.x
  5. Package type — Choose Zip
  6. Starter template — Choose 2 — Hello World Example TypeScript
  7. Enable X-Ray tracing?N (we'll keep it simple for now)
  8. Enable CloudWatch Application Insights?N
  9. Enable Lambda Insights?N
  10. 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

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

  2. 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 deployY
  • Allow SAM CLI IAM role creationY (SAM needs to create a role for your function)
  • Disable rollbackN
  • Save arguments to config fileY (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. 🚀