0% found this document useful (0 votes)
15 views30 pages

AWS Lambda Starter guide_2024_12_01

Uploaded by

Venkata Ramana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views30 pages

AWS Lambda Starter guide_2024_12_01

Uploaded by

Venkata Ramana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Unlock

The Power
of AWS
Lambda
The Term
“Serverless”
Lambda is a serverless compute service
that lets you run code without provisioning
or managing servers.

Key points:
Serverless Paradigm: Emphasizes the shift
from traditional server management to
deploying code in the form of functions.

Deployment Simplicity: Developers focus


solely on writing code, leaving
infrastructure concerns to AWS.

Event-Driven: Lambda functions are


triggered by events, making them ideal for
real-time data processing.
Key Features of
AWS Lambda
Lambda's robust feature set includes:
Multi-Language Support: Supports
Node.js, Python, Java, C#, and more.

Execution Time: Functions can run up to


15 minutes.

Environment Variables: Manage


configuration settings and secrets easily.

Monitoring: Integrated with AWS


CloudWatch for logging and performance
monitoring.
Lambda Pricing

Pay Per Calls:


First 1,000,000 requests are free

$0.20 per 1 million requests thereafter


($0.0000002 per request)

Pay per duration:


400,000 GB -seconds of compute time per
month for FREE

== 400,000 seconds if function is 1GB RAM

After that $1.00 for 600,000 GB-seconds

You can find overall pricing information here:


https://aws.amazon.com/lambda/pricing
Creating a
Lambda Function
Navigate to the AWS Lambda

Click on Create a Function

Select a Function Blueprint:


Author from scratch: Choose this to create a function from
scratch.
Blueprints: You can choose a pre-configured function template
(optional).
Creating a
Lambda Function
Configure the Basic Settings:
Function name: Enter a unique name for your function (e.g.,
MyFirstLambdaFunction).

Runtime: Choose the runtime environment (e.g., Python 3.10,


Node.js 18.x, etc.).

Click "Create function”


Creating a
Lambda Function
Write Your Lambda Function Code
Modify the Code according to your need

Test Your Lambda Function


Click on Test

Enter Event Name: testEvent

Event template: Select a


template (e.g., Hello World).

Event JSON: You can leave


the default

Click “Save”
Creating a
Lambda Function
Run the Test:

Click on Test

Review Results:
Check the execution results.
Lambda Execution
Role
Execution Role grants the Lambda function permissions to AWS
services / resources

Sample managed policies for Lambda:


AWSLambdaBasicExecutionRole – Upload logs to CloudWatch.

AWSLambdaDynamoDBExecutionRole – Read from DynamoDB


Streams.

AWSLambdaSQSQueueExecutionRole – Read from SQS.

AWSLambdaVPCAccessExecutionRole – Deploy Lambda


function in VPC.

AWSLambdaInvocation-DynamoDB – Allow a Lambda function


to be invoked by DynamoDB streams.

AWSLambdaInvocation-SQS – Allow a Lambda function to be


invoked by SQS messages.

AWSLambdaInvocation-SNS – Allow a Lambda function to be


invoked by SNS messages.

When you use an event source mapping to invoke your function,


Lambda uses the execution role to read event data.
Lambda Resource
Based Policies
Purpose:
Use resource-based policies to give other accounts and AWS
services permission to use your Lambda resources.

Access Control:
An IAM principal can access a Lambda function if:
The IAM policy attached to the principal authorizes it (e.g.,
user access).
The resource-based policy authorizes it (e.g., service
access).

Service Invocation:
When an AWS service like Amazon S3 calls your Lambda
function, the resource-based policy grants it the necessary
access.

Example Policy:
Lambda Environment
Variables
Purpose:
Environment variables allow you to configure various aspects of
your Lambda function's behavior without modifying its code.

They are useful for storing configuration settings, sensitive


information like API keys or database credentials, and any
other dynamic data needed by your function.

Accessing Environment Variables:


In your Lambda function code, you can access environment
variables using standard methods provided by your
programming language's runtime environment.

For example, in Node.js, you can access environment variables


using process.env.MY_VARIABLE.

Security Considerations:
AWS provides integration with Key Management Service (KMS) to
encrypt environment variables, ensuring that sensitive data
remains protected.
Lambda Environment
Variables
Navigate to Configuration tab:

Add Environment Variables:


Click on the "Edit” button.

Add key-value pairs for your


environment variables. For
example:
Key: DATABASE_URL
Value:
jdbc:mysql://example.com
:3306/database
Lambda Environment
Variables
Access Environment Variables in Your Code

In Node.js:

In Python:
AWS Lambda
Integrations
Main Ones:

API Gateway Kinesis DynamoDB

S3 CloudFront CloudWatch Events


EventBridge

CloudWatch Logs SNS SQS


