Serverless Compute:
-
Serverless compute enables running code or containers without managing servers. The cloud provider automatically handles infrastructure provisioning, scaling, patching, and availability.
-
Developers focus only on application logic, while the platform manages the backend infrastructure.
Max execution time limits 15 min
Common integrations include:
-
Amazon API Gateway:
- restapi: 30 sec
- http
- webscoket api
-
Amazon S3:
- upload files…events
-
Amazon DynamoDB:
- event
-
Amazon EventBridge:
- based metrix
- trigger cron
-
Pricing Model
Lambda pricing is based on:
-
Number of requests
-
Execution duration
-
Memory allocation (GB-seconds)
-
Additional costs may include:
-
Provisioned concurrency
please follow class recording and create lambda and layers
Lambda sample code to start/stop ec2 insatnce
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
tag_key = event.get('tag_key', 'Schedule')
tag_value = event.get('tag_value', 'StartStop')
action = event.get('action')
response = ec2.describe_instances(
Filters=[{'Name': f'tag:{tag_key}', 'Values': [tag_value]}]
)
results = []
for reservation in response['Reservations']:
for instance in reservation['Instances']:
instance_id = instance['InstanceId']
state = instance['State']['Name']
if action == 'start' and state == 'stopped':
ec2.start_instances(InstanceIds=[instance_id])
results.append(f"Started {instance_id}")
elif action == 'stop' and state == 'running':
ec2.stop_instances(InstanceIds=[instance_id])
results.append(f"Stopped {instance_id}")
else:
results.append(f"Instance {instance_id} is already {state}")
# ✅ Return a structured response
return {
"statusCode": 200,
"body": {
"action": action,
"tag_key": tag_key,
"tag_value": tag_value,
"results": results
}
}
payload to trigger lambda
{
"action": "start",
"tag_key": "Environment",
"tag_value": "dev"
}
