Skip to main content

Getting Started with Serverless Architecture Using AWS Lambda

Β· 4 min read
Nayanika Mukherjee
Full Stack Developer

Serverless architecture is a cloud computing execution model where the cloud provider dynamically manages the allocation and provisioning of servers. AWS Lambda, a key component of serverless architecture, allows you to run code without provisioning or managing servers. This guide will introduce you to AWS Lambda and provide a step-by-step approach to getting started with serverless architecture.

Key Concepts​

What is AWS Lambda?​

AWS Lambda is a compute service that lets you run code in response to events and automatically manages the compute resources required by that code. You pay only for the compute time you consume.

Serverless Benefits​

  • No Server Management: No need to provision or manage servers.
  • Scalability: Automatically scales your application by running code in response to each trigger.
  • Cost Efficiency: Pay only for the compute time you consume.

Event Sources​

Lambda can be triggered by various AWS services such as S3, DynamoDB, Kinesis, SNS, and more.

Setting Up AWS Lambda​

Prerequisites​

  • An AWS account.
  • AWS CLI installed and configured.
  • Basic knowledge of Python (or the language you choose for your Lambda functions).

Creating an IAM Role​

Before creating a Lambda function, you need an IAM role that Lambda assumes when it executes your function.

aws iam create-role --role-name lambda-execution-role --assume-role-policy-document file://trust-policy.json

trust-policy.json:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

Attach the necessary policies to the role:

aws iam attach-role-policy --role-name lambda-execution-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

Writing and Deploying Lambda Functions​

Basic Lambda Function​

Here is a simple Python function that returns a greeting.

lambda_function.py:

def lambda_handler(event, context):
return {
'statusCode': 200,
'body': 'Hello, World!'
}

Creating and Deploying the Function​

  • Create a ZIP file containing your code:
zip function.zip lambda_function.py
  • Deploy the Lambda Function:
aws lambda create-function --function-name HelloWorldFunction \
--zip-file fileb://function.zip --handler lambda_function.lambda_handler \
--runtime python3.8 --role arn:aws:iam::123456789012:role/lambda-execution-role

Lambda Execution Model​

Lambda functions have an execution model that includes:

  • Invocation: Functions can be invoked synchronously or asynchronously.
  • Concurrency: Lambda automatically scales to handle the incoming requests.
  • Execution Duration: You can configure the timeout for your function (default is 3 seconds, maximum is 15 minutes).

Managing Lambda Functions​

  • Updating a Function To update the function code:
zip function.zip lambda_function.py
aws lambda update-function-code --function-name HelloWorldFunction --zip-file fileb://function.zip
  • Monitoring and Logging AWS Lambda integrates with Amazon CloudWatch to provide monitoring and logging. You can view logs by navigating to the CloudWatch Logs in the AWS Management Console.

Advanced Topics​

Environment Variables​

You can use environment variables to pass configuration settings to your Lambda function.

aws lambda update-function-configuration --function-name HelloWorldFunction \
--environment "Variables={ENV_VAR1=value1,ENV_VAR2=value2}"

Layers​

Lambda layers allow you to package libraries and other dependencies separately from your function code.

  • Create a layer:
zip -r myLayer.zip python/
aws lambda publish-layer-version --layer-name myLayer --zip-file fileb://myLayer.zip

VPC Integration​

You can configure your Lambda function to access resources in a VPC.

aws lambda update-function-configuration --function-name HelloWorldFunction \
--vpc-config SubnetIds=subnet-abc123,SecurityGroupIds=sg-abc123

Performance and Scaling​

Cold Starts​

A cold start occurs when a new instance of the function is invoked after being idle. To mitigate cold starts:

  • Optimize initialization code.
  • Use Provisioned Concurrency for predictable performance.

Concurrency Limits​

You can configure reserved concurrency to limit the number of concurrent executions:

aws lambda put-function-concurrency --function-name HelloWorldFunction --reserved-concurrent-executions 10

Testing and Debugging​

Local Testing​

Use the AWS SAM CLI to test Lambda functions locally:

sam local invoke HelloWorldFunction -e event.json

Debugging​

Utilize CloudWatch Logs to debug issues by adding log statements in your code:

import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
logger.info("Event: %s", event)
return {
'statusCode': 200,
'body': 'Hello, World!'
}

Real-World Examples​

S3 Event Trigger​

Trigger a Lambda function when an object is uploaded to an S3 bucket:

import json

def lambda_handler(event, context):
for record in event['Records']:
s3 = record['s3']
bucket = s3['bucket']['name']
key = s3['object']['key']
print(f'Received event. Bucket: {bucket}, Key: {key}')
return {
'statusCode': 200,
'body': json.dumps('Processed S3 event')
}

DynamoDB Stream​

Process DynamoDB stream events:

import json

def lambda_handler(event, context):
for record in event['Records']:
if record['eventName'] == 'INSERT':
new_image = record['dynamodb']['NewImage']
print(f'New item added: {new_image}')
return {
'statusCode': 200,
'body': json.dumps('Processed DynamoDB stream event')
}

Conclusion​

AWS Lambda provides a powerful and flexible way to build serverless applications. By understanding its key concepts, setting up your environment, and leveraging advanced features, you can build scalable and cost-efficient solutions. This guide serves as a starting point for your journey into serverless architecture using AWS Lambda.