AWS Lambda (Serverless)

Lambda runs your code in response to events without any servers to manage, and you pay only per request.

AWS Lambda is serverless compute. You upload a function; AWS runs it whenever an event triggers it and scales it automatically. There is no instance to patch or keep running.

How it works

  • Write a handler function in Node.js, Python, Java, Go and more.
  • Configure a trigger — an HTTP request via API Gateway, a file landing in S3, a message on a queue, or a schedule.
  • You pay per request and per millisecond of runtime; idle time is free.

Good fits

  • APIs and microservices with variable traffic.
  • Event processing (resize an image when it is uploaded).
  • Scheduled jobs and glue code between services.

Example

Example · json
// A simple Node.js handler (index.mjs)
// export const handler = async (event) => {
//   return { statusCode: 200, body: JSON.stringify({ message: 'Hello from Lambda!' }) };
// };

// Deploy from a zip and invoke it:
// aws lambda create-function --function-name hello-world \
//   --runtime nodejs20.x --handler index.handler \
//   --role arn:aws:iam::123456789012:role/lambda-basic-exec \
//   --zip-file fileb://function.zip
// aws lambda invoke --function-name hello-world response.json

When to use it

  • An API backend runs as a Lambda function invoked by API Gateway, eliminating the need to manage any EC2 instances for a REST endpoint.
  • A Lambda function triggers automatically when a file is uploaded to S3, resizing images and storing thumbnails in another bucket.
  • A scheduled Lambda function runs every hour to check for expired database records and delete them, replacing a cron job on a server.

More examples

Create a Simple Lambda Function

Packages a Node.js file and deploys it as a Lambda function with a specified runtime and execution role.

Example · bash
# Package the function
zip function.zip index.js

# Deploy it
aws lambda create-function \
  --function-name hello-world \
  --runtime nodejs20.x \
  --role arn:aws:iam::123456789012:role/LambdaExecRole \
  --handler index.handler \
  --zip-file fileb://function.zip

Invoke Lambda and See Output

Invokes the Lambda function synchronously with a JSON payload and writes the response to a local file.

Example · bash
aws lambda invoke \
  --function-name hello-world \
  --payload '{"name": "SoundsCode"}' \
  --cli-binary-format raw-in-base64-out \
  response.json

cat response.json

Add S3 Trigger to Lambda

Grants S3 permission to invoke the Lambda function and wires the bucket's PUT event to trigger it automatically.

Example · bash
# Allow S3 to invoke the function
aws lambda add-permission \
  --function-name image-processor \
  --statement-id s3-trigger \
  --action lambda:InvokeFunction \
  --principal s3.amazonaws.com \
  --source-arn arn:aws:s3:::uploads-bucket

# Configure the S3 bucket notification
aws s3api put-bucket-notification-configuration \
  --bucket uploads-bucket \
  --notification-configuration file://s3-notification.json

Discussion

  • Be the first to comment on this lesson.