Lambda Event Source
Mapping
Event source mapping is a mechanism that allows AWS Lambda to
automatically process events from various AWS services without
the need for manual invocation.

How Event Source Mapping Works


Configuration: You configure an event source mapping to
specify the event source and the target Lambda function.

Polling or Event Trigger: Depending on the event source, AWS


Lambda either polls the event source (e.g., SQS, DynamoDB) or
receives events directly (e.g., S3, SNS).

Invocation: When an event occurs, AWS Lambda invokes the


specified function and passes the event data as input

Your paragraph text


Creating Event Source
Mapping
For S3 (Object Creation Event)

Navigate to S3
Console

Select the Bucket: Choose


the S3 bucket you want to
trigger the Lambda function
from.

Properties Tab: Go to the


"Properties" tab.

Scroll down to the


"Event notification”

and click "Create


event notification”
Creating Event Source
Mapping
Configure Event:
Event Name: Enter a
name for the event

Events: Select the event


types (e.g., "All object
create events”)

Destination: Choose
"Lambda function” and
select the function you want
to trigger.

Click "Save” to create the


event notification.
Creating Event Source
Mapping
For DynamoDB Streams

Navigate to DynamoDB
Console.

Select the table, Click on the "Exports and streams” tab in the
table details view.

Look for the "DynamoDB


stream details” section.

If streams are not enabled,


Click on "Turn on”
Creating Event Source
Mapping
Configure the Trigger:
Goto Lambda, Choose the Lambda function you want to
associate with the DynamoDB stream.

Go to the "Configuration”
tab of your Lambda
function.

In the left pane, click on


"Triggers”

and Click on “Add trigger”

In the trigger
configuration pane,
select "DynamoDB”
as the trigger type.
Creating Event Source
Mapping
Configure the Trigger:
Select the DynamoDB table that you have enabled streams for.

Specify the batch size. Default is 100.

Starting Position:
TRIM_HORIZON to start from the oldest available record.
LATEST to start from the newest record.

Click "Add” to establish the trigger.


Lambda, Event and
Context Objects
Event Object: Contains details about the event that triggered
the Lambda function.

Context Object: Provides runtime information about the Lambda


function.

Example Structure:

Event Object

Context Object
Lambda, Event and
Context Objects
Handling Events in Lambda Function
Example of a Lambda function written in Python that processes
DynamoDB stream events:
Lambda Limits to Know
per region
Execution:
Memory allocation: 128 MB – 10GB (1 MB increments)

Maximum execution time: 900 seconds (15 minutes)

Environment variables (4 KB)

Disk capacity in the “function container” (in /tmp): 512 MB to


10GB

Concurrency executions: 1000 (can be increased)

Deployment:
Lambda function deployment size (compressed .zip): 50 MB

Size of uncompressed deployment (code + dependencies): 250


MB

Can use the /tmp directory to load other files at startup


Lambda Layers
Lambda Layers are a feature that allows you to package libraries,
custom runtimes, and other dependencies that you can use with
your Lambda functions. Layers help you to manage your code better
and keep your deployment package size small.

Key Concepts
Layer Creation:
Layers are created separately from Lambda functions.
Each layer can contain libraries, a custom runtime, or other
dependencies.
You can create a layer from the AWS Management Console, AWS CLI, or
AWS SDKs.

Layer Versions:
Layers are versioned. Each update to a layer creates a new version.
You can specify which version of the layer to use with your Lambda
function.

Layer Usage:
You can use up to five layers in a single Lambda function.
Layers can be shared across accounts and with the public.

Layer Content:
Layers can include libraries, binaries, custom runtimes, and other files.
The content must be in a ZIP file when uploading.
Creating Lambda
Layers

Go to the Lambda
console.

Select "Layers”
from the left-hand
navigation.

Click "Create
layer”.
Creating Lambda
Layers

Provide a Name to
the Layer

Upload a ZIP file


containing your
layer content.

Click "Create”.

Using AWS CLI:


Adding a Layer to a
Lambda Function
Go to the Lambda
function
configuration
page.

Go to the Lambda function configuration page. In the


"Function overview” section, click "Add a layer”.

Choose the Layer


Source

Select the version of


the layer to add.

Click "Add”
Lambda Layers
Best Practices
Use Layers for Dependencies:
Include common libraries and dependencies in layers to
keep the function deployment package small.

Version Control:
Maintain version control for layers. Test new versions before
updating functions to ensure compatibility.

Optimize Layer Size:


Keep the layer size minimal. Remove unnecessary files and
dependencies to stay within the 50 MB limit (250 MB
unzipped).

Security:
Secure sensitive data within layers using encryption and IAM
policies.
Use AWS KMS to encrypt sensitive information stored in
layers.
Next Week I‘m covering the Advanced
Lambda Topics on my Newsletter.
FOLLOW FOR
MORE GUIDES!

You might also like