AWS Lambda runs small functions in a managed computing environment. You provide the code and configure its runtime; AWS manages the underlying servers, scaling, and availability. “Serverless” does not mean servers disappear—it means you do not operate them. Unlike an EC2 virtual machine, which provides an operating system you maintain, Lambda provides an execution environment for a function. section 6.1 section 6.1.1 section 6.1.3
To run a function:
- Write the code and include any required dependencies. Choose a supported runtime, such as Python or Node.js; a custom runtime is possible but adds complexity. section 6.1.2
- Create the function, set its runtime and configuration, then invoke it or arrange for an event to trigger it. section 6.1.2
- Give the function an IAM role when it needs permissions to use other AWS services. Check its invocations and search its logs in CloudWatch when something goes wrong. section 6.2.1 section 6.2.2
Check a website every five minutes
A Python function sends an HTTP request to a website and checks the response for expected text. An EventBridge rule using `rate(5 minutes)` invokes it repeatedly; CloudWatch records failures so an alarm can notify you by email. In the Management Console, the `lambda-canary` blueprint provides a starting point. This job suits Lambda because it runs briefly and does not need a machine waiting between checks. section 6.2 section 6.2.1
For repeatable deployment, define the function and its permissions with the Serverless Application Model (SAM) in a CloudFormation template. One Python example receives an event about a newly launched EC2 instance and uses the AWS SDK to add an `Owner` tag. Lambda also serves requests behind API Gateway or processes files when an object-store event arrives; those applications use other services for storage. section 6.3 section 6.3.2 section 6.3.3 section 6.4.3
Listing 6.5 Defining a Lambda function with SAM within a CloudFormation template
---
AWSTemplateFormatVersion: '2010-09-09' #1
Transform: AWS::Serverless-2016-10-31 #2
Description: Adding an owner tag to EC2 instances automatically
Resources:
# [...]
EC2OwnerTagFunction:
Type: AWS::Serverless::Function #3
Properties:
Handler: lambda_function.lambda_handler #4
Runtime: python3.9 #5
Architectures: #6
- arm64
CodeUri: '.' #7
Policies: #8
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action: 'ec2:CreateTags'
Resource: '*'
Events:
EventBridgeRule: #9
Type: EventBridgeRule #10
Properties:
Pattern: #11
source:
- 'aws.ec2'
detail-type:
- 'AWS API Call via CloudTrail'
detail:
eventSource:
- 'ec2.amazonaws.com'
eventName:
- 'RunInstances'
The SAM template shows where the handler, Python runtime, code location, and permission to create EC2 tags are declared. section 6.3.3
Choose Lambda with its limits in mind. One invocation can run for at most 900 seconds, and a newly created execution context can add cold-start latency. Functions run outside your VPC by default; reaching private resources requires VPC, subnet, and security-group configuration. Billing follows requests and resource consumption rather than an idle virtual machine, which can favor occasional work, but high request volume may cost more than EC2. Include the costs of services used alongside Lambda when comparing options. section 6.4.1 section 6.2.4 section 6.4.2

